OpenTelemetry By Example

docs

OpenTelemetry By Example is a curated collection of reproducible code snippets that demonstrate how to instrument applications using OpenTelemetry capabilities. From basic setups to advanced use cases with popular frameworks and libraries, these snippets ensure you're always running with up-to-date versions and reliable best practices. Say goodbye to outdated examples—each snippet is designed to be clear, step-by-step, and guaranteed to work every time you run it.

Whether you're instrumenting your application with OpenTelemetry using your favorite programming language or configuring components like the OpenTelemetry Collector, this repository serves as your definitive, state-of-the-art reference for learning OpenTelemetry with hands-on examples.

Supported Snippets

SnippetCI
Pythonpython
Gogo
JavaJava

Go

Go - Hello World

A simple Go console application instrumented with OpenTelemetry that generates a span and metrics in some scenarios:

Configuring environment

Install the following packages:

Python

Test Python Snippets Minimum Python Version

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")
    span.add_event("event in span")
    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")
    span.add_event("event in span")
    print("Hello world")

Metrics

Logs

OpenTelemetry Operator

OpenTelemetry Collector

Contributors