# 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:

- `OTEL_EXPORTER_OTLP_TRACES_TIMEOUT`

- `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL`

- `OTEL_EXPORTER_OTLP_TRACES_HEADERS`

- `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`

- `OTEL_EXPORTER_OTLP_TRACES_COMPRESSION`

- `OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE`

- `OTEL_EXPORTER_OTLP_TIMEOUT`

- `OTEL_EXPORTER_OTLP_PROTOCOL`

- `OTEL_EXPORTER_OTLP_HEADERS`

- `OTEL_EXPORTER_OTLP_ENDPOINT`

- `OTEL_EXPORTER_OTLP_COMPRESSION`

- `OTEL_EXPORTER_OTLP_CERTIFICATE`

    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(*values)

  Bases: `Enum`

  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)

  Bases: `SpanExporter`

  export(spans)

    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()

    Shuts down the exporter.

    Called when the SDK is shut down.

  force_flush(timeout_millis=30000)

    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)

  Bases: `MetricExporter`, `OTLPMetricExporterMixin`

  export(metrics_data, timeout_millis=10000, **kwargs)

    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)

    Shuts down the exporter.

    Called when the SDK is shut down.

    Return type:

      `None`

  force_flush(timeout_millis=10000)

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

    Return type:

      `bool`

  set_meter_provider(meter_provider)
    Return type:

      `None`

opentelemetry.exporter.otlp.proto.http.metric_exporter.get_resource_data(sdk_resource_scope_data, resource_class, name)
  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)

  Bases: `LogRecordExporter`

  export(batch)

    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)

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

    Return type:

      `bool`

  shutdown()

    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:

- `OTEL_EXPORTER_OTLP_TRACES_TIMEOUT`

- `OTEL_EXPORTER_OTLP_TRACES_PROTOCOL`

- `OTEL_EXPORTER_OTLP_TRACES_HEADERS`

- `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`

- `OTEL_EXPORTER_OTLP_TRACES_COMPRESSION`

- `OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE`

- `OTEL_EXPORTER_OTLP_TIMEOUT`

- `OTEL_EXPORTER_OTLP_PROTOCOL`

- `OTEL_EXPORTER_OTLP_HEADERS`

- `OTEL_EXPORTER_OTLP_ENDPOINT`

- `OTEL_EXPORTER_OTLP_COMPRESSION`

- `OTEL_EXPORTER_OTLP_CERTIFICATE`

    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)

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

  OTLP span exporter

  Parameters:

    - endpoint (`str` | `None`) – OpenTelemetry Collector receiver endpoint

    - insecure (`bool` | `None`) – Connection type

    - credentials (`ChannelCredentials` | `None`) – Credentials object for server authentication

    - headers (`Sequence`[`tuple`[`str`, `str`]] | `dict`[`str`, `str`] | `str` | `None`) – Headers to send when exporting

    - timeout (`float` | `None`) – Backend request timeout in seconds

    - compression (`Compression` | `None`) – gRPC compression method to use

  export(spans)

    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)

    Shuts down the exporter.

    Called when the SDK is shut down.

    Return type:

      `None`

  force_flush(timeout_millis=30000)

    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)

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

  OTLP metric exporter

  Parameters:

    - endpoint (`str` | `None`) – Target URL to which the exporter is going to send metrics

    - max_export_batch_size (`int` | `None`) – 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)

    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)

    Shuts down the exporter.

    Called when the SDK is shut down.

    Return type:

      `None`

  set_meter_provider(meter_provider)
  force_flush(timeout_millis=10000)

    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)

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

  export(batch)

    Exports a batch of logs.

    Parameters:

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

    Return type:

      `Literal`[`SUCCESS`, `FAILURE`]

    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)

    Shuts down the exporter.

    Called when the SDK is shut down.

    Return type:

      `None`

  force_flush(timeout_millis=10000)

    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:

- `FileSpanExporter` - traces

- `FileMetricExporter` - metrics

- `FileLogExporter` - logs

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:

- `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE`

- `OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION`

### API

class opentelemetry.exporter.otlp.json.file.trace_exporter.FileSpanExporter(path=None, *, stream=None)

  Bases: `SpanExporter`

  export(spans)

    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)

    Shuts down the exporter.

    Called when the SDK is shut down.

    Return type:

      `None`

  force_flush(timeout_millis=30000)

    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=None, *, stream=None, preferred_temporality=None, preferred_aggregation=None)

  Bases: `MetricExporter`

  export(metrics_data, timeout_millis=10000, **kwargs)

    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)

    Shuts down the exporter.

    Called when the SDK is shut down.

    Return type:

      `None`

  force_flush(timeout_millis=10000)

    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=None, *, stream=None)

  Bases: `LogRecordExporter`

  export(batch)

    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()

    Shuts down the exporter.

    Called when the SDK is shut down.

    Return type:

      `None`

  force_flush(timeout_millis=10000)
    Return type:

      `bool`