Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ cc_library(
"src/datadog/span.cpp",
"src/datadog/span_data.cpp",
"src/datadog/span_data.h",
"src/datadog/span_link.cpp",
"src/datadog/span_matcher.cpp",
"src/datadog/span_sampler.cpp",
"src/datadog/span_sampler.h",
Expand Down Expand Up @@ -132,6 +133,7 @@ cc_library(
"include/datadog/span.h",
"include/datadog/span_config.h",
"include/datadog/span_defaults.h",
"include/datadog/span_link.h",
"include/datadog/span_matcher.h",
"include/datadog/span_sampler_config.h",
"include/datadog/string_view.h",
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ target_sources(dd-trace-cpp-objects
include/datadog/span.h
include/datadog/span_config.h
include/datadog/span_defaults.h
include/datadog/span_link.h
include/datadog/span_matcher.h
include/datadog/span_sampler_config.h
include/datadog/string_view.h
Expand Down Expand Up @@ -203,6 +204,7 @@ target_sources(dd-trace-cpp-objects
src/datadog/runtime_id.cpp
src/datadog/span.cpp
src/datadog/span_data.cpp
src/datadog/span_link.cpp
src/datadog/span_matcher.cpp
src/datadog/span_sampler_config.cpp
src/datadog/span_sampler.cpp
Expand Down
8 changes: 8 additions & 0 deletions include/datadog/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

#include "clock.h"
#include "optional.h"
#include "span_link.h"
Comment thread
MilanGarnier marked this conversation as resolved.
#include "string_view.h"
#include "trace_id.h"
#include "trace_source.h"
Expand Down Expand Up @@ -166,6 +167,13 @@ class Span {
// Specifies the product (AppSec, DBM) that created this span.
void set_source(Source);

// Add a span link to this span, filled from the specified live span.
// The linked span's trace ID, span ID, tracestate, and trace flags are
// populated automatically. `attrs` is optional user-supplied attributes.
void add_link(const Span& linked, const SpanLinkAttributes& attrs = {});
// Add a pre-built span link to this span.
void add_link(const SpanLink& link);

// Write information about this span and its trace into the specified `writer`
// using all of the configured injection propagation styles.
void inject(DictWriter& writer) const;
Expand Down
41 changes: 41 additions & 0 deletions include/datadog/span_link.h
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
35 changes: 35 additions & 0 deletions src/datadog/span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <datadog/optional.h>
#include <datadog/span.h>
#include <datadog/span_config.h>
#include <datadog/span_link.h>
#include <datadog/string_view.h>
#include <datadog/trace_segment.h>

Expand Down Expand Up @@ -164,6 +165,40 @@ void Span::set_source(Source source) {
to_tag(source));
}

void Span::add_link(const Span& linked, const SpanLinkAttributes& attrs) {
SpanLink link;
link.trace_id = linked.trace_id();
link.span_id = linked.id();
link.attributes = attrs;

// Capture injected headers (tracestate, traceparent flags) into a map.
struct : DictWriter {
std::unordered_map<std::string, std::string> map;
void set(StringView k, StringView v) override {
map[std::string(k)] = std::string(v);
}
} w;
linked.inject(w);
if (auto it = w.map.find("tracestate"); it != w.map.end()) {
link.tracestate = it->second;
}
if (auto it = w.map.find("traceparent"); it != w.map.end()) {
const auto& tp = it->second;
const auto pos = tp.rfind('-');
if (pos != std::string::npos && pos + 1 < tp.size()) {
try {
link.flags = static_cast<std::uint32_t>(
std::stoul(tp.substr(pos + 1), nullptr, 16));
} catch (...) {
}
}
}

data_->span_links.push_back(std::move(link));
}

void Span::add_link(const SpanLink& link) { data_->span_links.push_back(link); }

TraceSegment& Span::trace_segment() { return *trace_segment_; }

const TraceSegment& Span::trace_segment() const { return *trace_segment_; }
Expand Down
20 changes: 19 additions & 1 deletion src/datadog/span_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Comment thread
MilanGarnier marked this conversation as resolved.
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;
Expand Down
2 changes: 2 additions & 0 deletions src/datadog/span_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <datadog/clock.h>
#include <datadog/expected.h>
#include <datadog/optional.h>
#include <datadog/span_link.h>
#include <datadog/string_view.h>
#include <datadog/trace_id.h>

Expand Down Expand Up @@ -33,6 +34,7 @@ struct SpanData {
bool error = false;
std::unordered_map<std::string, std::string> tags;
std::unordered_map<std::string, double> numeric_tags;
std::vector<SpanLink> span_links;

Optional<StringView> environment() const;
Optional<StringView> version() const;
Expand Down
71 changes: 71 additions & 0 deletions src/datadog/span_link.cpp
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
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_executable(tests
test_parse_util.cpp
test_smoke.cpp
test_span.cpp
test_span_link.cpp
test_span_sampler.cpp
test_trace_id.cpp
test_trace_segment.cpp
Expand Down
4 changes: 4 additions & 0 deletions test/system-tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ int main(int argc, char* argv[]) {
[&handler](const httplib::Request& req, httplib::Response& res) {
handler.on_manual_keep(req, res);
});
svr.Post("/trace/span/add_link",
[&handler](const httplib::Request& req, httplib::Response& res) {
handler.on_add_link(req, res);
});

// Not implemented
svr.Post("/trace/span/set_metric",
Expand Down
Loading
Loading