Skip to main content

A library providing profiling functionalities related to OpenTelemetry

Project description

Span Profiles Support for OpenTelemetry in Python

This package links OpenTelemetry tracing data with Pyroscope continuous profiling data, enabling you to correlate traces with performance profiles.

Reference: https://grafana.com/docs/pyroscope/latest/configure-client/trace-span-profiles/

Prerequisites

Installation

pip install pyroscope-otel

Pyroscope Configuration (Required)

Pyroscope must be configured before creating any spans. This is mandatory for all setups:

from pyroscope import configure as pyroscope_configure

# Local setup (default)
pyroscope_configure(
    app_name="my-app",
    server_address="http://localhost:4040",
    sample_rate=100,
)

# Grafana Cloud setup (uncomment and update with your credentials)
# pyroscope_configure(
#     app_name="my-app",
#     server_address="https://pyroscope-blocks-prod-us-central-1.grafana-cloud.com/prom/push",
#     auth_token="<your-grafana-cloud-token>",
#     basic_auth_username="<your-username>",  # Optional: username for basic auth (Grafana Cloud)
#     basic_auth_password="<your-password>",  # Optional: password for basic auth (Grafana Cloud)
#     sample_rate=100,
# )

How It Works & Span Attributes

The PyroscopeSpanProcessor automatically attaches the profile identifier (pyroscope.profile.id) as an attribute to the root span of each trace. This creates a direct link between traces and their corresponding performance profiles in Grafana Tempo, allowing you to navigate from any trace to the exact performance profile data for that transaction.

Manual Instrumentation

Configure OpenTelemetry explicitly (after Pyroscope is already configured):

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from pyroscope.otel import PyroscopeSpanProcessor

# Configure OpenTelemetry
provider = TracerProvider()
provider.add_span_processor(PyroscopeSpanProcessor())

# TODO: Add your trace exporter configuration here
# (e.g., Grafana Tempo OTLP exporter, etc.)
# from opentelemetry.sdk.trace.export import BatchSpanProcessor
# provider.add_span_processor(BatchSpanProcessor(your_exporter))

trace.set_tracer_provider(provider)

# Use tracing in your application
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("my_operation"):
    # Your code here
    pass

Automatic Instrumentation

When using auto-instrumentation (e.g., opentelemetry-distro), you must still register PyroscopeSpanProcessor manually (after Pyroscope is already configured):

from opentelemetry import trace
from pyroscope.otel import PyroscopeSpanProcessor

# After auto-instrumentation is initialized
provider = trace.get_tracer_provider()
provider.add_span_processor(PyroscopeSpanProcessor())

Note: Auto-instrumentation only handles OpenTelemetry setup. Pyroscope configuration is still required.

Grafana Cloud OpenTelemetry Exporter (Optional)

# OpenTelemetry exporter for Grafana Cloud / Grafana Tempo
# from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
#
# otlp_exporter = OTLPSpanExporter(
#     endpoint="<your-tempo-instance>.grafana.net:443",
#     headers=(("Authorization", "Bearer <your-grafana-cloud-token>"),),
# )
# provider.add_span_processor(BatchSpanProcessor(otlp_exporter))

Integration Checklist

  • ✅ Pyroscope configured with pyroscope_configure()
  • ✅ OpenTelemetry TracerProvider created
  • PyroscopeSpanProcessor registered with add_span_processor()
  • ✅ Trace exporter configured (Grafana Tempo, etc.)
  • ✅ Application instrumented with OpenTelemetry
  • ✅ Verify pyroscope.profile.id appears in span attributes in Grafana Tempo

Troubleshooting

Issue Solution
pyroscope.profile.id not in spans Ensure PyroscopeSpanProcessor was registered with add_span_processor()
Profiles not appearing in Pyroscope Verify pyroscope_configure() is called before creating spans
Traces not exporting Check trace exporter configuration and credentials
Auto-instrumentation not working Manually add PyroscopeSpanProcessor() after initializing the provider

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyroscope_otel-1.0.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyroscope_otel-1.0.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file pyroscope_otel-1.0.1.tar.gz.

File metadata

  • Download URL: pyroscope_otel-1.0.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyroscope_otel-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6e491dc3c8065bc47e7304dc074f724ea011926be0ec5f69696b99da62cc54a2
MD5 461fbb75548f66cf952abac950549b63
BLAKE2b-256 9dede7aab94f86a208431f3680e34b9feb079ab5cc27fa059ab243ff1a77ac70

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyroscope_otel-1.0.1.tar.gz:

Publisher: release-please.yml on grafana/otel-profiling-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyroscope_otel-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyroscope_otel-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyroscope_otel-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c517f788758458fcecf31b8f805543e92c1f4a3288900013b781eaf074059398
MD5 65adec4964295f43cf9df4d7c5e41ec9
BLAKE2b-256 460540b89c2f8291501d71157ac2d2d91a130a3391e926ed8cc5058f931cc3d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyroscope_otel-1.0.1-py3-none-any.whl:

Publisher: release-please.yml on grafana/otel-profiling-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page