Pyroscope Python Integration

1.0.4 · active · verified Fri Apr 10

The `pyroscope-io` library provides seamless integration for Python applications with the Pyroscope continuous profiling platform. It collects CPU and memory profiling data from your application and sends it to a Pyroscope server for visualization and analysis. Currently at version 1.0.4, it follows a feature-driven release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates how to configure `pyroscope-io` and profile specific functions using the `@pyroscope.profile` decorator. Ensure a Pyroscope server is running and accessible at the configured `server_address`. The `application_name` is crucial for identifying your data in the Pyroscope UI.

import pyroscope
import time
import os

# Configure Pyroscope to send profiling data
# Ensure PYROSCOPE_SERVER_ADDRESS environment variable is set
# or use a default, e.g., 'http://localhost:4040'
pyroscope.configure(
    server_address=os.environ.get("PYROSCOPE_SERVER_ADDRESS", "http://localhost:4040"),
    application_name="my.python.app",
    # Optional: Set tags for filtering data in Pyroscope UI
    # tags={"env": "dev", "region": "us-east"}
)

# Decorator to profile a specific function
@pyroscope.profile(name="my_long_running_task")
def my_long_running_task():
    """Simulates a task that takes some CPU time."""
    print("Running my_long_running_task...")
    _ = [i*i for i in range(1_000_000)] # CPU intensive operation
    time.sleep(0.1)

@pyroscope.profile(name="another_task")
def another_task():
    """Another task that takes some time."""
    print("Running another_task...")
    time.sleep(0.2)


def main():
    print("Starting profiling example. Run a Pyroscope server on ", pyroscope.configure().get('server_address'), "to see data.")
    for i in range(5):
        print(f"Iteration {i+1}")
        my_long_running_task()
        another_task()
        time.sleep(0.5) # Simulate idle time
    print("Profiling example finished. Data might take a moment to appear in Pyroscope UI.")

if __name__ == "__main__":
    main()

view raw JSON →