Skip to content

feat(tracing): add span link support#330

Open
MilanGarnier wants to merge 27 commits into
mainfrom
milan.garnier/span-links-support
Open

feat(tracing): add span link support#330
MilanGarnier wants to merge 27 commits into
mainfrom
milan.garnier/span-links-support

Conversation

@MilanGarnier

@MilanGarnier MilanGarnier commented Jun 30, 2026

Copy link
Copy Markdown

Description

Adds OpenTelemetry-style span links to dd-trace-cpp.

  • ExtractedContext struct (include/datadog/extracted_context.h)
  • SpanLink struct (include/datadog/span_link.h): 128-bit trace_id, span_id, optional tracestate, string attributes, optional flags.
  • msgpack serialization (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_high only when non-zero, span_id, attributes only when non-empty, tracestate only when non-empty, flags with high bit set when present).
  • SpanData::span_links (src/datadog/span_data.h/.cpp): std::vector<SpanLink> field; the span encoder emits a span_links array only when non-empty (matches omitempty behaviour 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.
  • Parametric endpoint 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_EXTRACT config (planned).

Additional Notes

@datadog-datadog-prod-us1-2

This comment has been minimized.

@pr-commenter

pr-commenter Bot commented Jun 30, 2026

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2026-07-15 09:33:18

Comparing candidate commit 58fcb18 in PR branch milan.garnier/span-links-support with baseline commit d7ea3a8 in branch main.

Found 1 performance improvements and 1 performance regressions! Performance is the same for 6 metrics, 0 unstable metrics.

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

scenario:BM_TraceID_ParseHex/128bit

  • 🟩 execution_time [-2.711ns; -2.672ns] or [-2.157%; -2.126%]

scenario:BM_TraceTinyCCSource

  • 🟥 execution_time [+3.253ms; +3.414ms] or [+4.355%; +4.570%]

@MilanGarnier MilanGarnier changed the title feat(tracing): add support for span links feat(tracing): add span link support Jun 30, 2026

@xlamorlette-datadog xlamorlette-datadog left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only a first very light and superficial review, I should review in detail in a few days.

Comment thread include/datadog/span_link.h Outdated
Comment thread src/datadog/span.cpp Outdated
Comment thread src/datadog/span_data.cpp Outdated
Comment thread src/datadog/span_data.cpp Outdated
Comment thread src/datadog/span_link.cpp
Comment thread test/system-tests/request_handler.cpp
Comment thread test/system-tests/request_handler.cpp
Comment thread include/datadog/span.h Outdated
Comment thread src/datadog/span.cpp Outdated

// 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_)) {

@zacharycmontoya zacharycmontoya Jul 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/datadog/span_data.cpp
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@MilanGarnier MilanGarnier Jul 15, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"`

Comment thread src/datadog/span_link.cpp Outdated
Comment thread include/datadog/span_link.h Outdated
namespace datadog::tracing {

// Convenience alias: the map type used for span link user attributes.
using SpanLinkAttributes = std::unordered_map<std::string, std::string>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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()) {

@zacharycmontoya zacharycmontoya Jul 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread test/test_span_link.cpp
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants