Skip to content

fix: preserve the error description on the request span for 5xx - #191

Open
willspoint wants to merge 1 commit into
fastify:mainfrom
willspoint:fix/preserve-error-message-on-5xx-root-span
Open

fix: preserve the error description on the request span for 5xx#191
willspoint wants to merge 1 commit into
fastify:mainfrom
willspoint:fix/preserve-error-message-on-5xx-root-span

Conversation

@willspoint

@willspoint willspoint commented Sep 4, 2026

Copy link
Copy Markdown

recordErrorInSpanHook (onError) records the error status with error.message as the description. finalizeResponseSpanHook (onSend) runs afterwards and, for statusCode >= 500, called setStatus again with no message. Per the OTel spec setStatus replaces the status, so the description was cleared immediately before span.end() and the root request span was exported as ERROR with an empty description.

Because the branch was gated on >= 500, 4xx kept its description and 5xx always lost it; child spans were unaffected.

Track whether the error hook already recorded a status and only apply the bare status when it did not, so a 5xx that never threw (e.g. reply.code(500).send()) still gets an error status.

Checklist

`recordErrorInSpanHook` (onError) records the error status with
`error.message` as the description. `finalizeResponseSpanHook` (onSend)
runs afterwards and, for `statusCode >= 500`, called `setStatus` again
with no message. Per the OTel spec `setStatus` replaces the status, so
the description was cleared immediately before `span.end()` and the
root request span was exported as ERROR with an empty description.

Because the branch was gated on `>= 500`, 4xx kept its description and
5xx always lost it; child spans were unaffected.

Track whether the error hook already recorded a status and only apply
the bare status when it did not, so a 5xx that never threw (e.g.
`reply.code(500).send()`) still gets an error status.
@willspoint

Copy link
Copy Markdown
Author

Do I need to change documentation for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant