Repository navigation
fix: record propagated errors once per request span - #200
Merged
Merged
Conversation
A single error thrown by an instrumented handler or hook was recorded as an exception event on both the child span (where it was thrown) and the parent request span via the onError hook, which backends count as two failures. Track errors already recorded on a child (handler/hook) span in a Set scoped to the request, and skip re-recording the same propagated exception on the request span. The request span still gets an ERROR status with the error message, and errors that never pass through a wrapped handler/hook (e.g. reply.send(err), done(err)) are still recorded on the request span. Fixes #196
gurgunday
approved these changes
Oct 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #196.
When an instrumented handler or hook throws, the same error was recorded as an
exceptionevent on both the child span where it was thrown and the parent request span (via the request'sonErrorhook). Backends that map each exception event to an error occurrence (e.g. Elastic APM) counted a single failure twice.Changes
Setscoped to the request.recordErrorInSpanHook, skipspan.recordExceptionwhen the propagated error was already recorded on a child span, while still marking the request span asERRORwith the error message.reply.send(err)ordone(err)— are still recorded on the request span as before.throw 'foo') and does not leak state across requests.Tests
Added tests covering:
reply.send(err)is still recorded on the request span.All tests pass for Fastify v4 and v5 with 100% coverage, TypeScript checks, and lint.