Commit 0158624
[Tracing] Fix Hangfire baggage accumulation (#8900)
## Summary of changes
Fixes baggage accumulating across sequential Hangfire jobs processed by
the same worker. `DatadogHangfireServerFilter.OnPerforming` now replaces
`Baggage.Current` with the job's own extracted baggage instead of
merging into it, saving whatever baggage was ambient beforehand;
`OnPerformed` restores that saved baggage after the job completes.
## Reason for change
`Baggage.Current` is a mutable reference type stored in an `AsyncLocal`.
Hangfire workers process jobs sequentially on one persistent
thread/`ExecutionContext` (unlike, e.g., one `ExecutionContext` per HTTP
request), so that `AsyncLocal` is never naturally reset between jobs.
`OnPerforming` was merging each job's extracted baggage into the
existing `Baggage.Current` instance rather than replacing it, and
`OnPerformed` never reset it, so every job's baggage accumulated into
whatever the previous job on that worker had left behind. Over time this
produces oversized `baggage` headers on downstream HTTP calls, which can
be rejected by services enforcing header size limits.
## Implementation details
In `DatadogHangfireServerFilter.cs`:
- `OnPerforming`: save the current `Baggage.Current` into the job's
`Items` bag, then replace it with `propagationContext.Baggage ?? new
Baggage()` (the job's own extracted baggage) instead of merging into the
ambient instance.
- `OnPerformed`: restore the baggage saved in `OnPerforming` (in a
`finally` block, so it runs even if scope disposal throws), since the
worker's `ExecutionContext` is persistent across jobs. This mirrors the
save/replace/restore pattern used by OpenTelemetry's own Hangfire
instrumentation.
## Test coverage
Added `HangfireTests.BaggageDoesNotAccumulateAcrossSequentialJobs`,
which runs two sequential jobs on a single-worker Hangfire server, each
enqueued from its own isolated producer context with distinct baggage,
and asserts that the second job's `Baggage.Current` contains only its
own entry (not the first job's). The test also asserts both jobs ran on
the same worker thread, confirming the
single-worker/persistent-`ExecutionContext` precondition that makes the
bug reproducible.
## Other details
Fixes issue
[#8895](#8895)
---------
Co-authored-by: Steven Bouwkamp <steven.bouwkamp@datadoghq.com>
Co-authored-by: Zach Montoya <zach.montoya@datadoghq.com>1 parent ef7e8d0 commit 0158624
6 files changed
Lines changed: 111 additions & 11 deletions
File tree
- tracer
- src/Datadog.Trace/ClrProfiler/AutoInstrumentation/Hangfire
- test
- Datadog.Trace.ClrProfiler.IntegrationTests
- test-applications/integrations/Samples.Hangfire
- Jobs
Lines changed: 25 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
32 | 36 | | |
33 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
34 | 40 | | |
35 | 41 | | |
36 | 42 | | |
| |||
45 | 51 | | |
46 | 52 | | |
47 | 53 | | |
48 | | - | |
| 54 | + | |
49 | 55 | | |
50 | | - | |
| 56 | + | |
51 | 57 | | |
52 | | - | |
53 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
54 | 67 | | |
55 | | - | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
56 | 74 | | |
57 | | - | |
| 75 | + | |
58 | 76 | | |
59 | | - | |
60 | | - | |
61 | 77 | | |
62 | 78 | | |
63 | 79 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
32 | 51 | | |
33 | 52 | | |
34 | 53 | | |
| |||
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
14 | 25 | | |
15 | 26 | | |
16 | 27 | | |
| |||
Lines changed: 53 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
46 | 54 | | |
47 | 55 | | |
48 | 56 | | |
| |||
54 | 62 | | |
55 | 63 | | |
56 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
57 | 109 | | |
58 | 110 | | |
59 | 111 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
0 commit comments