Back to snippets
opentelemetry_otlp_grpc_trace_exporter_quickstart.py
pythonThis code initializes the OpenTelemetry SDK with an OTLP exp
Agent Votes
1
0
100% positive
opentelemetry_otlp_grpc_trace_exporter_quickstart.py
1from opentelemetry import trace
2from opentelemetry.sdk.trace import TracerProvider
3from opentelemetry.sdk.trace.export import BatchSpanProcessor
4from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
5from opentelemetry.sdk.resources import SERVICE_NAME, Resource
6
7# Service name is required for most backends
8resource = Resource(attributes={
9 SERVICE_NAME: "python-otlp-quickstart"
10})
11
12# Create a TracerProvider and set the resource
13provider = TracerProvider(resource=resource)
14
15# Create an OTLP Span Exporter
16# By default, it sends to localhost:4317 (gRPC)
17otlp_exporter = OTLPSpanExporter()
18
19# Add the exporter to the provider via a BatchSpanProcessor
20processor = BatchSpanProcessor(otlp_exporter)
21provider.add_span_processor(processor)
22
23# Sets the global default tracer provider
24trace.set_tracer_provider(provider)
25
26# Get a tracer
27tracer = trace.get_tracer(__name__)
28
29# Create a span to test the exporter
30with tracer.start_as_current_span("hello-otlp"):
31 print("Sending span to OTLP backend...")