OpenTelemetry Collector Exporters for sending telemetry data via OTLP protocol
—
OTLP exporters using gRPC protocol with protobuf serialization. These exporters provide efficient binary protocol communication with OpenTelemetry Collector endpoints, offering high performance and built-in load balancing capabilities.
Exports distributed tracing data using the gRPC protocol. Supports streaming and efficient batch export of span data to OpenTelemetry Collector endpoints.
class OTLPSpanExporter:
def __init__(
self,
endpoint: str | None = None,
insecure: bool | None = None,
credentials: grpc.ChannelCredentials | None = None,
headers: dict[str, str] | None = None,
timeout: float | None = None,
compression: grpc.Compression | None = None,
channel_options: list[tuple[str, str]] | None = None
):
"""
Create a gRPC OTLP span exporter.
Parameters:
- endpoint (str, optional): Collector endpoint URL. Defaults to "localhost:4317"
- insecure (bool, optional): Use insecure connection. Defaults to True for localhost
- credentials (grpc.ChannelCredentials, optional): gRPC channel credentials for secure connections
- headers (dict, optional): Additional headers to send with requests
- timeout (int, optional): Export timeout in seconds. Defaults to 10
- compression (grpc.Compression, optional): gRPC compression algorithm
- channel_options (list, optional): Additional gRPC channel options
Environment Variables:
- OTEL_EXPORTER_OTLP_TRACES_ENDPOINT
- OTEL_EXPORTER_OTLP_TRACES_HEADERS
- OTEL_EXPORTER_OTLP_TRACES_TIMEOUT
- OTEL_EXPORTER_OTLP_TRACES_COMPRESSION
"""
def export(self, spans) -> SpanExportResult:
"""Export span data to the collector."""
def shutdown(self, timeout_millis: int = 30000) -> bool:
"""Shutdown the exporter and clean up resources."""
def force_flush(self, timeout_millis: int = 30000) -> bool:
"""Force flush any pending spans."""Exports metrics data using the gRPC protocol. Provides efficient transmission of measurement data with support for different temporality preferences and batch size limits.
class OTLPMetricExporter:
def __init__(
self,
endpoint: str | None = None,
insecure: bool | None = None,
credentials: grpc.ChannelCredentials | None = None,
headers: dict[str, str] | None = None,
timeout: float | None = None,
compression: grpc.Compression | None = None,
preferred_temporality: dict | None = None,
preferred_aggregation: dict | None = None,
max_export_batch_size: int | None = None,
channel_options: list[tuple[str, str]] | None = None
):
"""
Create a gRPC OTLP metric exporter.
Parameters:
- endpoint (str, optional): Collector endpoint URL. Defaults to "localhost:4317"
- insecure (bool, optional): Use insecure connection. Defaults to True for localhost
- credentials (grpc.ChannelCredentials, optional): gRPC channel credentials for secure connections
- headers (dict, optional): Additional headers to send with requests
- timeout (int, optional): Export timeout in seconds. Defaults to 10
- compression (grpc.Compression, optional): gRPC compression algorithm
- preferred_temporality (dict, optional): Preferred temporality for metric instruments
- preferred_aggregation (dict, optional): Preferred aggregation for metric instruments
- max_export_batch_size (int, optional): Maximum number of metrics per export batch
- channel_options (list, optional): Additional gRPC channel options
Environment Variables:
- OTEL_EXPORTER_OTLP_METRICS_ENDPOINT
- OTEL_EXPORTER_OTLP_METRICS_HEADERS
- OTEL_EXPORTER_OTLP_METRICS_TIMEOUT
- OTEL_EXPORTER_OTLP_METRICS_COMPRESSION
"""
def export(self, metrics) -> MetricExportResult:
"""Export metric data to the collector."""
def shutdown(self, timeout_millis: int = 30000) -> bool:
"""Shutdown the exporter and clean up resources."""
def force_flush(self, timeout_millis: int = 30000) -> bool:
"""Force flush any pending metrics."""Exports structured log data using the gRPC protocol. Enables efficient transmission of log records with full OpenTelemetry context correlation.
class OTLPLogExporter:
def __init__(
self,
endpoint: str | None = None,
insecure: bool | None = None,
credentials: grpc.ChannelCredentials | None = None,
headers: dict[str, str] | None = None,
timeout: float | None = None,
compression: grpc.Compression | None = None,
channel_options: list[tuple[str, str]] | None = None
):
"""
Create a gRPC OTLP log exporter.
Parameters:
- endpoint (str, optional): Collector endpoint URL. Defaults to "localhost:4317"
- insecure (bool, optional): Use insecure connection. Defaults to True for localhost
- credentials (grpc.ChannelCredentials, optional): gRPC channel credentials for secure connections
- headers (dict, optional): Additional headers to send with requests
- timeout (int, optional): Export timeout in seconds. Defaults to 10
- compression (grpc.Compression, optional): gRPC compression algorithm
- channel_options (list, optional): Additional gRPC channel options
Environment Variables:
- OTEL_EXPORTER_OTLP_LOGS_ENDPOINT
- OTEL_EXPORTER_OTLP_LOGS_HEADERS
- OTEL_EXPORTER_OTLP_LOGS_TIMEOUT
- OTEL_EXPORTER_OTLP_LOGS_COMPRESSION
"""
def export(self, logs) -> LogExportResult:
"""Export log data to the collector."""
def shutdown(self, timeout_millis: int = 30000) -> bool:
"""Shutdown the exporter and clean up resources."""
def force_flush(self, timeout_millis: int = 30000) -> bool:
"""Force flush any pending logs."""class OTLPExporterMixin:
"""
Generic base class providing common OTLP gRPC export functionality.
This mixin provides shared functionality for all gRPC-based OTLP exporters,
including configuration parsing, channel setup, and common export logic.
"""
def _configure_exporter(self, **kwargs) -> None:
"""Configure the exporter with provided parameters and environment variables."""
def _export(self, serialized_data: bytes, path: str) -> bool:
"""Export serialized data to the configured endpoint."""
def _shutdown(self) -> None:
"""Clean up resources used by the exporter."""def environ_to_compression(compression: str) -> grpc.Compression:
"""
Convert environment variable compression value to gRPC Compression enum.
Parameters:
- compression (str): Compression algorithm name ("gzip", "deflate", or "none")
Returns:
grpc.Compression: Corresponding gRPC compression enum value
Raises:
ValueError: If compression algorithm is not supported
"""def _get_credentials(
insecure: bool = None,
credentials: grpc.ChannelCredentials = None
) -> grpc.ChannelCredentials:
"""
Load SSL credentials from environment variables or provided parameters.
Parameters:
- insecure (bool, optional): Whether to use insecure connection
- credentials (grpc.ChannelCredentials, optional): Pre-configured credentials
Returns:
grpc.ChannelCredentials: Channel credentials for secure connections
Environment Variables:
- OTEL_EXPORTER_OTLP_CERTIFICATE
- OTEL_EXPORTER_OTLP_CLIENT_KEY
- OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE
"""from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
# Create exporter with default settings
exporter = OTLPSpanExporter()
# Set up tracer provider with batch processor
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(exporter))
# Use the tracer
tracer = tracer_provider.get_tracer(__name__)
with tracer.start_as_current_span("example-span"):
print("Hello, World!")import grpc
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
# Create secure credentials
credentials = grpc.ssl_channel_credentials(
root_certificates=None, # Use system's root certificates
private_key=None,
certificate_chain=None
)
# Create exporter with secure connection
exporter = OTLPSpanExporter(
endpoint="https://my-collector:4317",
credentials=credentials,
headers={"api-key": "your-api-key"},
compression=grpc.Compression.Gzip
)import os
# Set environment variables
os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = "https://collector:4317"
os.environ["OTEL_EXPORTER_OTLP_TRACES_HEADERS"] = "api-key=your-key"
os.environ["OTEL_EXPORTER_OTLP_TRACES_COMPRESSION"] = "gzip"
# Create exporter - will automatically use environment variables
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
exporter = OTLPSpanExporter()# Default gRPC channel options
_OTLP_GRPC_CHANNEL_OPTIONS: list = [
("grpc.user_agent", "opentelemetry-python"),
# Additional channel configuration options
]Install with Tessl CLI
npx tessl i tessl/pypi-opentelemetry-exporter-otlp