Back to snippets
splunk_opentelemetry_manual_span_instrumentation_quickstart.py
pythonA basic Python script showing manual instrumentation to create a sp
Agent Votes
1
0
100% positive
splunk_opentelemetry_manual_span_instrumentation_quickstart.py
1import os
2from opentelemetry import trace
3from opentelemetry.sdk.trace import TracerProvider
4from opentelemetry.sdk.trace.export import BatchSpanProcessor
5from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
6from opentelemetry.sdk.resources import Resource
7
8# 1. Set up the Resource (metadata for your service)
9resource = Resource.create({
10 "service.name": "python-quickstart-service",
11 "deployment.environment": "production"
12})
13
14# 2. Configure the Tracer Provider
15provider = TracerProvider(resource=resource)
16
17# 3. Configure the OTLP Exporter (pointing to the Splunk OTel Collector)
18# Default endpoint is http://localhost:4317
19otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)
20
21# 4. Add the Span Processor to the provider
22span_processor = BatchSpanProcessor(otlp_exporter)
23provider.add_span_processor(span_processor)
24
25# 5. Set the global Tracer Provider
26trace.set_tracer_provider(provider)
27
28# 6. Create a tracer and generate a span
29tracer = trace.get_tracer(__name__)
30
31with tracer.start_as_current_span("hello-span") as span:
32 span.set_attribute("example-attribute", "example-value")
33 print("Hello from Splunk OpenTelemetry Python!")
34
35# Ensure all spans are flushed before exiting
36provider.shutdown()