⬡ GraphQL Advanced

How do you implement distributed tracing for GraphQL?

Answer

Distributed tracing in GraphQL helps identify performance bottlenecks at the field level. Approaches: (1) Apollo Tracing: Apollo Server's built-in tracing adds resolver timing to the response's extensions object — enable with the ApolloServerPluginInlineTrace plugin. Apollo Studio automatically collects and displays these traces. (2) OpenTelemetry: use @opentelemetry/api with a GraphQL instrumentation plugin (@opentelemetry/instrumentation-graphql) — creates spans for parse, validate, execute, and individual resolver calls. Send traces to Jaeger, Zipkin, Tempo, or Honeycomb. (3) Custom resolver middleware: wrap all resolvers with timing logic and send to your APM. (4) Logging: log slow resolvers (>100ms) with their full path and arguments. Key metrics to track: per-operation latency percentiles (p50, p95, p99), per-field resolver latency, cache hit rates, error rates by operation. Set up alerts for p99 latency regressions and error rate spikes.