Repository navigation
fix(tracing): isolate Celery contexts and guard unsampled tool spans - #45049
Conversation
Pyrefly Type Coverage
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #45049 +/- ##
=======================================
Coverage 89.55% 89.56%
=======================================
Files 6013 6013
Lines 377254 377253 -1
Branches 76379 76379
=======================================
+ Hits 337860 337873 +13
+ Misses 33401 33389 -12
+ Partials 5993 5991 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
MRZHUH
left a comment
There was a problem hiding this comment.
Reviewed all five changed files and the surrounding worker/tool instrumentation paths. No blocking findings.
The SQL-commenter token is now invocation-local, and signal registration correctly unwinds the SQL context before the OTel task scope. Existing external Celery instrumentation is preserved. The early is_recording() guard avoids accessing attributes on Logfire-wrapped NonRecordingSpan objects while retaining native-tool ownership checks.
Verified CI for this head and its merge with main: API unit shards report 24,360 passed (4 skipped); Agent tests report 1,046 passed (13 skipped). API integration/controller tests, Python style, Pyrefly, CodeQL, and image builds also passed. Reviewed the real Celery concurrency and Logfire/Pydantic AI regression tests; no local test rerun was performed.
Production rollout and impact verification remain separate from this code review.
Summary
Concurrent executions of the same Celery Task could overwrite the SQL commenter's shared context token, causing cross-Context detach errors and incomplete context restoration. Store the token on the invocation-local request and register SQL cleanup before OTel cleanup, so the contexts unwind in reverse activation order.
Handled Dify core-tool errors could also abort an Agent when Logfire wrapped an unsampled
NonRecordingSpanasReadableSpan. Checkis_recording()before reading attributes, retaining the existing native-tool ownership checks and recoverable error observations.Related to CRD-720.
If Celery was already instrumented externally, preserve its providers and spans and skip the additional SQL-commenter hooks; their required cleanup order cannot be established safely after registration. No dependency, lockfile, or deployment configuration changes are included.
Validation
Task.apply, concurrent invocations, both completion orders, sampled/unsampled spans, failure, retry, cooperative stop, and pre-existing instrumentation.make lint, targetedmake type-check, and package Ruff/format/Pyrefly checks passed.The production detach logs do not include caller frames, so this fixes the reproduced SQL-commenter defect without claiming that every historical detach log had that origin. Production rollout and impact measurement remain separate follow-up work.
Checklist
From Codex