-
Notifications
You must be signed in to change notification settings - Fork 19
feat(tracing): add span link support #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fad515b
0796973
0fecaca
f254562
58cf95d
84a7ef4
65382c4
ecfb7cc
eecfe93
c4ae457
86d189e
dff6677
1a36ade
6c2f121
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #pragma once | ||
|
|
||
| // Tracing context extracted from incoming distributed headers. Unlike `Span`, | ||
| // this type holds only the propagation fields of the upstream context and does | ||
| // not represent a locally-owned span. | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
|
|
||
| #include "optional.h" | ||
| #include "trace_id.h" | ||
|
|
||
| namespace datadog { | ||
| namespace tracing { | ||
|
|
||
| struct ExtractedContext { | ||
| TraceID trace_id; | ||
| // The upstream span's ID (e.g. x-datadog-parent-id or traceparent | ||
| // parent-id field). | ||
| std::uint64_t span_id = 0; | ||
| // W3C tracestate, if present in the incoming headers. | ||
| Optional<std::string> tracestate; | ||
| // W3C trace flags byte, if present in the incoming traceparent header. | ||
| Optional<std::uint32_t> flags; | ||
| // Sampling priority extracted from the incoming headers. | ||
| Optional<int> sampling_priority; | ||
| }; | ||
|
|
||
| } // namespace tracing | ||
| } // namespace datadog |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #pragma once | ||
|
|
||
| // This component defines `SpanLink`, a causal association between the span that | ||
| // owns the link and another span (possibly in another trace). Span links carry | ||
| // the linked span's identifiers plus optional W3C tracestate, trace flags, and | ||
| // arbitrary string attributes. They are serialized into the owning span under | ||
| // the `span_links` key in a format shared by all Datadog tracers. | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
| #include <unordered_map> | ||
|
|
||
| #include "expected.h" | ||
| #include "optional.h" | ||
| #include "trace_id.h" | ||
|
|
||
| namespace datadog { | ||
| namespace tracing { | ||
|
|
||
| // Convenience alias: the map type used for span link user attributes. | ||
| using SpanLinkAttributes = std::unordered_map<std::string, std::string>; | ||
|
|
||
| struct SpanLink { | ||
| // 128-bit trace ID of the linked span. | ||
| TraceID trace_id; | ||
| // The linked span's ID. | ||
| std::uint64_t span_id = 0; | ||
| // W3C `tracestate` header value from the linked context, if any. | ||
| Optional<std::string> tracestate; | ||
| // Additional string attributes associated with the link. | ||
| std::unordered_map<std::string, std::string> attributes; | ||
| // W3C trace flags from the linked context, if any. | ||
| Optional<std::uint32_t> flags; | ||
| }; | ||
|
|
||
| // Append to the specified `destination` the MessagePack representation of the | ||
| // specified `link`. | ||
| Expected<void> msgpack_encode(std::string& destination, const SpanLink& link); | ||
|
|
||
| } // namespace tracing | ||
| } // namespace datadog |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,7 +75,13 @@ void SpanData::apply_config(const SpanDefaults& defaults, | |
|
|
||
| Expected<void> msgpack_encode(std::string& destination, const SpanData& span) { | ||
| // clang-format off | ||
| msgpack::pack_map( | ||
| const bool has_links = !span.span_links.empty(); | ||
|
|
||
| // 12 always-present fields, plus span_links when there are any. | ||
| auto result = msgpack::pack_map(destination, has_links ? 13u : 12u); | ||
| if (!result) return result; | ||
|
|
||
| result = msgpack::pack_map_suffix( | ||
| destination, | ||
| "service", [&](auto& destination) { | ||
| return msgpack::pack_string(destination, span.service); | ||
|
|
@@ -131,6 +137,18 @@ Expected<void> msgpack_encode(std::string& destination, const SpanData& span) { | |
| }, "type", [&](auto& destination) { | ||
| return msgpack::pack_string(destination, span.service_type); | ||
| }); | ||
| if (!result) return result; | ||
|
|
||
| if (has_links) { | ||
| result = msgpack::pack_string(destination, "span_links"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The library still sends traces to the Agent on Useful? React with 👍 / 👎. |
||
| if (!result) return result; | ||
| result = msgpack::pack_array( | ||
| destination, span.span_links, | ||
| [](std::string& destination, const SpanLink& link) { | ||
| return msgpack_encode(destination, link); | ||
| }); | ||
| if (!result) return result; | ||
| } | ||
| // clang-format on | ||
|
|
||
| return nullopt; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #include <datadog/span_link.h> | ||
|
|
||
| #include <cstddef> | ||
| #include <string> | ||
|
|
||
| #include "msgpack.h" | ||
|
|
||
| namespace datadog { | ||
| namespace tracing { | ||
|
|
||
| Expected<void> msgpack_encode(std::string& destination, const SpanLink& link) { | ||
| const bool has_trace_id_high = link.trace_id.high != 0; | ||
| const bool has_attributes = !link.attributes.empty(); | ||
| const bool has_tracestate = link.tracestate && !link.tracestate->empty(); | ||
| const bool has_flags = link.flags.has_value(); | ||
|
|
||
| std::size_t size = 2; // trace_id + span_id are always present | ||
| if (has_trace_id_high) ++size; | ||
| if (has_attributes) ++size; | ||
| if (has_tracestate) ++size; | ||
| if (has_flags) ++size; | ||
|
|
||
| auto result = msgpack::pack_map(destination, size); | ||
| if (!result) return result; | ||
|
|
||
| // trace_id (low 64 bits) | ||
| result = msgpack::pack_string(destination, "trace_id"); | ||
| if (!result) return result; | ||
| msgpack::pack_integer(destination, link.trace_id.low); | ||
|
|
||
| if (has_trace_id_high) { | ||
| result = msgpack::pack_string(destination, "trace_id_high"); | ||
| if (!result) return result; | ||
| msgpack::pack_integer(destination, link.trace_id.high); | ||
| } | ||
|
|
||
| result = msgpack::pack_string(destination, "span_id"); | ||
| if (!result) return result; | ||
| msgpack::pack_integer(destination, link.span_id); | ||
|
|
||
| if (has_attributes) { | ||
| result = msgpack::pack_string(destination, "attributes"); | ||
| if (!result) return result; | ||
| result = | ||
| msgpack::pack_map(destination, link.attributes, | ||
| [](std::string& destination, const auto& value) { | ||
| return msgpack::pack_string(destination, value); | ||
| }); | ||
| if (!result) return result; | ||
| } | ||
|
|
||
| if (has_tracestate) { | ||
| result = msgpack::pack_string(destination, "tracestate"); | ||
| if (!result) return result; | ||
| result = msgpack::pack_string(destination, *link.tracestate); | ||
| if (!result) return result; | ||
| } | ||
|
|
||
| if (has_flags) { | ||
| result = msgpack::pack_string(destination, "flags"); | ||
| if (!result) return result; | ||
| // The high bit marks "flags is present" so a receiver can distinguish an | ||
| // explicit value of 0 from an omitted field. | ||
| msgpack::pack_integer(destination, std::uint64_t(*link.flags | (1u << 31))); | ||
| } | ||
|
|
||
| return nullopt; | ||
| } | ||
|
|
||
| } // namespace tracing | ||
| } // namespace datadog |
Uh oh!
There was an error while loading. Please reload this page.