dd-trace
Version:
Datadog APM tracing client for JavaScript
89 lines (70 loc) • 2.26 kB
JavaScript
const TracingPlugin = require('../../dd-trace/src/plugins/tracing')
const { extractErrorIntoSpanEvent } = require('./utils')
let tools
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) {
const tags = {}
if (variableValues && config.variables) {
const variables = config.variables(variableValues)
for (const param in variables) {
tags[`graphql.variables.${param}`] = variables[param]
}
}
span.addTags(tags)
}
function getSignature (document, operationName, operationType, calculate) {
if (calculate !== false && tools !== false) {
try {
try {
tools = tools || require('./tools')
} catch (e) {
tools = false
throw e
}
return tools.defaultEngineReportingSignature(document, operationName)
} catch {
// safety net
}
}
return [operationType, operationName].filter(Boolean).join(' ')
}
module.exports = GraphQLExecutePlugin