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.
If you want to manually control the lifecycle of the profiler, use the ddtrace.profiling.Profiler object:
fromddtrace.profilingimportProfilerprof=Profiler(env="prod",# if not specified, falls back to environment variable DD_ENVservice="my-web-app",# if not specified, falls back to environment variable DD_SERVICEversion="1.0.3",# if not specified, falls back to environment variable DD_VERSION)prof.start()# Should be as early as possible, eg before other imports, to ensure everything is profiled
When your process forks using os.fork, the profiler needs to be started in
the child process. In Python 3.7+, this is done automatically. In Python < 3.7,
you need to manually start a new profiler in your child process:
# For ddtrace-run users, call this in your child processddtrace.profiling.auto.start_profiler()# Should be as early as possible, eg before other imports, to ensure everything is profiled# Alternatively, for manual instrumentation,# create a new profiler in your child process:fromddtrace.profilingimportProfilerprof=Profiler(...)prof.start()# Should be as early as possible, eg before other imports, to ensure everything is profiled
The Python profiler supports code provenance reporting, which provides
insight into the library that is running the code. While this is
disabled by default, you can turn it on by setting
DD_PROFILING_ENABLE_CODE_PROVENANCE=1.
Stack V2 is the new stack sampler implementation for CPython 3.8+ on x86_64 Linux.
It enhances the performance, accuracy, and reliability of Python CPU profiling.
The feature is enabled by default from ddtrace versions 2.20+ and we highly recommend
using the most recent release of the library to benefit from latest improvements
and bug fixes. This feature activates our new stack sampling, collection and
export system.
The following are known issues and missing features from Stack V2:
gunicorn and Stack V2 results in performance degradation of services
gevent support is lacking
Exception sampling is missing
If you find these as a blocker for enabling Stack V2 for your services, you can
turn it off via setting DD_PROFILING_STACK_V2_ENABLED=0. If you find any other
issue, then please proceed to escalate using appropriate support channels or
file an issue on the GitHub repository.
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.