Skip to content
10 changes: 6 additions & 4 deletions packages/datadog-plugin-graphql/src/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ let tools
class GraphQLExecutePlugin extends TracingPlugin {
static get id () { return 'graphql' }
static get operation () { return 'execute' }
static get type () { return 'graphql' }
static get kind () { return 'server' }

start ({ operation, args, docSource }) {
const type = operation && operation.operation
const name = operation && operation.name && operation.name.value
const document = args.document
const source = this.config.source && document && docSource

const span = this.startSpan('graphql.execute', {
service: this.config.service,
const span = this.startSpan(this.operationName(), {
service: this.config.service || this.serviceName(),
resource: getSignature(document, name, type, this.config.signature),
kind: 'server',
type: 'graphql',
kind: this.constructor.kind,
type: this.constructor.type,
meta: {
'graphql.operation.type': type,
'graphql.operation.name': name,
Expand Down
18 changes: 18 additions & 0 deletions packages/datadog-plugin-graphql/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { expect } = require('chai')
const semver = require('semver')
const agent = require('../../dd-trace/test/plugins/agent')
const { ERROR_MESSAGE, ERROR_TYPE, ERROR_STACK } = require('../../dd-trace/src/constants')
const namingSchema = require('./naming')

describe('Plugin', () => {
let tracer
Expand Down Expand Up @@ -175,6 +176,23 @@ describe('Plugin', () => {
return agent.close({ ritmReset: false })
})

withNamingSchema(
done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }
graphql.graphql({ schema, source, variableValues })
.then(done)
.catch(done)
},
() => namingSchema.server.opName,
() => namingSchema.server.serviceName,
'test',
(traces) => {
const spans = sort(traces[0])
return spans[0]
}
)

it('should instrument parsing', done => {
const source = `query MyQuery { hello(name: "world") }`
const variableValues = { who: 'world' }
Expand Down
14 changes: 14 additions & 0 deletions packages/datadog-plugin-graphql/test/naming.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { resolveNaming } = require('../../dd-trace/test/plugins/helpers')

module.exports = resolveNaming({
server: {
v0: {
opName: 'graphql.execute',
serviceName: 'test'
},
v1: {
opName: 'graphql.server.request',
serviceName: 'test'
}
}
})
12 changes: 12 additions & 0 deletions packages/dd-trace/src/service-naming/schemas/v0/graphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { identityService } = require('../util')

const graphql = {
server: {
graphql: {
opName: () => 'graphql.execute',
serviceName: identityService
}
}
}

module.exports = graphql
3 changes: 2 additions & 1 deletion packages/dd-trace/src/service-naming/schemas/v0/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const SchemaDefinition = require('../definition')
const messaging = require('./messaging')
const storage = require('./storage')
const graphql = require('./graphql')
const web = require('./web')

module.exports = new SchemaDefinition({ messaging, storage, web })
module.exports = new SchemaDefinition({ messaging, storage, web, graphql })
12 changes: 12 additions & 0 deletions packages/dd-trace/src/service-naming/schemas/v1/graphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { identityService } = require('../util')

const graphql = {
server: {
graphql: {
opName: () => 'graphql.server.request',
serviceName: identityService
}
}
}

module.exports = graphql
3 changes: 2 additions & 1 deletion packages/dd-trace/src/service-naming/schemas/v1/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const SchemaDefinition = require('../definition')
const messaging = require('./messaging')
const storage = require('./storage')
const graphql = require('./graphql')
const web = require('./web')

module.exports = new SchemaDefinition({ messaging, storage, web })
module.exports = new SchemaDefinition({ messaging, storage, web, graphql })
10 changes: 8 additions & 2 deletions packages/dd-trace/test/setup/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ function loadInstFile (file, instrumentations) {
})
}

function withNamingSchema (spanProducerFn, expectedOpName, expectedServiceName, expectedShortCircuitName) {
function withNamingSchema (
spanProducerFn,
expectedOpName,
expectedServiceName,
expectedShortCircuitName,
selectSpan = (traces) => traces[0][0]
) {
let fullConfig

describe('service and operation naming', () => {
Expand All @@ -69,7 +75,7 @@ function withNamingSchema (spanProducerFn, expectedOpName, expectedServiceName,
it(`should conform to the naming schema`, done => {
agent
.use(traces => {
const span = traces[0][0]
const span = selectSpan(traces)
expect(span).to.have.property('name', expectedOpName())
expect(span).to.have.property('service', expectedServiceName())
})
Expand Down