dd-trace
Version:
Datadog APM tracing client for JavaScript
67 lines (52 loc) • 1.82 kB
JavaScript
const TracingPlugin = require('../../dd-trace/src/plugins/tracing')
const { extractErrorIntoSpanEvent, getSignature } = require('./utils')
class GraphQLExecutePlugin extends TracingPlugin {
static id = 'graphql'
static operation = 'execute'
static type = 'graphql'
static kind = 'server'
bindStart (ctx) {
const { operation, args, docSource } = ctx
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(this.operationName(), {
service: this.config.service || this.serviceName(),
resource: getSignature(document, name, type, this.config.signature),
kind: this.constructor.kind,
type: this.constructor.type,
meta: {
'graphql.operation.type': type,
'graphql.operation.name': name,
'graphql.source': source,
},
}, ctx)
addVariableTags(this.config, span, args.variableValues)
return ctx.currentStore
}
finish (ctx) {
const { res, args } = ctx
const span = ctx?.currentStore?.span || this.activeSpan
this.config.hooks.execute(span, args, res)
if (res?.errors) {
for (const err of res.errors) {
extractErrorIntoSpanEvent(this._tracerConfig, span, err)
}
}
super.finish(ctx)
return ctx.parentStore
}
}
// span-related
function addVariableTags (config, span, variableValues) {
if (!variableValues || !config.variables) return
const tags = {}
const variables = config.variables(variableValues)
for (const [param, value] of Object.entries(variables)) {
tags[`graphql.variables.${param}`] = value
}
span.addTags(tags)
}
module.exports = GraphQLExecutePlugin