OpenTelemetry Protocol Buffer encoding library for OTLP exporters
npx @tessl/cli install tessl/pypi-opentelemetry-exporter-otlp-proto-common@1.36.0A convenience library for encoding OpenTelemetry telemetry data to Protocol Buffers (protobuf) format. This package serves as a common foundation for OTLP (OpenTelemetry Protocol) exporters, providing efficient encoding mechanisms for transforming OpenTelemetry SDK data structures into protobuf messages that can be transmitted to OpenTelemetry collectors and other OTLP-compatible telemetry backends.
pip install opentelemetry-exporter-otlp-proto-common# Version information
from opentelemetry.exporter.otlp.proto.common import __version__
# Main encoding functions
from opentelemetry.exporter.otlp.proto.common.trace_encoder import encode_spans
from opentelemetry.exporter.otlp.proto.common.metrics_encoder import encode_metrics
from opentelemetry.exporter.otlp.proto.common._log_encoder import encode_logsfrom opentelemetry.exporter.otlp.proto.common.trace_encoder import encode_spans
from opentelemetry.exporter.otlp.proto.common.metrics_encoder import encode_metrics
from opentelemetry.exporter.otlp.proto.common._log_encoder import encode_logs
# Example: Encoding spans for trace export
from opentelemetry.sdk.trace import TracerProvider, Tracer
from opentelemetry.sdk.trace.export import ReadableSpan
# Assume you have a collection of spans from the OpenTelemetry SDK
spans = [] # List of ReadableSpan objects
# Encode spans to OTLP protobuf format
encoded_request = encode_spans(spans)
# The encoded_request can now be sent via gRPC or HTTP to an OTLP collectorAccess the package version for compatibility checking and debugging.
__version__: strEncodes OpenTelemetry span data into OTLP protobuf format for transmission to trace collectors.
def encode_spans(sdk_spans: Sequence[ReadableSpan]) -> PB2ExportTraceServiceRequest:
"""
Encode a sequence of OpenTelemetry spans into OTLP protobuf format.
Parameters:
- sdk_spans: Sequence[ReadableSpan] - A sequence of span objects from the OpenTelemetry SDK
Returns:
- PB2ExportTraceServiceRequest - Protobuf message containing encoded trace data
"""Encodes OpenTelemetry metrics data into OTLP protobuf format for transmission to metrics collectors.
def encode_metrics(data: MetricsData) -> ExportMetricsServiceRequest:
"""
Encode OpenTelemetry metrics data into OTLP protobuf format.
Parameters:
- data: MetricsData - OpenTelemetry metrics data object containing metrics to encode
Returns:
- ExportMetricsServiceRequest - Protobuf message containing encoded metrics data
"""Encodes OpenTelemetry log data into OTLP protobuf format for transmission to log collectors.
def encode_logs(batch: Sequence[LogData]) -> ExportLogsServiceRequest:
"""
Encode a sequence of OpenTelemetry log records into OTLP protobuf format.
Parameters:
- batch: Sequence[LogData] - A sequence of log data objects from the OpenTelemetry SDK
Returns:
- ExportLogsServiceRequest - Protobuf message containing encoded log data
"""# Core types from OpenTelemetry SDK (imported for type references)
from typing import Sequence
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.sdk.metrics.export import MetricsData
from opentelemetry.sdk._logs import LogData
# Protobuf message types (return types)
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ExportTraceServiceRequest as PB2ExportTraceServiceRequest
from opentelemetry.proto.collector.metrics.v1.metrics_service_pb2 import ExportMetricsServiceRequest
from opentelemetry.proto.collector.logs.v1.logs_service_pb2 import ExportLogsServiceRequestThe encoding functions may raise exceptions during the encoding process. These are typically standard Python exceptions that occur during protobuf message construction or data validation.
This package requires:
opentelemetry-proto == 1.36.0 - Protocol buffer definitions for OpenTelemetryThis library is designed to be used internally by OTLP exporters including:
opentelemetry-exporter-otlp-proto-grpc - gRPC transport for OTLPopentelemetry-exporter-otlp-proto-http - HTTP transport for OTLPThe encoding functions transform OpenTelemetry SDK data structures into protobuf messages that maintain compatibility with OpenTelemetry collectors and other OTLP-compatible telemetry backends, ensuring proper handling of semantic conventions and the OTLP protobuf schema.