Set Up Profiling

Learn how to enable profiling in your app if it is not already set up.

With profiling, Sentry tracks your software's performance by sampling your program's call stack in a variety of environments. This feature collects function-level information about your code and enables you to fine-tune your program's performance. Sentry's profiler captures function calls and their exact locations, aggregates them, and shows you the most common code paths of your program. This highlights areas you could optimize to help increase both the performance of your code and increase user satisfaction, as well as drive down costs. Learn how to enable and configure profiling in Python with Sentry's stable Python SDK

Continuous profiling supports two modes - manual and trace. The two modes are mutually exclusive, and cannot be used at the same time.

In manual mode, the profiling data collection can be managed via calls to sentry_sdk.profiler.start_profiler and sentry_sdk.profiler.stop_profiler. You are entirely in control of when the profiler runs.

In trace mode, the profiler manages its own start and stop calls, which are based on spans: the profiler continues to run while there is at least one active span, and stops when there are no active spans.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be automatically collected while
    # there is an active span.
    profile_lifecycle="trace",
)

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # To collect profiles for all profile sessions,
    # set `profile_session_sample_rate` to 1.0.
    profile_session_sample_rate=1.0,
    # Profiles will be collected when
    # `sentry_sdk.profiler.start_profiler` is called and
    # stopped when `sentry_sdk.profiler.stop_profiler` is called.
    profile_lifecycle="manual",
)

sentry_sdk.profiler.start_profiler()

# run some code here

sentry_sdk.profiler.stop_profiler()

Sentry SDK supports an additional profile_session_sample_rate that must be set to a non-zero value to enable continuous profiling. This can be used to control session sampling rates at the service level as the sampling decision is evaluated only once at SDK initialization.

This is useful for cases where you deploy your service many times, but would only like a subset of those deployments to be profiled.

Continuous profiling was experimental in SDK versions prior to 2.24.1 and will be deprecated. Data sent by these older versions will not be accepted in the near future. Learn how to upgrade here.

Transaction-based profiling only runs in tandem with performance transactions that were started either automatically or manually with sentry_sdk.start_transaction, and stops after the transaction ends or after 30 seconds.

Copied
import sentry_sdk
from sentry_sdk.types import SamplingContext

def profiles_sampler(sampling_context: SamplingContext) -> float:
    # ...
    # return a number between 0 and 1 or a boolean

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Add data like request headers and IP for users, if applicable;
    # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
    send_default_pii=True,
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # To set a uniform sample rate
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production,
    profiles_sample_rate=1.0,
    # Alternatively, to control sampling dynamically
    profiles_sampler=profiles_sampler
)

Transaction-based profiling was experimental in SDK versions prior to 1.18.0. Learn how to upgrade here.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").