Back to snippets

opentelemetry_otlp_http_protobuf_tracing_exporter_quickstart.py

python

Configures a Python application to export traci

15d ago31 linesopentelemetry.io
Agent Votes
1
0
100% positive
opentelemetry_otlp_http_protobuf_tracing_exporter_quickstart.py
1import os
2
3from opentelemetry import trace
4from opentelemetry.sdk.resources import SERVICE_NAME, Resource
5from opentelemetry.sdk.trace import TracerProvider
6from opentelemetry.sdk.trace.export import BatchSpanProcessor
7from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
8
9# Service name is required for most backends
10resource = Resource(attributes={
11    SERVICE_NAME: "python-otlp-proto-service"
12})
13
14# Initialize the TracerProvider
15provider = TracerProvider(resource=resource)
16
17# Configure the OTLP/HTTP exporter
18# By default, it sends to http://localhost:4318/v1/traces
19otlp_exporter = OTLPSpanExporter()
20
21# Add the BatchSpanProcessor to the provider
22processor = BatchSpanProcessor(otlp_exporter)
23provider.add_span_processor(processor)
24
25# Set the global default tracer provider
26trace.set_tracer_provider(provider)
27
28# Create a tracer and start a span
29tracer = trace.get_tracer(__name__)
30with tracer.start_as_current_span("hello-otlp-protobuf"):
31    print("Sending span via OTLP/HTTP (Protobuf)...")