Commit 55659d9
feat(traces): [SVLS-8734] respect Datadog-Client-Computed-Stats header (#1118)
## Background
traces/stats that go through Lambda extension can generate stats in
three ways:
1. tracer generates stats and sends them to extension at `/v0.6/stats`.
Only works if the env var `DD_TRACE_STATS_COMPUTATION_ENABLED` is
`true`.
2. extension generates stats from traces. It's off by default, and works
only if env var `DD_COMPUTE_TRACE_STATS_ON_EXTENSION` is true.
3. Datadog backend generates stats from traces. Works only if the trace
tag `_dd.compute_stats` is `1`.
A separate thing: the Go tracer sends a dummy `aws.lambda` span to the
extension, just to send metadata. The extension extracts metadata from
this span received, and sets it on the real `aws.lambda` span generated
on the extension side. The latter is what's finally sent to the trace
intake.
## Summary
If a trace has the header `Datadog-Client-Computed-Stats` with a truthy
value (which means 1 is on), then turn off 2 and 3 to avoid duplicate
counts, i.e.
1. make extension not generate stats
2. set trace tag `_dd.compute_stats` to 0
As a special case, if the dummy `aws.lambda` span sent by the Go tracer
has a `Datadog-Client-Computed-Stats` header with truthy value, then
extension propagates this value to the `aws.lambda` span generated by
the extension.
The `_dd.compute_stats` tag is determined by the combination of
`compute_trace_stats_on_extension` and `client_computed_stats`:
| Input: `compute_trace_stats_on_extension` | Input:
`client_computed_stats` | Expected: `_dd.compute_stats` | Expected:
Extension generates stats? |
|-------------------------------------------|--------------------------------|-------------------------------|--------------------------------------|
| `false` | `false` | `"1"` | No |
| `false` | `true` | `"0"` | No |
| `true` | `false` | `"0"` | Yes |
| `true` | `true` | `"0"` | No |
## Details
- `_dd.compute_stats` is now always set in `process_traces()`
(centralized from `tags_from_env()` in `tags.rs`), with the value
determined by both `compute_trace_stats_on_extension` and
`client_computed_stats` as shown in the table above
- When `client_computed_stats` is true or
`compute_trace_stats_on_extension` is true, the extension skips stats
generation in `send_processed_traces()` to avoid double-counting
- `COMPUTE_STATS_KEY` is `pub(crate)` in `tags.rs` so it can be
referenced from `trace_processor`
- Propagates `client_computed_stats` to the extension-generated
`aws.lambda` invocation span: when the Go tracer sends its placeholder
span (`dd-tracer-serverless-span`) with `Datadog-Client-Computed-Stats:
t`, that flag is stored in the invocation `Context` and passed through
to `send_spans`, so the extension also skips stats generation for the
`aws.lambda` span it creates
## Test plan
### Automated tests
Passed the new unit tests
### Manual tests
#### Steps
1. Deploy a Lambda function that uses `github.com/DataDog/dd-trace-go/v2
v2.6.0`
2. Set env var:
1. `DD_TRACE_STATS_COMPUTATION_ENABLED`: `true`, which turns on 1
2. `DD_COMPUTE_TRACE_STATS_ON_EXTENSION`: `true`, which tries to turn on
2
#### Result
Before: 2 hits (duplicate):
1. one with `resource_name=dd-tracer-serverless-span`, generated by Go
tracer
1. one with `resource_name=yiming2-xxx`
<img width="699" height="286" alt="image"
src="https://github.com/user-attachments/assets/6545cf12-c90b-4ed1-832e-1dab7b6c4d7c"
/>
After: 1 hit. Only the one with
`resource_name=dd-tracer-serverless-span`
<img width="696" height="283" alt="image"
src="https://github.com/user-attachments/assets/d2e49c3a-473c-4043-a952-9a555eada0e7"
/>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>1 parent f7cc7a0 commit 55659d9
File tree
6 files changed
+316
-24
lines changed- bottlecap/src
- lifecycle/invocation
- tags/lambda
- traces
6 files changed
+316
-24
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
46 | 56 | | |
47 | 57 | | |
48 | 58 | | |
| |||
94 | 104 | | |
95 | 105 | | |
96 | 106 | | |
| 107 | + | |
97 | 108 | | |
98 | 109 | | |
99 | 110 | | |
| |||
508 | 519 | | |
509 | 520 | | |
510 | 521 | | |
511 | | - | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
512 | 528 | | |
513 | 529 | | |
514 | 530 | | |
| |||
528 | 544 | | |
529 | 545 | | |
530 | 546 | | |
| 547 | + | |
531 | 548 | | |
532 | 549 | | |
533 | 550 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
610 | 610 | | |
611 | 611 | | |
612 | 612 | | |
| 613 | + | |
613 | 614 | | |
614 | | - | |
615 | | - | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
616 | 623 | | |
617 | 624 | | |
618 | 625 | | |
| |||
677 | 684 | | |
678 | 685 | | |
679 | 686 | | |
680 | | - | |
| 687 | + | |
681 | 688 | | |
682 | 689 | | |
683 | 690 | | |
| |||
689 | 696 | | |
690 | 697 | | |
691 | 698 | | |
| 699 | + | |
692 | 700 | | |
693 | 701 | | |
694 | 702 | | |
| |||
701 | 709 | | |
702 | 710 | | |
703 | 711 | | |
704 | | - | |
| 712 | + | |
705 | 713 | | |
706 | 714 | | |
707 | 715 | | |
| |||
1337 | 1345 | | |
1338 | 1346 | | |
1339 | 1347 | | |
1340 | | - | |
| 1348 | + | |
1341 | 1349 | | |
1342 | | - | |
| 1350 | + | |
| 1351 | + | |
1343 | 1352 | | |
1344 | 1353 | | |
1345 | 1354 | | |
| |||
2110 | 2119 | | |
2111 | 2120 | | |
2112 | 2121 | | |
| 2122 | + | |
| 2123 | + | |
| 2124 | + | |
| 2125 | + | |
| 2126 | + | |
| 2127 | + | |
| 2128 | + | |
| 2129 | + | |
| 2130 | + | |
| 2131 | + | |
| 2132 | + | |
| 2133 | + | |
| 2134 | + | |
| 2135 | + | |
| 2136 | + | |
| 2137 | + | |
| 2138 | + | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
| 2164 | + | |
| 2165 | + | |
| 2166 | + | |
| 2167 | + | |
| 2168 | + | |
| 2169 | + | |
| 2170 | + | |
| 2171 | + | |
| 2172 | + | |
| 2173 | + | |
| 2174 | + | |
| 2175 | + | |
| 2176 | + | |
| 2177 | + | |
| 2178 | + | |
| 2179 | + | |
| 2180 | + | |
| 2181 | + | |
| 2182 | + | |
| 2183 | + | |
| 2184 | + | |
| 2185 | + | |
| 2186 | + | |
| 2187 | + | |
| 2188 | + | |
| 2189 | + | |
| 2190 | + | |
| 2191 | + | |
| 2192 | + | |
| 2193 | + | |
| 2194 | + | |
| 2195 | + | |
| 2196 | + | |
| 2197 | + | |
| 2198 | + | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
| 2202 | + | |
| 2203 | + | |
| 2204 | + | |
| 2205 | + | |
| 2206 | + | |
| 2207 | + | |
| 2208 | + | |
| 2209 | + | |
| 2210 | + | |
| 2211 | + | |
2113 | 2212 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
| |||
372 | 373 | | |
373 | 374 | | |
374 | 375 | | |
| 376 | + | |
375 | 377 | | |
376 | 378 | | |
377 | 379 | | |
378 | 380 | | |
| 381 | + | |
379 | 382 | | |
380 | 383 | | |
381 | 384 | | |
| |||
585 | 588 | | |
586 | 589 | | |
587 | 590 | | |
588 | | - | |
589 | | - | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
590 | 596 | | |
591 | 597 | | |
592 | 598 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | 130 | | |
137 | 131 | | |
138 | 132 | | |
| |||
292 | 286 | | |
293 | 287 | | |
294 | 288 | | |
295 | | - | |
296 | | - | |
| 289 | + | |
297 | 290 | | |
298 | 291 | | |
299 | 292 | | |
| |||
428 | 421 | | |
429 | 422 | | |
430 | 423 | | |
431 | | - | |
| 424 | + | |
432 | 425 | | |
433 | 426 | | |
434 | 427 | | |
| |||
470 | 463 | | |
471 | 464 | | |
472 | 465 | | |
473 | | - | |
| 466 | + | |
474 | 467 | | |
475 | 468 | | |
476 | 469 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
501 | 501 | | |
502 | 502 | | |
503 | 503 | | |
504 | | - | |
| 504 | + | |
| 505 | + | |
505 | 506 | | |
506 | 507 | | |
507 | 508 | | |
| |||
531 | 532 | | |
532 | 533 | | |
533 | 534 | | |
534 | | - | |
535 | 535 | | |
536 | 536 | | |
537 | 537 | | |
| |||
586 | 586 | | |
587 | 587 | | |
588 | 588 | | |
589 | | - | |
| 589 | + | |
590 | 590 | | |
591 | 591 | | |
592 | 592 | | |
| |||
0 commit comments