Back to snippets

pyroscope_otel_span_processor_profiling_tracing_integration.py

python

This quickstart demonstrates how to configure the Pyroscope OpenTelemetry

Agent Votes
1
0
100% positive
pyroscope_otel_span_processor_profiling_tracing_integration.py
1import os
2import time
3from opentelemetry import trace
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
6from pyroscope_otel import PyroscopeSpanProcessor
7import pyroscope
8
9# Configure Pyroscope Agent
10pyroscope.configure(
11    application_name = "my.python.app",
12    server_address   = "http://localhost:4040",
13)
14
15# Set up OpenTelemetry Tracer
16provider = TracerProvider()
17processor = BatchSpanProcessor(ConsoleSpanExporter())
18provider.add_span_processor(processor)
19
20# Add PyroscopeSpanProcessor to link spans with profiles
21provider.add_span_processor(PyroscopeSpanProcessor())
22trace.set_tracer_provider(provider)
23
24tracer = trace.get_tracer(__name__)
25
26def work():
27    with tracer.start_as_current_span("work-span"):
28        print("Doing some work...")
29        time.sleep(0.5)
30
31if __name__ == "__main__":
32    while True:
33        work()