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.

Copied
import sentry_sdk

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

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    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
)

Profiling was experimental in SDK versions 1.17.0 and older. Learn how to upgrade here.

(New in version 2.13.0)

The current profiling implementation stops the profiler automatically after 30 seconds (unless you manually stop it earlier). Naturally, this limitation makes it difficult to get full coverage of your app's execution. We now offer an experimental continuous mode, where profiling data is periodically uploaded while running, with no limit on how long the profiler may run.

To get started with continuous profiling, you can start and stop the profiler directly with sentry_sdk.profiler.start_profiler and sentry_sdk.profiler.stop_profiler.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    traces_sample_rate=1.0,
)

sentry_sdk.profiler.start_profiler()

# run some code here

sentry_sdk.profiler.stop_profiler()

For some applications such as web servers, it may be difficult to call sentry_sdk.profiler.start_profiler in every process. Instead, you can use the continuous_profiling_auto_start option to automatically start the continuous profiler when a transaction is started.

Copied
import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    traces_sample_rate=1.0,
    _experiments={
      "continuous_profiling_auto_start": True,
    },
)

These new APIs do not offer any sampling functionality—every call to start the profiler will start it, and the same goes for launch profiles if you've configured that. If you are interested in reducing the amount of profiles that run, you must take care to do it at the callsites.

Continuous profiling has implications for your org's billing structure. This feature is only available for subscription plans that enrolled after June 5, 2024.

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").