Once you have a reference to a span—either fromDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/datadog/dd-trace-js/llms.txt
Use this file to discover all available pages before exploring further.
tracer.trace(), tracer.startSpan(), or tracer.scope().active()—you can read and modify it using the methods below.
Adding tags
span.setTag(key, value)
Sets a single key/value tag on the span.
span.addTags(keyValuePairs)
Sets multiple tags at once from an object.
Changing the operation name
span.setOperationName(name)
Overrides the operation name set at span creation. Useful when the actual operation name is only known after the span is started.
Finishing a span
span.finish([finishTime])
Marks the span as finished. Pass an optional timestamp (in milliseconds since Unix epoch) to backdate the finish time.
tracer.trace() are finished automatically—do not call span.finish() on them unless you also want to finish early.
Reading span context
span.context()
Returns the SpanContext for this span, which exposes the trace and span IDs.
SpanContext is also used for distributed tracing via tracer.inject() and tracer.extract().
Span links
span.addLink(link)
Adds a causal link to another span. Links are useful when a span has multiple parents or a causal relationship that is not strictly parent/child.
Error handling
Pass theerror tag to record an error on a span. dd-trace recognizes the standard error key and marks the span as an error in the UI.
tracer.trace(), any exception thrown from the callback is automatically recorded as an error. The explicit setTag('error', err) pattern is only needed when you catch and re-throw, or when using tracer.startSpan() directly.
Custom tags best practices
Reserved tags with special meaning:| Tag | Effect |
|---|---|
error | Marks the span as an error. Accepts an Error object or a string. |
error.type | Error class name. |
error.message | Human-readable error message. |
error.stack | Stack trace string. |
service.name | Overrides the service name for OpenTracing compatibility. |
resource.name | Overrides the resource name for OpenTracing compatibility. |
span.type | Overrides the span type for OpenTracing compatibility. |
- String values are truncated at 25,000 UTF-8 bytes.
- Use numeric values (not strings) for quantitative measurements to enable aggregation in Datadog.
