OpenTelemetry OTLP Exporters

opentelemetry.exporter.otlp.proto.http

This library allows to export tracing data to an OTLP collector.

Usage

The OTLP Span Exporter allows to export OpenTelemetry traces to the OTLP collector.

You can configure the exporter with the following environment variables:

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# Resource can be required for some backends, e.g. Jaeger
# If resource wouldn't be set - traces wouldn't appears in Jaeger
resource = Resource.create({
    "service.name": "service"
})

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

otlp_exporter = OTLPSpanExporter()

span_processor = BatchSpanProcessor(otlp_exporter)

trace.get_tracer_provider().add_span_processor(span_processor)

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

API

class opentelemetry.exporter.otlp.proto.http.Compression(value)[source]

Bases: Enum

An enumeration.

NoCompression = 'none'
Deflate = 'deflate'
Gzip = 'gzip'
class opentelemetry.exporter.otlp.proto.http.trace_exporter.OTLPSpanExporter(endpoint=None, certificate_file=None, client_key_file=None, client_certificate_file=None, headers=None, timeout=None, compression=None, session=None, *, meter_provider=None)[source]

Bases: SpanExporter

export(spans)[source]

Exports a batch of telemetry data.

Parameters:

spans (Sequence[ReadableSpan]) – The list of opentelemetry.trace.Span objects to be exported

Return type:

SpanExportResult

Returns:

The result of the export

shutdown()[source]

Shuts down the exporter.

Called when the SDK is shut down.

force_flush(timeout_millis=30000)[source]

Nothing is buffered in this exporter, so this method does nothing.

Return type:

bool

class opentelemetry.exporter.otlp.proto.http.metric_exporter.OTLPMetricExporter(endpoint=None, certificate_file=None, client_key_file=None, client_certificate_file=None, headers=None, timeout=None, compression=None, session=None, preferred_temporality=None, preferred_aggregation=None, max_export_batch_size=None, *, meter_provider=None)[source]

Bases: MetricExporter, OTLPMetricExporterMixin

export(metrics_data, timeout_millis=10000, **kwargs)[source]

Exports a batch of telemetry data.

Parameters:

metrics – The list of opentelemetry.sdk.metrics.export.Metric objects to be exported

Return type:

MetricExportResult

Returns:

The result of the export

shutdown(timeout_millis=30000, **kwargs)[source]

Shuts down the exporter.

Called when the SDK is shut down.

Return type:

None

force_flush(timeout_millis=10000)[source]

Nothing is buffered in this exporter, so this method does nothing.

Return type:

bool

set_meter_provider(meter_provider)[source]
Return type:

None

opentelemetry.exporter.otlp.proto.http.metric_exporter.get_resource_data(sdk_resource_scope_data, resource_class, name)[source]
Return type:

list[Resource]

class opentelemetry.exporter.otlp.proto.http._log_exporter.OTLPLogExporter(endpoint=None, certificate_file=None, client_key_file=None, client_certificate_file=None, headers=None, timeout=None, compression=None, session=None, *, meter_provider=None)[source]

Bases: LogRecordExporter

export(batch)[source]

Exports a batch of logs.

Parameters:

batch (Sequence[ReadableLogRecord]) – The list of ReadableLogRecord objects to be exported.

Return type:

LogRecordExportResult

Returns:

The result of the export.

Raises:

Exception – This method may raise exceptions on network errors, timeouts, or other failures. Callers (i.e., log processors) should handle these exceptions to comply with OpenTelemetry error handling principles.

force_flush(timeout_millis=10000)[source]

Nothing is buffered in this exporter, so this method does nothing.

Return type:

bool

shutdown()[source]

Shuts down the exporter.

Called when the SDK is shut down.

opentelemetry.exporter.otlp.proto.grpc

This library allows to export tracing data to an OTLP collector.

Usage

The OTLP Span Exporter allows to export OpenTelemetry traces to the OTLP collector.

You can configure the exporter with the following environment variables:

from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# Resource can be required for some backends, e.g. Jaeger
# If resource wouldn't be set - traces wouldn't appears in Jaeger
resource = Resource.create({
    "service.name": "service"
})

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

otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)

span_processor = BatchSpanProcessor(otlp_exporter)

trace.get_tracer_provider().add_span_processor(span_processor)

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

API

OTLP Span Exporter

class opentelemetry.exporter.otlp.proto.grpc.trace_exporter.OTLPSpanExporter(endpoint=None, insecure=None, credentials=None, headers=None, timeout=None, compression=None, channel_options=None, retryable_error_codes=None, *, meter_provider=None)[source]

Bases: SpanExporter, OTLPExporterMixin[Sequence[ReadableSpan], ExportTraceServiceRequest, SpanExportResult, TraceServiceStub]

OTLP span exporter

Parameters:
export(spans)[source]

Exports a batch of telemetry data.

Parameters:

spans (Sequence[ReadableSpan]) – The list of opentelemetry.trace.Span objects to be exported

Return type:

SpanExportResult

Returns:

The result of the export

shutdown(timeout_millis=30000, **kwargs)[source]

Shuts down the exporter.

Called when the SDK is shut down.

Return type:

None

force_flush(timeout_millis=30000)[source]

Nothing is buffered in this exporter, so this method does nothing.

Return type:

bool

class opentelemetry.exporter.otlp.proto.grpc.metric_exporter.OTLPMetricExporter(endpoint=None, insecure=None, credentials=None, headers=None, timeout=None, compression=None, preferred_temporality=None, preferred_aggregation=None, max_export_batch_size=None, channel_options=None, retryable_error_codes=None, *, meter_provider=None)[source]

Bases: MetricExporter, OTLPExporterMixin[MetricsData, ExportMetricsServiceRequest, MetricExportResult, MetricsServiceStub], OTLPMetricExporterMixin

OTLP metric exporter

Parameters:
  • endpoint (Optional[str]) – Target URL to which the exporter is going to send metrics

  • max_export_batch_size (Optional[int]) – Maximum number of data points to export in a single request. This is to deal with gRPC’s 4MB message size limit. If not set there is no limit to the number of data points in a request. If it is set and the number of data points exceeds the max, the request will be split.

export(metrics_data, timeout_millis=10000, **kwargs)[source]

Exports a batch of telemetry data.

Parameters:

metrics – The list of opentelemetry.sdk.metrics.export.Metric objects to be exported

Return type:

MetricExportResult

Returns:

The result of the export

shutdown(timeout_millis=30000, **kwargs)[source]

Shuts down the exporter.

Called when the SDK is shut down.

Return type:

None

set_meter_provider(meter_provider)[source]
force_flush(timeout_millis=10000)[source]

Nothing is buffered in this exporter, so this method does nothing.

Return type:

bool

class opentelemetry.exporter.otlp.proto.grpc._log_exporter.OTLPLogExporter(endpoint=None, insecure=None, credentials=None, headers=None, timeout=None, compression=None, channel_options=None, retryable_error_codes=None, *, meter_provider=None)[source]

Bases: LogRecordExporter, OTLPExporterMixin[Sequence[ReadableLogRecord], ExportLogsServiceRequest, LogRecordExportResult, LogsServiceStub]

export(batch)[source]

Exports a batch of logs.

Parameters:

batch (Sequence[ReadableLogRecord]) – The list of ReadableLogRecord objects to be exported.

Return type:

Literal[<LogRecordExportResult.SUCCESS: 0>, <LogRecordExportResult.FAILURE: 1>]

Returns:

The result of the export.

Raises:

Exception – This method may raise exceptions on network errors, timeouts, or other failures. Callers (i.e., log processors) should handle these exceptions to comply with OpenTelemetry error handling principles.

shutdown(timeout_millis=30000, **kwargs)[source]

Shuts down the exporter.

Called when the SDK is shut down.

Return type:

None

force_flush(timeout_millis=10000)[source]

Nothing is buffered in this exporter, so this method does nothing.

Return type:

bool

opentelemetry.exporter.otlp.json.file

This library allows exporting OpenTelemetry telemetry to a file or stream in OTLP JSON (JSON Lines) format.

Usage

