feat(tracing): add span link support#330
Conversation
This comment has been minimized.
This comment has been minimized.
BenchmarksBenchmark execution time: 2026-07-15 09:33:18 Comparing candidate commit 58fcb18 in PR branch Found 1 performance improvements and 1 performance regressions! Performance is the same for 6 metrics, 0 unstable metrics.
|
cbb3ed8 to
86d189e
Compare
003399b to
28432fe
Compare
xlamorlette-datadog
left a comment
There was a problem hiding this comment.
This is only a first very light and superficial review, I should review in detail in a few days.
|
|
||
| // Populate tracestate/flags from the linked trace's sampling decision, if | ||
| // one has already been made. Do not force a decision here | ||
| if (auto context = linked.trace_segment().w3c_link_context(*linked.data_)) { |
There was a problem hiding this comment.
If you end up taking my suggestion to pass in a context, then we can just copy over the tracestate and trace flags from the input SpanContext. There will be no need to get information from a trace segment. As a result, we could remove the w3c_link_context call you added to trace segment
| pack_resource, "trace_id", pack_trace_id, "span_id", pack_span_id, | ||
| "parent_id", pack_parent_id, "start", pack_start, "duration", | ||
| pack_duration, "error", pack_error, "meta", pack_meta, "metrics", | ||
| pack_metrics, "type", pack_type, "span_links", pack_span_links); |
There was a problem hiding this comment.
Is there a reason for splitting the behavior when there are no span links? I imagine it would be okay to run this even if we have an empty array of span links
There was a problem hiding this comment.
In other tracers the field is omitted when empty, I was following the same logic. I'm also not satisfied with how it looks that way, the other option I had was using a magic number for the number of fields and several msg_pack_suffix to avoid splitting the logic.
e.g. Go uses :
spanLinks []SpanLink `msg:"span_links,omitempty"`| namespace datadog::tracing { | ||
|
|
||
| // Convenience alias: the map type used for span link user attributes. | ||
| using SpanLinkAttributes = std::unordered_map<std::string, std::string>; |
There was a problem hiding this comment.
@xlamorlette-datadog I want the add_link API to be able to accept a map where the attribute values could be one of many types (see https://github.com/open-telemetry/opentelemetry-cpp/blob/4e20f690352ce6c2793efcb6448e51363a2cad18/api/include/opentelemetry/common/attribute_value.h). Is there any easy way to for us to build that forward compatability right now, or should we just tackle that when we get to the point that we can record attributes in general to have a variety of primitive types (rather than strings)?
| // The linked context is either another active span or a previously extracted | ||
| // propagation context, both keyed by `parent_id`. | ||
| auto linked_it = active_spans_.find(*parent_id); | ||
| if (linked_it != active_spans_.cend()) { |
There was a problem hiding this comment.
Going down the path of mirroring the OpenTelemetry C++ API, we could implement a GetContext() public API on the Span class that would return a SpanContext object with the pertinent details of that Span, rather than having to do context injection to surface the information
| TEST_SPAN_LINK("minimal link encodes only trace_id and span_id") { | ||
| SpanLink link; | ||
| link.trace_id = TraceID(0x1122334455667788ULL); // high == 0 | ||
| link.trace_id = TraceID(0x1122334455667788ULL, 0xBBBBBBBBBBBBBBBBULL); |
There was a problem hiding this comment.
I actually preferred leaving the high-bits as 0 to ensure that we always emit the trace_id_high. Can you add a case where we test the high-bits being 0?
Description
Adds OpenTelemetry-style span links to dd-trace-cpp.
ExtractedContext struct(include/datadog/extracted_context.h)SpanLinkstruct (include/datadog/span_link.h): 128-bittrace_id,span_id, optionaltracestate, stringattributes, optionalflags.src/datadog/span_link.cpp): per-link map with the exact field names and omission rules used by dd-trace-go / dd-trace-py / dd-trace-rs (trace_id,trace_id_highonly when non-zero,span_id,attributesonly when non-empty,tracestateonly when non-empty,flagswith high bit set when present).SpanData::span_links(src/datadog/span_data.h/.cpp):std::vector<SpanLink>field; the span encoder emits aspan_linksarray only when non-empty (matchesomitemptybehaviour of other tracers).Span::add_link(const SpanLink&)(include/datadog/span.h,src/datadog/span.cpp): public API to attach a link to a live span.POST /trace/span/add_link(test/system-tests/): wires up the cross-language system-test client contract.Motivation
This feature is supported by all other tracers and is a dependency for future implementations, such as
DD_TRACE_PROPAGATION_BEHAVIOR_EXTRACTconfig (planned).Additional Notes
test/test_span_link.cppandtest/test_span.cppexercise serialization and the public API end-to-end.Jira ticket: APMAPI-1941