Add custom span tags to your spans to customize your observability within Datadog. The 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.
C++ tracing uses “common tags”. These tags can be sourced from both Datadog specific tags or OpenTracing tags, and included like this:
#include <opentracing/ext/tags.h>
#include <datadog/tags.h>
Note that the Datadog tags are necessary for unified service tagging.
Add tags directly to a span object by calling Span::SetTag
. For example:
auto tracer = ...
auto span = tracer->StartSpan("operation_name");
span->SetTag("key must be string", "Values are variable types");
span->SetTag("key must be string", 1234);
Values are of variable type and can be complex objects. Values are serialized as JSON, with the exception of a string value being serialized bare (without extra quotation marks).
To set tags across all your spans, set the DD_TAGS
environment variable as a list of key:value
pairs separated by commas.
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->SetTag(opentracing::ext::error, true);
Or, alternatively:
span->SetTag("error", true);
Add more specific information about the error by setting any combination of the
error.msg
, error.stack
, or error.type
tags. 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->SetTag("error.msg", "[EBADF] invalid file");
span->SetTag("error.type", "errno");
Adding any of the error.msg
, error.stack
, or error.type
tags sets
error
to the value true
.
To unset an error on a span, set the error
tag to value false
, which removes
any previously set error.msg
, error.stack
, or error.type
tags.
// Clear any error information associated with this span.
span->SetTag("error", false);
Adding spans
Manually instrument a method
To manually instrument your code, install the tracer as in the setup examples, and then use the tracer object to create spans.
{
// Create a root span for the current request.
auto root_span = tracer->StartSpan("get_ingredients");
// Set a resource name for the root span.
root_span->SetTag(datadog::tags::resource_name, "bologna_sandwich");
// Create a child span with the root span as its parent.
auto child_span = tracer->StartSpan(
"cache_lookup",
{opentracing::ChildOf(&root_span->context())});
// Set a resource name for the child span.
child_span->SetTag(datadog::tags::resource_name, "ingredients.bologna_sandwich");
// Spans can be finished at an explicit time ...
child_span->Finish();
} // ... or implicitly when the destructor is invoked.
// For example, root_span finishes here.
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
Additional helpful documentation, links, and articles: