You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Learn how to enable profiling in your app if it is not already set up."
4
4
sidebar_order: 5000
5
5
---
@@ -8,104 +8,121 @@ sidebar_order: 5000
8
8
9
9
With [profiling](/product/explore/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](https://sentry.io/for/profiling/) 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](https://sentry.io/for/python/)
10
10
11
-
## Enable Profiling in Python
11
+
## Enabling Continuous Profiling
12
12
13
13
<Alert>
14
14
15
-
Python profiling is stable as of SDK version `1.18.0`.
15
+
Continuous profiling is available starting in SDK version `2.24.1`.
16
+
17
+
<PlatformLinkto="/profiling/#enable-transaction-based-profiling">Transaction-based profiling</PlatformLink> is available starting in SDK version `1.18.0`.
16
18
17
19
</Alert>
18
20
21
+
Continuous profiling supports two modes - `manual` and `trace`. The two modes are mutually exclusive, and cannot be used at the same time.
22
+
23
+
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.
24
+
25
+
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.
26
+
27
+
### Enabling Trace Lifecycle Profiling
28
+
19
29
```python
20
30
import sentry_sdk
21
31
22
-
defprofiles_sampler(sampling_context):
23
-
# ...
24
-
# return a number between 0 and 1 or a boolean
25
-
26
32
sentry_sdk.init(
27
33
dsn="___PUBLIC_DSN___",
28
-
29
-
# Add data like request headers and IP for users, if applicable;
30
-
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
31
34
send_default_pii=True,
32
-
33
35
traces_sample_rate=1.0,
34
-
35
-
# To set a uniform sample rate
36
-
# Set profiles_sample_rate to 1.0 to profile 100%
37
-
# of sampled transactions.
38
-
# We recommend adjusting this value in production,
39
-
profiles_sample_rate=1.0,
40
-
41
-
# Alternatively, to control sampling dynamically
42
-
profiles_sampler=profiles_sampler
36
+
# To collect profiles for all profile sessions,
37
+
# set `profile_session_sample_rate` to 1.0.
38
+
profile_session_sample_rate=1.0,
39
+
# Profiles will be automatically collected while
40
+
# there is an active span.
41
+
profile_lifecycle="trace",
43
42
)
44
43
```
45
44
46
-
<Alert>
45
+
### Enabling Manual Lifecycle Profiling
47
46
48
-
The <PlatformIdentifiername="profiles_sample_rate" /> setting is _relative_ to the <PlatformIdentifiername="traces_sample_rate" /> setting.
47
+
```python
48
+
import sentry_sdk
49
49
50
-
For Profiling to work, you have to first enable [Sentry’s tracing](/concepts/key-terms/tracing/) via `traces_sample_rate` (like in the example above). Read our <PlatformLinkto="/tracing/">tracing setup documentation</PlatformLink> to learn how to configure sampling. If you set your sample rate to 1.0, all transactions will be captured.
50
+
sentry_sdk.init(
51
+
dsn="___PUBLIC_DSN___",
52
+
send_default_pii=True,
53
+
traces_sample_rate=1.0,
51
54
52
-
</Alert>
55
+
# To collect profiles for all profile sessions,
56
+
# set `profile_session_sample_rate` to 1.0.
57
+
profile_session_sample_rate=1.0,
58
+
# Profiles will be collected when
59
+
# `sentry_sdk.profiler.start_profiler` is called and
60
+
# stopped when `sentry_sdk.profiler.stop_profiler` is called.
61
+
profile_lifecycle="manual",
62
+
)
53
63
54
-
## Enable Continuous Profiling
64
+
sentry_sdk.profiler.start_profiler()
55
65
56
-
<Includename="feature-stage-beta.mdx" />
66
+
# run some code here
57
67
58
-
_(New in version 2.24.1)_
68
+
sentry_sdk.profiler.stop_profiler()
69
+
```
59
70
60
-
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.
71
+
### Managing Profile Sampling Rates
61
72
62
-
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`.
73
+
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.
63
74
64
-
### Sampling
75
+
This is useful for cases where you deploy your service many times, but would only like a subset of those deployments to be profiled.
65
76
66
-
Sampling for continuous profiling is determined only once when the SDK is configured. That sampling decision is used to decide if the profiles will be collected or not for the entirety of the process.
77
+
### Upgrading From Older SDK Versions
67
78
68
-
Set `profile_session_sample_rate=1.0` to collect continuous profiles for 100% of profile sessions.
79
+
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 <PlatformLinkto="/troubleshooting/#continuous-profiling">here</PlatformLink>.
80
+
81
+
## Enable Transaction-Based Profiling
69
82
70
83
<Alert>
71
84
72
-
If you previously set `profiles_sample_rate` or `profilers_sampler` to use transaction-based profiling, you must remove those lines of code from your configuration in order to use continuous profiling.
85
+
Transaction-based profiling is available starting in SDK version `1.18.0`.
73
86
74
87
</Alert>
75
88
89
+
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.
90
+
76
91
```python
77
92
import sentry_sdk
78
93
94
+
defprofiles_sampler(sampling_context):
95
+
# ...
96
+
# return a number between 0 and 1 or a boolean
97
+
79
98
sentry_sdk.init(
80
99
dsn="___PUBLIC_DSN___",
100
+
101
+
# Add data like request headers and IP for users, if applicable;
102
+
# see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info
81
103
send_default_pii=True,
104
+
82
105
traces_sample_rate=1.0,
83
106
84
-
# To collect profiles for all profile sessions, set `profile_session_sample_rate` to 1.0.
85
-
profile_session_sample_rate=1.0,
86
-
)
107
+
# To set a uniform sample rate
108
+
# Set profiles_sample_rate to 1.0 to profile 100%
109
+
# of sampled transactions.
110
+
# We recommend adjusting this value in production,
111
+
profiles_sample_rate=1.0,
87
112
88
-
sentry_sdk.profiler.start_profiler()
113
+
# Alternatively, to control sampling dynamically
114
+
profiles_sampler=profiles_sampler
115
+
)
116
+
```
89
117
90
-
# run some code here
118
+
<Alert>
91
119
92
-
sentry_sdk.profiler.stop_profiler()
93
-
```
94
-
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.
120
+
The <PlatformIdentifiername="profiles_sample_rate" /> setting is _relative_ to the <PlatformIdentifiername="traces_sample_rate" /> setting.
95
121
96
-
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 `profile_lifecycle` option to automatically profile anytime a transaction is active.
122
+
For transaction-based profiling to work, you have to first enable [Sentry’s tracing](/concepts/key-terms/tracing/) via `traces_sample_rate` (like in the example above). Read our <PlatformLinkto="/tracing/">tracing setup documentation</PlatformLink> to learn how to configure sampling. If you set your sample rate to 1.0, all transactions will be captured.
97
123
98
-
```python
99
-
import sentry_sdk
124
+
</Alert>
100
125
101
-
sentry_sdk.init(
102
-
dsn="___PUBLIC_DSN___",
103
-
send_default_pii=True,
104
-
traces_sample_rate=1.0,
105
-
# To collect profiles for all profile sessions, set `profile_session_sample_rate` to 1.0.
106
-
profile_session_sample_rate=1.0,
107
-
profile_lifecycle="trace",
108
-
)
109
-
```
126
+
### Upgrading From Older Python SDK Versions
110
127
111
-
Continuous profiling has implications for your org's billing structure. This feature is only available for subscription plans that enrolled after June 5, 2024.
128
+
Transaction-based profiling was experimental in SDK versions prior to `1.18.0`. Learn how to upgrade <PlatformLinkto="/troubleshooting/#transaction-based-profiling">here</PlatformLink>.
Copy file name to clipboardExpand all lines: docs/platforms/python/troubleshooting.mdx
+12-2Lines changed: 12 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -193,17 +193,27 @@ If you don't see any profiling data in [sentry.io](https://sentry.io), you can t
193
193
194
194
### Upgrading From Older SDK Versions
195
195
196
-
The feature was experimental prior to version `1.17.0`. To update your SDK to the latest version, remove `profiles_sample_rate` from `_experiments` and set it in the top-level options.
196
+
#### Transaction-Based Profiling
197
+
198
+
The transaction-based profiling feature was experimental prior to version `1.18.0`. To update your SDK to the latest version, remove `profiles_sample_rate` from `_experiments` and set it in the top-level options.
197
199
198
200
```python
199
201
sentry_sdk.init(
200
202
dsn="___PUBLIC_DSN___",
201
203
traces_sample_rate=1.0,
202
204
_experiments={
203
-
"profiles_sample_rate": 1.0, # for versions before 1.17.0
205
+
"profiles_sample_rate": 1.0, # for versions before 1.18.0
204
206
},
205
207
)
206
208
```
209
+
210
+
#### Continuous Profiling
211
+
212
+
The continuous profiling feature was experimental prior to version `2.24.1`. To upgrade your SDK to the latest version:
213
+
214
+
- Remove `continuous_profiling_auto_start` from `_experiments` and set `profile_lifecycle="trace"` in the top-level options.
215
+
- Add `profile_session_sample_rate` to the top-level options.
0 commit comments