Fastify OpenTelemetry: a 415 should not mark the server span as ERROR
Fastify OTEL #210 fixed request spans marked ERROR for 4xx because onError ran before statusCode existed. The fix applies status when the response finishes.
A completed HTTP server span was marked ERROR when Fastify returned a 4xx. The reported case was a POST with Content-Type application/octet-stream to a route that only accepts JSON. Fastify responded 415 and the handler never ran. The request span was exported as ERROR, with an exception event.
This is one of the pull requests behind Six merged PRs in 72 hours. The series looks at the failure, the fix and the review evidence, one merge at a time.
The change: +86 / -35 across 3 files. PR 210.
The bug
The cause was ordering. onError ran before Fastify copied error.statusCode onto the reply, so the instrumentation classified the request before the status existed. It saw an error object, not a final HTTP response.
This matters because 4xx is a client outcome, not a server failure. OpenTelemetry’s HTTP semantic conventions leave a server span unset for 4xx and set ERROR for 5xx. A dashboard that treats every 415 as an error teaches the team to ignore the dashboard.
Why it mattered
Span status is part of the alerting contract. If a validation error, an unsupported media type or a 404 page creates ERROR spans, error-rate alerts stop being about server health. The noise also hides the real 5xx that the team needs to see.
The fix
onError now only remembers the error. The status is applied when the response finishes, after the status code is final: below 500 the request span stays unset and no exception is recorded on it; a 5xx sets ERROR and keeps error.message when the error was captured; an exception already recorded on a child handler or hook span is not recorded again. A reply.code(500).send() with no thrown error still gets a bare ERROR status.
The evidence
The PR adds coverage for the 415 path and the 5xx path. It runs npm test on Node 22.19.0 against Fastify 4 and Fastify 5, 80 tests each, with 100% coverage on index.js and 24 TypeScript assertions. It also cites the OpenTelemetry HTTP rule, which is stronger than asserting only the local Fastify behavior.
What I would repeat
When a signal is derived from a request, use the final response, not the first error callback. Then encode the external semantic convention in the test. Instrumentation bugs are data bugs: they make the system look wrong even when the code is right.
Sources
If this was useful, connect with me on LinkedIn; I write about the bugs behind merged pull requests and the evidence that gets them accepted.
FAQ
- Why was a 4xx marked ERROR?
- onError ran before Fastify copied the status code to the reply, so the request span was classified before the final status existed.
- What is the correct OpenTelemetry status for 4xx?
- For an HTTP server span, 4xx stays unset; 5xx is ERROR. That is the HTTP semantic convention.
- Did the handler span change too?
- A thrown 400 still marks the handler span ERROR. Only the request span follows the HTTP server-span rule.
Join the Newsletter
Subscribe to get my latest content by email.
We won't send you spam. Unsubscribe at any time.