[OTLP] Add trace metrics via OTLP span metrics export#8826
Conversation
Execution-Time Benchmarks Report ⏱️Execution-time results for samples comparing This PR (8826) and master. ✅ No regressions detected - check the details below Full Metrics ComparisonFakeDbCommand
HttpMessageHandler
Comparison explanationExecution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:
Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard. Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph). Duration chartsFakeDbCommand (.NET Framework 4.8)gantt
title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (73ms) : 69, 77
master - mean (73ms) : 69, 77
section Bailout
This PR (8826) - mean (76ms) : 73, 79
master - mean (76ms) : 72, 81
section CallTarget+Inlining+NGEN
This PR (8826) - mean (1,085ms) : 1033, 1137
master - mean (1,084ms) : 1036, 1132
FakeDbCommand (.NET Core 3.1)gantt
title Execution time (ms) FakeDbCommand (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (113ms) : 105, 120
master - mean (113ms) : 108, 118
section Bailout
This PR (8826) - mean (111ms) : 108, 113
master - mean (111ms) : 108, 113
section CallTarget+Inlining+NGEN
This PR (8826) - mean (782ms) : 754, 809
master - mean (777ms) : 752, 801
FakeDbCommand (.NET 6)gantt
title Execution time (ms) FakeDbCommand (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (97ms) : 94, 100
master - mean (96ms) : 94, 99
section Bailout
This PR (8826) - mean (98ms) : 95, 100
master - mean (100ms) : 95, 105
section CallTarget+Inlining+NGEN
This PR (8826) - mean (942ms) : 899, 984
master - mean (939ms) : 895, 982
FakeDbCommand (.NET 8)gantt
title Execution time (ms) FakeDbCommand (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (99ms) : 94, 104
master - mean (96ms) : 90, 102
section Bailout
This PR (8826) - mean (97ms) : 93, 100
master - mean (95ms) : 94, 97
section CallTarget+Inlining+NGEN
This PR (8826) - mean (814ms) : 773, 855
master - mean (813ms) : 779, 848
HttpMessageHandler (.NET Framework 4.8)gantt
title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (204ms) : 199, 210
master - mean (205ms) : 200, 211
section Bailout
This PR (8826) - mean (207ms) : 204, 210
master - mean (207ms) : 201, 213
section CallTarget+Inlining+NGEN
This PR (8826) - mean (1,213ms) : 1175, 1251
master - mean (1,215ms) : 1172, 1259
HttpMessageHandler (.NET Core 3.1)gantt
title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (292ms) : 283, 301
master - mean (291ms) : 286, 297
section Bailout
This PR (8826) - mean (292ms) : 289, 296
master - mean (294ms) : 289, 299
section CallTarget+Inlining+NGEN
This PR (8826) - mean (977ms) : 947, 1008
master - mean (974ms) : 954, 995
HttpMessageHandler (.NET 6)gantt
title Execution time (ms) HttpMessageHandler (.NET 6)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (282ms) : 276, 288
master - mean (285ms) : 280, 290
section Bailout
This PR (8826) - mean (280ms) : 275, 286
master - mean (284ms) : 280, 288
section CallTarget+Inlining+NGEN
This PR (8826) - mean (1,176ms) : 1139, 1212
master - mean (1,176ms) : 1138, 1215
HttpMessageHandler (.NET 8)gantt
title Execution time (ms) HttpMessageHandler (.NET 8)
dateFormat x
axisFormat %Q
todayMarker off
section Baseline
This PR (8826) - mean (282ms) : 276, 289
master - mean (287ms) : 281, 293
section Bailout
This PR (8826) - mean (282ms) : 275, 290
master - mean (286ms) : 277, 294
section CallTarget+Inlining+NGEN
This PR (8826) - mean (1,047ms) : 999, 1095
master - mean (1,050ms) : 1012, 1088
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
BenchmarksBenchmark execution time: 2026-07-02 17:33:51 Comparing candidate commit f93b8f5 in PR branch Found 0 performance improvements and 1 performance regressions! Performance is the same for 71 metrics, 0 unstable metrics, 60 known flaky benchmarks, 66 flaky benchmarks without significant changes.
|
0876bf1 to
5f901eb
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 00915b660f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
00915b6 to
74cd1de
Compare
Add OTEL_TRACES_SPAN_METRICS_ENABLED (tri-state gate) and DD_TRACE_OTEL_SEMANTICS_ENABLED config keys. Extend StatsBucket with exact MinDuration/MaxDuration for OTLP histogram min/max fields (DDSketch approximation is insufficient). Wire TracerManagerFactory to create an OTLP-backed StatsAggregator when span metrics are enabled. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…duration) Introduces OtlpSpanStatsSerializer with SerializeJson: converts a StatsBuffer into an OTLP ExportMetricsServiceRequest (proto3 JSON). Projects DDSketch bins onto 16 fixed explicit bounds (seconds). Uint64 fields (timestamps, counts) are encoded as quoted strings per the proto3 JSON spec. Attribute set per data point: span.name, span.kind, http.request.method, http.response.status_code, http.route, rpc.response.status_code, status.code (errors only), service.name (when non-default). Datadog-specific attributes (datadog.operation.name, datadog.span.type, etc.) suppressed when DD_TRACE_OTEL_SEMANTICS_ENABLED. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Extends OtlpSpanStatsSerializer with a binary-protobuf path (Serialize): hand-rolls varint/fixed64/length-delimited encoding against the OTLP proto schema — no proto codegen dependency. Adds attribute helpers (WriteAttribute, WriteIntAttribute, WriteBoolAttribute) and FieldNumbers/AnyValueFieldNumbers constants. Wires ApiOtlp.SendStatsAsyncImpl to serialize and POST to the OTLP metrics endpoint. Protocol selected by OTEL_EXPORTER_OTLP_METRICS_PROTOCOL (http/protobuf default, http/json for JSON). Custom headers from OTEL_EXPORTER_OTLP_METRICS_HEADERS applied per request. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Mirror the Datadog-Client-Computed-Stats header (AgentWriter.CanComputeStats) on the OTLP trace path so a downstream Datadog Agent does not recompute stats. Co-authored-by: Cursor <cursoragent@cursor.com>
…ve stale comments Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add #nullable enable (CS8632) - Move const before readonly field (SA1203) - Move private CreateBuffer/CreateKey methods after public [Fact] methods (SA1202) - Put each StatsAggregationKey constructor arg on its own line (SA1117) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add OTEL_TRACES_SPAN_METRICS_ENABLED and DD_TRACE_OTEL_SEMANTICS_ENABLED to config_norm_rules.json so AllConfigurationValuesAreRegisteredWithIntake passes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
optional uint64 count and repeated uint64 bucket_counts are varint fields (wire type 0) in the OTLP HistogramDataPoint proto — not fixed64 (wire type 1). Using WireTypeFixed64 produced tag bytes 33/49 instead of 32/48, causing conforming decoders to treat them as unknown fields and drop them. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Change 1: rpc.response.status_code emitted as canonical string name
- Add GrpcStatusNames[0..16] lookup table
- Add NormalizeGrpcStatusName: handles numeric codes, named codes,
StatusCode.* prefix, CANCELED alias, NOTFOUND alias
- Omit attribute when empty/unparseable/out-of-range
Change 2: rpc.method explicitly not emitted (remove TODO comment)
- gRPC method is already the span resource/span.name; separate dimension
would inflate cardinality for no benefit
Change 3: confirm true top-level counts (already implemented)
- OTLP aggregation key already uses span.IsTopLevel, so top-level and
non-top-level spans with identical other attributes bucket separately
- Remove misleading "derived as all-spans-top-level" comment
Add tests covering all three changes.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
a183e87 to
bb159a5
Compare
…ndant comments Decorative ── section headers were unique to OtlpSpanStatsSerializer — not used elsewhere in the codebase. Multi-line XML docstrings on public methods and the EncodeKeyValue helper explain what the code does, which is already clear from names and signatures. Per-attribute comments in SerializeDataPoint duplicated the string literals they sat above. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The metrics request factory was previously built from OtlpTransportStrategy.GetTraces, which uses traces transport config (endpoint, protocol, headers, timeout). Add GetMetrics() so the metrics factory respects OTEL_EXPORTER_OTLP_METRICS_* settings, and stop manually adding metrics headers per-request since they're now DefaultHeaders on the HttpClient. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ctory Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
StatsBucket, StatsBuffer, and StatsAggregationKey constructors gained new required parameters in master; update test call sites to match. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
CanComputeStats controls P0 dropping and must be true in OTLP mode so unsampled traces are not forwarded. IsClientComputingStats separately controls whether _dd.stats_computed=true is emitted on OTLP traces, and is only true when span metrics are actually exported. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
| // sampling decision override takes precedence over TraceContext.SamplingPriority | ||
| SamplingPriority = samplingPriority; | ||
| IsFirstChunkInPayload = isFirstChunkInPayload; | ||
| ClientComputedStats = clientComputedStats; |
There was a problem hiding this comment.
You should calculate ClientComputedStats from the tracer settings, like how IsApmEnabled is calculated. As a result, you can remove the bool clientComputedStats parameter from being passed into this file, AgentWriter.cs, and SpanBuffer.cs. This should also let you eliminate AgentWriter.ClientComputedStats
There was a problem hiding this comment.
This should also allow you to delete the property IStatsAggregator.IsClientComputingStats
| } | ||
|
|
||
| private async Task<bool> SendWithRetry<T>(Uri endpoint, SendCallback<T> callback, T state, int retryLimit = 5) | ||
| private async Task<bool> SendWithRetry<T>(Uri endpoint, SendCallback<T> callback, T state, int retryLimit = 5, IApiRequestFactory requestFactory = null) |
There was a problem hiding this comment.
Let's make the IApiRequestFactory requestFactory field mandatory so we don't create diverging logic between the OTLP Traces and OTLP Span Metrics.
| private async Task<bool> SendWithRetry<T>(Uri endpoint, SendCallback<T> callback, T state, int retryLimit = 5, IApiRequestFactory requestFactory = null) | |
| private async Task<bool> SendWithRetry<T>(Uri endpoint, IApiRequestFactory requestFactory, SendCallback<T> callback, T state, int retryLimit = 5) |
| WriteVarInt(writer, resourceData.Length); | ||
| writer.Write(resourceData); | ||
|
|
||
| // Single scope-metrics entry (no InstrumentationScope — omitted per spec) |
There was a problem hiding this comment.
Is the InstrumentationScope omitted omitted in our own spec or in the Span Metrics Connector? We should give it a similar shape to the SMC I would imagine
| return stream.ToArray(); | ||
| } | ||
|
|
||
| private static byte[] SerializeResource(StatsBuffer buffer, bool otelSemanticsEnabled) |
There was a problem hiding this comment.
The resource we emit here should match the resource emitted by the existing OtlpMetricsSerializer (it currently only emits protobuf). Can you please ensure their resource header is produced identically?
| writer.Write(endTimeUnixNano); | ||
|
|
||
| var count = (ulong)bucket.Hits; | ||
| WriteTag(writer, FieldNumbers.HistogramDataPointCount, WireTypeVarInt); |
RFC: https://docs.google.com/document/d/1cDB_NVkyY_DXzb_EfQS6BsGu5Rx3gnf-tbpgJCYbIkc/edit?pli=1&tab=t.0
Overview
Adds the ability to export existing client-side computed stats via OTLP. No changes to cardinality, aggregation keys, or the values being exported — the same
StatsBuffer/DDSketchdata the tracer already computes is serialized as atraces.span.sdk.metrics.durationOTLP histogram (units, DELTA temporality) and POSTed to an OTLP metrics endpoint. This lets downstream collectors consume span metrics using OTel semantics without recomputing them from raw spans.The PR is split into four commits — reviewing commit-by-commit is recommended.
Reviewer guide
Commit 1 — config keys (
OTEL_TRACES_SPAN_METRICS_ENABLED,DD_TRACE_OTEL_SEMANTICS_ENABLED),StatsBucketmin/max fields,TracerManagerFactorywiring. No serialization logic.Commit 2 —
OtlpSpanStatsSerializer.SerializeJson: projects DDSketch bins onto 16 fixed bounds viaPositiveValueStore.EnumerateAscending(); exact min/max fromStatsBucketsince sketch values are approximate.Commit 3 — same projection, hand-rolled protobuf encoding (no codegen).
ApiOtlp.SendStatsAsyncImpldispatches JSON vs protobuf viaOTEL_EXPORTER_OTLP_METRICS_PROTOCOL.Commit 4 — emits
_dd.stats_computedOTLP resource attribute to prevent downstream Agent from recomputing stats (mirrorsDatadog-Client-Computed-Statsheader). Adds unit tests forOtlpSpanStatsSerializer.Test coverage
System tests: DataDog/system-tests#6834
Notes
rpc.methodattribute omitted (high cardinality, follow-up)