The OTLP JSON File Exporter writes OpenTelemetry traces, metrics, and logs as compact JSON Lines to a file, an arbitrary IO[str] stream or sys.stdout.

Three exporters are provided:

Each exporter accepts a destination in one of three ways:

  • path - a file path opened in append mode (UTF-8)

  • stream - any IO[str] text stream

  • neither - defaults to sys.stdout

from opentelemetry import trace
from opentelemetry.exporter.otlp.json.file import FileSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

# Write traces to a file (append mode, UTF-8)
exporter = FileSpanExporter("/var/log/otel/traces.jsonl")

# Alternatively, write to an explicit stream:
# exporter = FileSpanExporter(stream=open("traces.jsonl", "a"))

# Or write to sys.stdout (default when no path or stream is given):
# exporter = FileSpanExporter()

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
    BatchSpanProcessor(exporter)
)

tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("foo"):
    print("Hello world!")

The metric exporter supports the following environment variables:

API

class opentelemetry.exporter.otlp.json.file.trace_exporter.FileSpanExporter(path: str | PathLike[str])[source]
class opentelemetry.exporter.otlp.json.file.trace_exporter.FileSpanExporter(*, stream: IO[str])
class opentelemetry.exporter.otlp.json.file.trace_exporter.FileSpanExporter

Bases: SpanExporter

export(spans)[source]

Exports a batch of telemetry data.

Parameters:

spans (Sequence[ReadableSpan]) – The list of opentelemetry.trace.Span objects to be exported

Return type:

SpanExportResult

Returns:

The result of the export

shutdown(timeout_millis=30000, **kwargs)[source]

Shuts down the exporter.

Called when the SDK is shut down.

Return type:

None

force_flush(timeout_millis=30000)[source]

Hint to ensure that the export of any spans the exporter has received prior to the call to ForceFlush SHOULD be completed as soon as possible, preferably before returning from this method.

Return type:

bool

class opentelemetry.exporter.otlp.json.file.metric_exporter.FileMetricExporter(path: str | PathLike[str], *, preferred_temporality: dict[type, AggregationTemporality] | None = None, preferred_aggregation: dict[type, Aggregation] | None = None)[source]
class opentelemetry.exporter.otlp.json.file.metric_exporter.FileMetricExporter(*, stream: IO[str], preferred_temporality: dict[type, AggregationTemporality] | None = None, preferred_aggregation: dict[type, Aggregation] | None = None)
class opentelemetry.exporter.otlp.json.file.metric_exporter.FileMetricExporter(*, preferred_temporality: dict[type, AggregationTemporality] | None = None, preferred_aggregation: dict[type, Aggregation] | None = None)

Bases: MetricExporter

export(metrics_data, timeout_millis=10000, **kwargs)[source]

Exports a batch of telemetry data.

Parameters:

metrics – The list of opentelemetry.sdk.metrics.export.Metric objects to be exported

Return type:

MetricExportResult

Returns:

The result of the export

shutdown(timeout_millis=30000, **kwargs)[source]

Shuts down the exporter.

Called when the SDK is shut down.

Return type:

None

force_flush(timeout_millis=10000)[source]

Ensure that export of any metrics currently received by the exporter are completed as soon as possible.

Return type:

bool

class opentelemetry.exporter.otlp.json.file._log_exporter.FileLogExporter(path: str | PathLike[str])[source]
class opentelemetry.exporter.otlp.json.file._log_exporter.FileLogExporter(*, stream: IO[str])
class opentelemetry.exporter.otlp.json.file._log_exporter.FileLogExporter

Bases: LogRecordExporter

export(batch)[source]

Exports a batch of logs.

Parameters:

batch (Sequence[ReadableLogRecord]) – The list of ReadableLogRecord objects to be exported.

Return type:

LogRecordExportResult

Returns:

The result of the export.

Raises:

Exception – This method may raise exceptions on network errors, timeouts, or other failures. Callers (i.e., log processors) should handle these exceptions to comply with OpenTelemetry error handling principles.

shutdown()[source]

Shuts down the exporter.

Called when the SDK is shut down.

Return type:

None

force_flush(timeout_millis=10000)[source]
Return type:

bool