Skip to content

Commit 670471b

Browse files
rarguelloFtlhunterJordi Bertran de Balanda
authored andcommitted
implement naming schema for graphql plugin (#3279)
* implement naming schema for graphql plugin * implement schema v1 and add tests --------- Co-authored-by: Thomas Hunter II <tlhunter@datadog.com> Co-authored-by: Jordi Bertran de Balanda <jordi.bertran@datadoghq.com>
1 parent 74f5c4b commit 670471b

File tree

8 files changed

+72
-8
lines changed

8 files changed

+72
-8
lines changed

packages/datadog-plugin-graphql/src/execute.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ let tools
77
class GraphQLExecutePlugin extends TracingPlugin {
88
static get id () { return 'graphql' }
99
static get operation () { return 'execute' }
10+
static get type () { return 'graphql' }
11+
static get kind () { return 'server' }
1012

1113
start ({ operation, args, docSource }) {
1214
const type = operation && operation.operation
1315
const name = operation && operation.name && operation.name.value
1416
const document = args.document
1517
const source = this.config.source && document && docSource
1618

17-
const span = this.startSpan('graphql.execute', {
18-
service: this.config.service,
19+
const span = this.startSpan(this.operationName(), {
20+
service: this.config.service || this.serviceName(),
1921
resource: getSignature(document, name, type, this.config.signature),
20-
kind: 'server',
21-
type: 'graphql',
22+
kind: this.constructor.kind,
23+
type: this.constructor.type,
2224
meta: {
2325
'graphql.operation.type': type,
2426
'graphql.operation.name': name,

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { expect } = require('chai')
44
const semver = require('semver')
55
const agent = require('../../dd-trace/test/plugins/agent')
66
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
7+
const namingSchema = require('./naming')
78

89
describe('Plugin', () => {
910
let tracer
@@ -175,6 +176,21 @@ describe('Plugin', () => {
175176
return agent.close({ ritmReset: false })
176177
})
177178

179+
withNamingSchema(
180+
() => {
181+
const source = `query MyQuery { hello(name: "world") }`
182+
const variableValues = { who: 'world' }
183+
graphql.graphql({ schema, source, variableValues })
184+
},
185+
() => namingSchema.server.opName,
186+
() => namingSchema.server.serviceName,
187+
'test',
188+
(traces) => {
189+
const spans = sort(traces[0])
190+
return spans[0]
191+
}
192+
)
193+
178194
it('should instrument parsing', done => {
179195
const source = `query MyQuery { hello(name: "world") }`
180196
const variableValues = { who: 'world' }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { resolveNaming } = require('../../dd-trace/test/plugins/helpers')
2+
3+
module.exports = resolveNaming({
4+
server: {
5+
v0: {
6+
opName: 'graphql.execute',
7+
serviceName: 'test'
8+
},
9+
v1: {
10+
opName: 'graphql.server.request',
11+
serviceName: 'test'
12+
}
13+
}
14+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { identityService } = require('../util')
2+
3+
const graphql = {
4+
server: {
5+
graphql: {
6+
opName: () => 'graphql.execute',
7+
serviceName: identityService
8+
}
9+
}
10+
}
11+
12+
module.exports = graphql
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const SchemaDefinition = require('../definition')
22
const messaging = require('./messaging')
33
const storage = require('./storage')
4+
const graphql = require('./graphql')
45
const web = require('./web')
56

6-
module.exports = new SchemaDefinition({ messaging, storage, web })
7+
module.exports = new SchemaDefinition({ messaging, storage, web, graphql })
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { identityService } = require('../util')
2+
3+
const graphql = {
4+
server: {
5+
graphql: {
6+
opName: () => 'graphql.server.request',
7+
serviceName: identityService
8+
}
9+
}
10+
}
11+
12+
module.exports = graphql
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const SchemaDefinition = require('../definition')
22
const messaging = require('./messaging')
33
const storage = require('./storage')
4+
const graphql = require('./graphql')
45
const web = require('./web')
56

6-
module.exports = new SchemaDefinition({ messaging, storage, web })
7+
module.exports = new SchemaDefinition({ messaging, storage, web, graphql })

packages/dd-trace/test/setup/mocha.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ function loadInstFile (file, instrumentations) {
4848
})
4949
}
5050

51-
function withNamingSchema (spanProducerFn, expectedOpName, expectedServiceName, expectedShortCircuitName) {
51+
function withNamingSchema (
52+
spanProducerFn,
53+
expectedOpName,
54+
expectedServiceName,
55+
expectedShortCircuitName,
56+
selectSpan = (traces) => traces[0][0]
57+
) {
5258
let fullConfig
5359

5460
describe('service and operation naming', () => {
@@ -69,7 +75,7 @@ function withNamingSchema (spanProducerFn, expectedOpName, expectedServiceName,
6975
it(`should conform to the naming schema`, done => {
7076
agent
7177
.use(traces => {
72-
const span = traces[0][0]
78+
const span = selectSpan(traces)
7379
expect(span).to.have.property('name', expectedOpName())
7480
expect(span).to.have.property('service', expectedServiceName())
7581
})

0 commit comments

Comments
 (0)