The profiler is shipped within Datadog tracing libraries. If you are already using APM to collect traces for your application, you can skip installing the library and go directly to enabling the profiler.
The following profiling features are available depending on your Python version:
|Feature||Supported Python versions|
|Wall time profiling||Python >= 2.7|
|CPU time profiling||Python >= 2.7 on POSIX platforms|
|Exception profiling||Python >= 3.7 on POSIX platforms|
|Lock profiling||Python >= 2.7|
|Memory profiling||Python >= 3.5|
ddtrace, which provides both tracing and profiling functionalities:
pip install ddtrace
Note: Profiling requires the
ddtrace library version 0.40+.
If you are using a platform where
ddtrace binary distribution is not available, install a development environment.
For example, on Alpine Linux, this can be done with:
apk install gcc musl-dev linux-headers
To automatically profile your code, set the
DD_PROFILING_ENABLED environment variable to
true when you use
DD_PROFILING_ENABLED=true \ DD_ENV=prod \ DD_SERVICE=my-web-app \ DD_VERSION=1.0.3 \ ddtrace-run python app.py
It is strongly recommended that you add tags like
version, as they provide the ability to slice and dice your profiles across these dimensions. See Configuration below.
After a couple of minutes, visualize your profiles on the Datadog APM > Profiler page.
If you want to manually control the lifecycle of the profiler, use the
from ddtrace.profiling import Profiler prof = Profiler( env="prod", # if not specified, falls back to environment variable DD_ENV service="my-web-app", # if not specified, falls back to environment variable DD_SERVICE version="1.0.3", # if not specified, falls back to environment variable DD_VERSION ) prof.start()
When your process forks using
os.fork, the profiler is actually stopped in
the child process and needs to be restarted. For Python 3.7+ on Unix platforms,
a new profiler is automatically started.
If you use Python < 3.7, or run on a non-Unix platform, you need to manually start a new profiler in your child process.
# For ddtrace-run users, call this in your child process ddtrace.profiling.auto.start_profiler() # Alternatively, for manual instrumentation, # create a new profiler in your child process: from ddtrace.profiling import Profiler prof = Profiler() prof.start()
You can configure the profiler using the following environment variables:
|Environment Variable||Keyword Argument to ||Type||Description|
|Boolean||Set to |
|Boolean||Set to |
|String||The Datadog service name.|
|String||The Datadog environment name, for example, |
|String||The version of your application.|
|String / Dictionary||Tags to apply to an uploaded profile. If set with an environment variable, it must be a list of |
The Getting Started with Profiler guide takes a sample service with a performance problem and shows you how to use Continuous Profiler to understand and fix the problem.
Additional helpful documentation, links, and articles: