Skip to content

Commit 8cc7705

Browse files
authored
fix apollo gateway get signature method (#4174)
* fix apollo gateway get signature method & add checks for config values
1 parent c9e1082 commit 8cc7705

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

index.d.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,26 @@ declare namespace tracer {
11241124
* This module uses graphql operations to service requests & thus generates graphql spans.
11251125
* We recommend disabling the graphql plugin if you only want to trace @apollo/gateway
11261126
*/
1127-
interface apollo extends Instrumentation {}
1127+
interface apollo extends Instrumentation {
1128+
/**
1129+
* Whether to include the source of the operation within the query as a tag
1130+
* on every span. This may contain sensitive information and should only be
1131+
* enabled if sensitive data is always sent as variables and not in the
1132+
* query text.
1133+
*
1134+
* @default false
1135+
*/
1136+
source?: boolean;
1137+
1138+
/**
1139+
* Whether to enable signature calculation for the resource name. This can
1140+
* be disabled if your apollo/gateway operations always have a name. Note that when
1141+
* disabled all queries will need to be named for this to work properly.
1142+
*
1143+
* @default true
1144+
*/
1145+
signature?: boolean;
1146+
}
11281147

11291148
/**
11301149
* This plugin automatically instruments the

packages/datadog-plugin-apollo/src/gateway/request.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class ApolloGatewayRequestPlugin extends ApolloBasePlugin {
4242
if (requestContext?.operationName) {
4343
spanData.meta['graphql.operation.name'] = requestContext.operationName
4444
}
45-
if (gateway?.config?.telemetry?.includeDocument !== false && requestContext?.source) {
45+
if ((this.config.source || gateway?.config?.telemetry?.includeDocument) && requestContext?.source) {
4646
spanData.meta['graphql.source'] = requestContext.source
4747
}
4848

@@ -121,7 +121,7 @@ function getSignature (document, operationName, operationType, calculate) {
121121
if (calculate !== false && tools !== false) {
122122
try {
123123
try {
124-
tools = tools || require('./tools')
124+
tools = tools || require('../../../datadog-plugin-graphql/src/tools')
125125
} catch (e) {
126126
tools = false
127127
throw e

packages/datadog-plugin-apollo/test/index.spec.js

+15-19
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,18 @@ describe('Plugin', () => {
134134
})
135135
it('should instrument apollo/gateway', done => {
136136
const operationName = 'MyQuery'
137-
const resource = `query ${operationName}`
138-
const source = `${resource} { hello(name: "world") }`
137+
const source = `query ${operationName} { hello(name: "world") }`
139138
const variableValues = { who: 'world' }
140139
agent
141140
.use((traces) => {
142141
// the spans are in order of execution
143142
expect(traces[0][0]).to.have.property('name', expectedSchema.server.opName)
144143
expect(traces[0][0]).to.have.property('service', expectedSchema.server.serviceName)
145-
expect(traces[0][0]).to.have.property('resource', resource)
144+
expect(traces[0][0]).to.have.property('resource', 'query MyQuery{hello(name:"")}')
146145
expect(traces[0][0]).to.have.property('type', 'web')
147146
expect(traces[0][0]).to.have.property('error', 0)
148147
expect(traces[0][0].meta).to.have.property('graphql.operation.name', operationName)
149-
expect(traces[0][0].meta).to.have.property('graphql.source', source)
148+
expect(traces[0][0].meta).to.not.have.property('graphql.source')
150149
expect(traces[0][0].meta).to.have.property('graphql.operation.type', 'query')
151150
expect(traces[0][0].meta).to.have.property('component', 'apollo.gateway')
152151

@@ -196,10 +195,10 @@ describe('Plugin', () => {
196195
.use((traces) => {
197196
expect(traces[0][0]).to.have.property('name', expectedSchema.server.opName)
198197
expect(traces[0][0]).to.have.property('service', expectedSchema.server.serviceName)
199-
expect(traces[0][0]).to.have.property('resource', 'query')
198+
expect(traces[0][0]).to.have.property('resource', '{hello(name:"")}')
200199
expect(traces[0][0]).to.have.property('type', 'web')
201200
expect(traces[0][0]).to.have.property('error', 0)
202-
expect(traces[0][0].meta).to.have.property('graphql.source', source)
201+
expect(traces[0][0].meta).to.not.have.property('graphql.source')
203202
expect(traces[0][0].meta).to.have.property('graphql.operation.type', 'query')
204203
expect(traces[0][0].meta).to.have.property('component', 'apollo.gateway')
205204
})
@@ -228,10 +227,10 @@ describe('Plugin', () => {
228227
.use((traces) => {
229228
expect(traces[0][0]).to.have.property('name', expectedSchema.server.opName)
230229
expect(traces[0][0]).to.have.property('service', expectedSchema.server.serviceName)
231-
expect(traces[0][0]).to.have.property('resource', 'query')
230+
expect(traces[0][0]).to.have.property('resource', '{human{address{civicNumber street}name}}')
232231
expect(traces[0][0]).to.have.property('type', 'web')
233232
expect(traces[0][0]).to.have.property('error', 0)
234-
expect(traces[0][0].meta).to.have.property('graphql.source', source)
233+
expect(traces[0][0].meta).to.not.have.property('graphql.source')
235234
expect(traces[0][0].meta).to.have.property('graphql.operation.type', 'query')
236235
expect(traces[0][0].meta).to.have.property('component', 'apollo.gateway')
237236
})
@@ -315,8 +314,7 @@ describe('Plugin', () => {
315314
it('should instrument plan failure', done => {
316315
let error
317316
const operationName = 'MyQuery'
318-
const resource = `subscription ${operationName}`
319-
const source = `${resource} { hello(name: "world") }`
317+
const source = `subscription ${operationName} { hello(name: "world") }`
320318
const variableValues = { who: 'world' }
321319
agent
322320
.use((traces) => {
@@ -350,8 +348,7 @@ describe('Plugin', () => {
350348
it('should instrument fetch failure', done => {
351349
let error
352350
const operationName = 'MyQuery'
353-
const resource = `query ${operationName}`
354-
const source = `${resource} { hello(name: "world") }`
351+
const source = `query ${operationName} { hello(name: "world") }`
355352
const variableValues = { who: 'world' }
356353
agent
357354
.use((traces) => {
@@ -410,8 +407,7 @@ describe('Plugin', () => {
410407

411408
it('should run spans in the correct context', done => {
412409
const operationName = 'MyQuery'
413-
const resource = `query ${operationName}`
414-
const source = `${resource} { hello(name: "world") }`
410+
const source = `query ${operationName} { hello(name: "world") }`
415411
const variableValues = { who: 'world' }
416412

417413
agent
@@ -446,8 +442,7 @@ describe('Plugin', () => {
446442
withNamingSchema(
447443
() => {
448444
const operationName = 'MyQuery'
449-
const resource = `query ${operationName}`
450-
const source = `${resource} { hello(name: "world") }`
445+
const source = `query ${operationName} { hello(name: "world") }`
451446
const variableValues = { who: 'world' }
452447
gateway()
453448
.then(({ executor }) => {
@@ -464,18 +459,19 @@ describe('Plugin', () => {
464459

465460
describe('with configuration', () => {
466461
before(() => {
467-
return agent.load('apollo', { service: 'custom' })
462+
return agent.load('apollo', { service: 'custom', source: true, signature: false })
468463
})
469464

470465
it('should be configured with the correct values', done => {
471466
const operationName = 'MyQuery'
472-
const resource = `query ${operationName}`
473-
const source = `${resource} { hello(name: "world") }`
467+
const source = `query ${operationName} { hello(name: "world") }`
474468
const variableValues = { who: 'world' }
475469
agent
476470
.use((traces) => {
477471
expect(traces[0][0]).to.have.property('name', expectedSchema.server.opName)
478472
expect(traces[0][0]).to.have.property('service', 'custom')
473+
expect(traces[0][0]).to.have.property('resource', `query ${operationName}`)
474+
expect(traces[0][0].meta).to.have.property('graphql.source', source)
479475

480476
expect(traces[0][1]).to.have.property('name', 'apollo.gateway.validate')
481477
expect(traces[0][1]).to.have.property('service', 'custom')

0 commit comments

Comments
 (0)