fix #4251: wrap BackgroundTasks in their own span#4368
fix #4251: wrap BackgroundTasks in their own span#4368davidgss04 wants to merge 4 commits intoopen-telemetry:mainfrom
Conversation
|
Thanks for this! The fix to Please could you also commit fixes from:
|
Only the request/ASGI middleware was traced; background tasks were not. Background tasks ran after the response with no span, causing child spans to attach to a closed parent span. This change patches BackgroundTask.__call__ to wrap execution in a new span. Each background task now creates a child span of the request, ensuring a correct trace hierarchy. Add three regression tests for background task instrumentation.
d65500d to
ab9b962
Compare
|
Good evening, I have applied the requested |
|
Thanks @davidgss04 ! Forgot to also mention: please add an entry to |
|
Good evening @tammy-baylis-swi! I have added the entry to CHANGELOG.md has requested. Thanks! |
tammy-baylis-swi
left a comment
There was a problem hiding this comment.
Thanks again! This lgtm. 👍 The maintainers will also have to review this.
Description
Fixes #4251
FastAPI background tasks were not properly instrumented. While request spans were created correctly, background tasks executed after the response without their own span. As a result, any spans created inside a background task were incorrectly attached directly to the request span, which had already finished, leading to broken trace hierarchies and inaccurate timing information.
The fix patches
BackgroundTask.__call__to wrap its execution in a dedicated span. This ensures that each background task runs within its own span, correctly parented to the request span, and that any spans created inside the task are properly nested.The patch is applied only once using a guard (
hasattr) to avoid double instrumentation, and the original method is restored duringuninstrument_appto prevent side effects.Type of change
Testing
Added three regression tests:
test_background_task_span_parents_inner_spans— verifies that background tasks create a wrapper span and that spans created inside the task are correctly parentedtest_uninstrument_app_restores_background_task_call— ensures the originalBackgroundTask.__call__is restored after uninstrumentationtest_background_task_span_not_duplicated_on_double_instrument_app— verifies that repeated instrumentation does not create duplicate spans or apply the patch multiple timesDoes This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.