OpenTelemetry Datadog Exporter

The OpenTelemetry Datadog Exporter provides a span exporter from OpenTelemetry traces to Datadog by using the Datadog Agent.

Installation

pip install opentelemetry-exporter-datadog

Usage

The Datadog exporter provides a span processor that must be added along with the exporter. In addition, a formatter is provided to handle propagation of trace context between OpenTelemetry-instrumented and Datadog-instrumented services in a distributed trace.

from opentelemetry import propagators, trace
from opentelemetry.exporter.datadog import DatadogExportSpanProcessor, DatadogSpanExporter
from opentelemetry.exporter.datadog.propagator import DatadogFormat
from opentelemetry.sdk.trace import TracerProvider

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)

exporter = DatadogSpanExporter(
    agent_url="http://agent:8126", service="my-helloworld-service"
)

span_processor = DatadogExportSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)

# Optional: use Datadog format for propagation in distributed traces
propagators.set_global_textmap(DatadogFormat())

with tracer.start_as_current_span("foo"):
    print("Hello world!")

Examples

The docs/examples/datadog_exporter includes examples for using the Datadog exporter with OpenTelemetry instrumented applications.

API

class opentelemetry.exporter.datadog.DatadogSpanExporter(agent_url=None, service=None, env=None, version=None, tags=None)[source]

Bases: opentelemetry.sdk.trace.export.SpanExporter

Datadog span exporter for OpenTelemetry.

Parameters
  • agent_url – The url of the Datadog Agent or use DD_TRACE_AGENT_URL environment variable

  • service – The service name to be used for the application or use DD_SERVICE environment variable

  • env – Set the application’s environment or use DD_ENV environment variable

  • version – Set the application’s version or use DD_VERSION environment variable

  • tags – A list of default tags to be added to every span or use DD_TAGS environment variable

property agent_writer
export(spans)[source]

Exports a batch of telemetry data.

Parameters

spans – The list of opentelemetry.trace.Span objects to be exported

Returns

The result of the export

shutdown()[source]

Shuts down the exporter.

Called when the SDK is shut down.

class opentelemetry.exporter.datadog.DatadogExportSpanProcessor(span_exporter, schedule_delay_millis=5000, max_trace_size=4096)[source]

Bases: opentelemetry.sdk.trace.SpanProcessor

Datadog exporter span processor

DatadogExportSpanProcessor is an implementation of SpanProcessor that batches all opened spans into a list per trace. When all spans for a trace are ended, the trace is queues up for export. This is required for exporting to the Datadog Agent which expects to received list of spans for each trace.

on_start(span, parent_context=None)[source]

Called when a opentelemetry.trace.Span is started.

This method is called synchronously on the thread that starts the span, therefore it should not block or throw an exception.

Parameters
Return type

None

on_end(span)[source]

Called when a opentelemetry.trace.Span is ended.

This method is called synchronously on the thread that ends the span, therefore it should not block or throw an exception.

Parameters

span (Span) – The opentelemetry.trace.Span that just ended.

Return type

None

worker()[source]
is_trace_exportable(trace_id)[source]
export()[source]

Exports traces with finished spans.

Return type

None

force_flush(timeout_millis=30000)[source]

Export all ended spans to the configured Exporter that have not yet been exported.

Parameters

timeout_millis (int) – The maximum amount of time to wait for spans to be exported.

Return type

bool

Returns

False if the timeout is exceeded, True otherwise.

shutdown()[source]

Called when a opentelemetry.sdk.trace.Tracer is shutdown.

Return type

None