OpenStack Profiler Library

4.3.0 · active · verified Thu Apr 16

OSProfiler is a Python library used by OpenStack projects and clients for cross-service profiling. It enables the generation of a single trace per request across multiple services, forming a tree of calls to help identify performance bottlenecks. It is actively maintained by the OpenStack community and provides API and CLI tools for trace management.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize OSProfiler and add trace points using both the `@profiler.trace` decorator and the `with profiler.Trace` context manager. It highlights the importance of `profiler.init()` for activating profiling. The `HMAC_KEY` is essential for security and validating trace information, especially in distributed OpenStack environments.

import os
from osprofiler import profiler

# In a production OpenStack environment, HMAC_KEY would be a shared secret.
# For local testing, a simple key suffices.
HMAC_KEY = os.environ.get('OSPROFILER_HMAC_KEY', 'my-very-secret-key-for-profiling')

# Initialize the profiler with a base host, project, and HMAC key.
# This is crucial; without initialization, profiling calls are ignored.
profiler.init('local-host', 'my-test-project', HMAC_KEY)

@profiler.trace('my_outer_operation', info={'type': 'application_logic'})
def perform_complex_task(input_data):
    print(f"Starting complex task with: {input_data}")
    # Simulate some work
    import time
    time.sleep(0.01)

    with profiler.Trace('inner_computation', info={'stage': 'data_processing'}):
        intermediate_result = input_data * 2
        time.sleep(0.005)
    
    print(f"Intermediate result: {intermediate_result}")
    return intermediate_result + 10

if profiler.is_active():
    final_output = perform_complex_task(5)
    print(f"Final output: {final_output}")
    print("Profiler is active and trace points would be generated.")
    # In a real setup, trace data would be sent to a collector (e.g., Ceilometer)
    # and retrieved via the `osprofiler` CLI (e.g., `osprofiler trace show <trace_id>`).
else:
    print("Profiler is not active. Check initialization and configuration.")

view raw JSON →