OpenTelemetry By Example
OpenTelemetry By Example is a curated collection of practical and reproducible examples that showcase how to instrument your applications leveraging OpenTelemetry's capabilities. These examples cover everything from basic setups to advanced use cases with popular frameworks and libraries. Say goodbye to outdated or deprecated examples—this collection ensures you are always working with the latest and most reliable best practices with step-by-step examples that works everytime you run it.
Whether you're instrumenting your application with OpenTelemetry using your favorite programming language or configuring components like the OpenTelemetry Collector, this book serves as your definitive, state-of-the-art reference for building world-class observability solutions using OpenTelemetry.
Python
Here you can find all OpenTelemetry Python SDK related snippets
Python - Hello World
A simple python console application instrumented with OpenTelemetry that generates a span and metrics in some scenarios:
Configuring environment
Install the following packages:
pip install opentelemetry-api
pip install opentelemetry-sdk
pip install opentelemetry-exporter-otlp-proto-grpc
pip install opentelemetry-exporter-otlp-proto-http
Start your collector:
docker run -p 4317:4317 -p 4318:4318 otel/opentelemetry-collector
Traces
Basic Console Exporter (only for debug purposes)
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
# Creates a resource and adds it to the tracer provider
resource = Resource.create({"service.name": "hello-world-console"})
provider = TracerProvider(resource=resource)
trace.set_tracer_provider(provider)
# Adds span processor with the OTLP exporter to the tracer provider
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
tracer = trace.get_tracer(__name__)
# Starts and sets an attribute to a span
with tracer.start_as_current_span("HelloWorldSpan") as span:
span.set_attribute("foo", "bar")
print("Hello world Example")
Basic OTLP gRPC Exporter
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
# Creates a resource and adds it to the tracer provider
resource = Resource.create({"service.name": "hello-world-otlp-grpc"})
provider = TracerProvider(resource=resource)
trace.set_tracer_provider(provider)
# Adds span processor with the OTLP exporter to the tracer provider
provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4317", insecure=True)))
tracer = trace.get_tracer(__name__, attributes={"scope": "foo"})
# Starts and sets an attribute to a span
with tracer.start_as_current_span("HelloWorldSpanGrpc") as span:
span.set_attribute("foo", "grpc")
print("Hello world")
Basic OTLP HTTP Exporter
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
# Creates a resource and adds it to the tracer provider
resource = Resource.create({"service.name": "hello-world-otlp-http"})
provider = TracerProvider(resource=resource)
trace.set_tracer_provider(provider)
# Adds span processor with the OTLP exporter to the tracer provider
provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")))
tracer = trace.get_tracer(__name__, attributes={"scope": "foo"})
# Starts and sets an attribute to a span
with tracer.start_as_current_span("HelloWorldSpanHttp") as span:
span.set_attribute("foo", "http")
print("Hello world")