Observability and tracing
One URL and mostly 200 responses hide GraphQL problems. Add per-operation metrics, OpenTelemetry resolver traces, and error tracking that reads errors.
A server with GraphQL YogaErrors and partial responsesThe N+1 problem
Observability and tracing
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Standard HTTP monitoring sees one URL and mostly 200 responses, so it cannot tell a healthy GraphQL API from a broken one.
- Record each request by operation name, type and outcome, and require clients to name their operations.
- A trace with one span per resolver shows where time goes inside a single query, including N+1 patterns.
- Errors live in the response body, so error tracking must read the errors array and attach the operation and path.
- Do not log variables or results by default, since they carry user data, and sample resolver spans because they are numerous.
Why the usual dashboards go quiet
A REST service is easy to watch because the URL carries meaning. GET /orders has a latency and an error rate, and a spike in 500s on one route is obvious. GraphQL takes that away. Every request is POST /graphql, almost every response is HTTP 200, and the real outcome is in the body. A dashboard keyed on route and status shows one flat line even while a quarter of operations fail.
Observability for GraphQL means putting back the structure the transport removed. Three layers do it: per-operation metrics, per-request traces, and error tracking that understands the response.
Layer 1: per-operation metrics
The unit to measure is the operation: its type (query, mutation, subscription), its name, and its outcome. This is why variables and operation names matter beyond style. An anonymous { books { title } } cannot be told apart from any other. A named query Books { ... } can have its own latency percentiles, error rate and call count.
So make naming a rule: lint for it, and consider rejecting anonymous operations in production. For each request, record operation name, type, duration, whether errors was present, and the client name and version if your clients send them in a header. Keep cardinality in mind. Operation names are bounded by your clients, which is fine. Raw query text or variable values are not bounded, so do not use them as metric labels.
Layer 2: traces
Metrics tell you which operation is slow. A trace tells you why. OpenTelemetry represents a request as a tree of spans, where each span has a name, a duration and attributes. For GraphQL, the useful shape is one span per operation with child spans for parse, validate, execute and, optionally, each resolver.
I ran this with GraphQL Yoga 6.1.0 and @envelop/opentelemetry 10.0.1, exporting to an in-memory exporter (@opentelemetry/sdk-trace-base 2.12.1):
import { useOpenTelemetry } from "@envelop/opentelemetry";
createYoga({
schema,
plugins: [useOpenTelemetry({ resolvers: true, variables: false, result: false }, provider)],
});
For query Books { books { title author { name } } }, the exporter received an operation span named query.Books with graphql.execute.operationName=Books and graphql.execute.operationType=query, plus resolver spans named Query.books, Book.title, Book.author and Author.name. With two books, the Book.title, Book.author and Author.name spans each appeared twice. That repetition is the whole diagnostic value. A span for the same field repeated once per list item is the N+1 pattern from the N+1 lesson, now visible in a waterfall instead of inferred from database logs. After you add DataLoader, the repeated database spans collapse into one.
Replace the in-memory exporter with an OTLP exporter and a backend such as Honeycomb, Grafana Tempo or Jaeger, and the same spans become searchable. I did not run a collector or a backend in this session.
Two settings deserve attention. variables and result are off in my example, and that is the safe default: arguments and results contain emails, ids and tokens. And resolver-level spans are numerous, so in production sample them or limit them to resolvers you care about; a list of a thousand items makes thousands of spans.
Layer 3: errors
Because errors arrive in a 200, an error tracker hooked to HTTP exceptions misses most of them. Hook into the server’s error path instead, for example a plugin that reports each entry in errors along with the operation name, the path, the user id and the trace id. Separate the expected kinds from the unexpected. A FORBIDDEN is normal traffic. An internal error masked as “Unexpected error.” to the client is a bug that needs the original stack, which stays in your logs. Tie them together with a request id returned in the response extensions or a header, so a user report can lead to a trace.
What to alert on
A short list beats a wall of graphs:
- Error rate by operation, not overall. One broken screen disappears in the aggregate.
- p95 and p99 latency by operation.
- Resolver hot spots, the slowest fields by total time.
- Cost or complexity distribution, to tune the limits.
- Cache hit rate if you run a response cache.
- Deprecated field usage, which is how you know when it is safe to remove a field, as described in evolving a schema without versions.
That last one is where observability feeds schema management: usage data per field is what a registry uses to tell you whether a change breaks live clients. See schema registry and CI checks. The general side of this, latency and error budgets for any API, is in API performance metrics and monitoring.
With a router
In a federated setup, the router sees the whole query and emits a trace that includes each subgraph fetch, so you can see the plan as well as the resolvers. Propagate trace context to subgraphs, so their spans attach to the router’s. Apollo’s router has configurable telemetry for spans, events and metrics, which its own video below covers. That part is documentation-derived; I did not run a router.
What the video covers
Honeycomb’s “Graph Observability: Honeycomb and Apollo GraphQL With OpenTelemetry” is a 13-minute video from December 2021. Its chapters are: graph observability setup, adding experimental features, analyzing query performance, diagnosing with Honeycomb, and conclusion and best practices. It is a vendor demo for a specific product pairing, and it is built on Apollo Server’s experimental tracing of its day, so the setup steps have likely changed; I could not verify them. The value is the second half: how an investigation proceeds from a slow operation to a slow resolver. Apollo’s router telemetry video is the runner-up for the federated case; I did not find its duration or date.
Where this fits
With visibility on a running API, the question becomes how schema changes get shipped safely. Next is schema registry and CI checks.
Check yourself
Why does an uptime check on the GraphQL endpoint tell you little?
A failing query still returns HTTP 200 with errors in the body, so a status-code check passes while real operations fail.
Why insist that clients name their operations?
The operation name is the label that lets you group latency, error rate and volume per client query, instead of seeing anonymous requests to one URL.
What does a repeated resolver span under a list field suggest?
An N+1 pattern, where the field runs once per item, which batching with DataLoader would reduce.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.