OpenStack Profiler Library
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
-
TypeError: 'module' object is not callable (from osprofiler import profiler; profiler.start(...))
cause Attempting to call methods directly on the `osprofiler` module instead of the `profiler` object imported from it. The main API is exposed via `osprofiler.profiler`.fixEnsure you are importing `profiler` explicitly: `from osprofiler import profiler`. Then, use `profiler.start()`, `profiler.trace()`, etc. -
NameError: name 'OSProfile' is not defined (or similar error when trying to use 'osprofile')
cause Confusing `osprofiler` (the OpenStack profiler library) with a different library or concept named `osprofile` (e.g., Informatica's OS Profile feature). These are entirely separate and unrelated.fixVerify that you intend to use `osprofiler` for OpenStack-style request tracing. If so, ensure your imports and calls correctly reference `osprofiler` and its `profiler` object. If you are looking for operating system profile management, `osprofiler` is not the correct library. -
osprofiler CLI command not found (e.g., `osprofiler trace show <id>`)
cause The `osprofiler` CLI entry point might not be in your system's PATH, or the package might not be installed in an environment where CLI tools are automatically exposed.fixEnsure `osprofiler` is installed in your active virtual environment. If the issue persists, you might need to run it via `python -m osprofiler.cmd.cli <command>` or check your shell's PATH configuration.
Warnings
- gotcha Calls to `profiler.start()` and `profiler.stop()` or uses of `@profiler.trace` and `with profiler.Trace` will be silently ignored if `osprofiler.profiler.init()` has not been called beforehand. Ensure proper initialization at application startup.
- gotcha For cross-service profiling, `hmac_keys` must be correctly configured and trace headers (`X-Trace-Info`, `X-Trace-HMAC`) must be propagated between services (e.g., via WSGI middleware). Misconfiguration will lead to traces being dropped or not correctly correlated across services.
- breaking Major version 4.0.0 (released June 21, 2023) required Python >=3.9. Projects on older Python versions must upgrade their Python environment to use osprofiler 4.x.
Install
-
pip install osprofiler
Imports
- profiler
import osprofiler
from osprofiler import profiler
- WsgiMiddleware
from osprofiler.web import WsgiMiddleware
Quickstart
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.")