GraphQL Instrumentation: Obtaining traceId
from Active Context / Setting Active Context
#2019
-
This is one of two questions that I mentioned in my introduction 🙂 apologies if I do not use all the right terms, I am still learning and trying to be precise. 🙇 Hopefully the working example I provided is helpful in clarification!Question part 1.a
I would like to intercept and obtain the Through experimentation (and honestly sheer luck/curiosity in logging out GraphQL context) I learned that the Operation's Span and SpanContext are available within each resolver that does let me get the Question part 1.b
I tried the following code example provided by @dyladan from a Slack thread in #otel-js and the span from the active context did not appear to be set. //Code example
span1 = api.trace.getTracerProvider('graphql').getTracer('graphql').startSpan('GraphQLContextSpan')
let ctx = api.setSpan(api.context.active(), span1);
api.context.with(ctx, () => {
const s = api.getSpan(api.context.active());
console.log('results', s === span1) //evaluates false The current code that I have (that my working example is based off of) is using const traceID: string = provider
?.getTracer(process.env.TRACE_SERVICE_NAME)
?.getCurrentSpan()
?.context().traceId;
I look forward to hearing back and engaging with y'all! 😁 cc: @dyladan, @obecny (since I saw all your work on the GraphQL instrumentation), @vmarchaud (from our brief conversation on Slack) Working Code Example to HighlightI have created a working example of the problem I am trying to solve here: code: https://github.com/mannyluvstacos/graphql-otel-test/blob/main/index.js One should be able to
ContextServer: NodeJS + Fastify + Apollo-Server-Fastify "dependencies": {
"@opentelemetry/api": "^0.18.0",
"@opentelemetry/exporter-jaeger": "^0.18.0",
"@opentelemetry/exporter-zipkin": "^0.18.0",
"@opentelemetry/instrumentation": "^0.18.0",
"@opentelemetry/instrumentation-graphql": "^0.14.0",
"@opentelemetry/instrumentation-grpc": "^0.18.0",
"@opentelemetry/node": "^0.18.0",
"@opentelemetry/tracing": "^0.18.0",
"apollo-server-fastify": "^2.21.1",
"fastify": "^2.14.1",
"graphql": "^15.5.0",
"pino": "^6.11.2"
}, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The issue is that you didn't register the node sdk, so the context manager that makes everything work isn't enabled. You just need to add: tracerProvider.register() After this line and it should works (both for using |
Beta Was this translation helpful? Give feedback.
The issue is that you didn't register the node sdk, so the context manager that makes everything work isn't enabled. You just need to add:
After this line and it should works (both for using
getSpan
and getting thetraceId
from the spanContext.