C++ Custom Instrumentation using Datadog API

If you have not yet read the setup instructions, start with the C++ Setup Instructions.

Creating spans

To manually instrument a method:

{
  // Create a root span for the current request.
  auto root_span = tracer.create_span();
  root_span.set_name("get_ingredients");
  // Set a resource name for the root span.
  root_span.set_resource_name("bologna_sandwich");
  // Create a child span with the root span as its parent.
  auto child_span = root_span.create_child();
  child_span.set_name("cache_lookup");
  // Set a resource name for the child span.
  child_span.set_resource_name("ingredients.bologna_sandwich");
  // Spans can be finished at an explicit time ...
  child_span.set_end_time(std::chrono::steady_clock::now());
} // ... or implicitly when the destructor is invoked.
  // For example, root_span finishes here.

Adding tags

Add custom span tags to your spans to customize your observability within Datadog. Span tags are applied to your incoming traces, allowing you to correlate observed behavior with code-level information such as merchant tier, checkout amount, or user ID.

Note that some Datadog tags are necessary for unified service tagging.

Manually

Add tags directly to a span object by calling Span::set_tag. For example:

// Add tags directly to a span by calling `Span::set_tag`
auto span = tracer.create_span();
span.set_tag("key must be string", "value must also be a string");

// Or, add tags by setting a `SpanConfig`
datadog::tracing::SpanConfig opts;
opts.tags.emplace("team", "apm-proxy");
auto span2 = tracer.create_span(opts);

Environment variable

To set tags across all your spans, set the DD_TAGS environment variable as a list of key:value pairs separated by commas.

export DD_TAGS=team:apm-proxy,key:value

Manually

datadog::tracing::TracerConfig tracer_config;
tracer_config.tags = {
  {"team", "apm-proxy"},
  {"apply", "on all spans"}
};

const auto validated_config = datadog::tracing::finalize_config(tracer_config);
auto tracer = datadog::tracing::Tracer(*validated_config);

// All new spans will have contains tags defined in `tracer_config.tags`
auto span = tracer.create_span();

Set errors on a span

To associate a span with an error, set one or more error-related tags on the span. For example:

span.set_error(true);

Add more specific information about the error by setting any combination of error.message, error.stack, or error.type by using respectively Span::set_error_message, Span::set_error_stack and Span::set_error_type. See Error Tracking for more information about error tags.

An example of adding a combination of error tags:

// Associate this span with the "bad file descriptor" error from the standard
// library.
span.set_error_message("error");
span.set_error_stack("[EBADF] invalid file");
span.set_error_type("errno");
Using any of the `Span::set_error_*` result in an underlying call to `Span::set_error(true)`.

To unset an error on a span, set Span::set_error to false, which removes any combination of Span::set_error_stack, Span::set_error_type or Span::set_error_message.

// Clear any error information associated with this span.
span.set_error(false);

Propagating context with headers extraction and injection

You can configure the propagation of context for distributed traces by injecting and extracting headers. Read Trace Context Propagation for information.

Resource filtering

Traces can be excluded based on their resource name, to remove synthetic traffic such as health checks from sending traces and influencing trace metrics. Find information about this and other security and fine-tuning configuration on the Security page.

Further Reading

PREVIEWING: may/unit-testing