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,109 +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 rofiling 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 the 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>
47
-
48
-
The <PlatformIdentifiername="profiles_sample_rate" /> setting is _relative_ to the <PlatformIdentifiername="traces_sample_rate" /> setting.
45
+
### Enabling Manual Lifecycle Profiling
49
46
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.
47
+
```python
48
+
import sentry_sdk
51
49
52
-
</Alert>
50
+
sentry_sdk.init(
51
+
dsn="___PUBLIC_DSN___",
52
+
send_default_pii=True,
53
+
traces_sample_rate=1.0,
53
54
54
-
### Upgrading from Older Python SDK Versions
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
+
)
55
63
56
-
Profiling was experimental in SDK versions `1.17.0` and older. Learn how to upgrade <PlatformLinkto="/profiling/troubleshooting/#ipgrading-from-older-sdk-versions">here</PlatformLink>.
64
+
sentry_sdk.profiler.start_profiler()
57
65
58
-
## Enable Continuous Profiling
66
+
# run some code here
59
67
60
-
<Includename="feature-stage-beta.mdx" />
68
+
sentry_sdk.profiler.stop_profiler()
69
+
```
61
70
62
-
_(New in version 2.24.1)_
71
+
### Managing profile sampling rates
63
72
64
-
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.
73
+
Sentry SDK supports an additional `profile_session_sample_rate` that will enable or disable profiling for the entire session. This can be used if you want to control session sampling rates at the service level as the sampling decision is evaluated only once at SDK init.
65
74
66
-
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`.
75
+
This is useful for cases where you deploy your service many times, but would only like a subset of those services to be profiled.
67
76
68
-
### Sampling
77
+
### Upgrading from Older SDK versions
69
78
70
-
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.
79
+
Continuous profiling was experimental in SDK versions prior to `2.24.1`. Learn how to upgrade <PlatformLinkto="/profiling/troubleshooting/#continuous-profiling">here</PlatformLink>.
71
80
72
-
Set `profile_session_sample_rate=1.0` to collect continuous profiles for 100% of profile sessions.
81
+
## Enable Transaction Based Profiling
73
82
74
83
<Alert>
75
84
76
-
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`.
77
86
78
87
</Alert>
79
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
+
80
91
```python
81
92
import sentry_sdk
82
93
94
+
defprofiles_sampler(sampling_context):
95
+
# ...
96
+
# return a number between 0 and 1 or a boolean
97
+
83
98
sentry_sdk.init(
84
99
dsn="___PUBLIC_DSN___",
85
-
send_default_pii=True,
86
-
traces_sample_rate=1.0,
87
100
88
-
#To collect profiles for all profile sessions, set `profile_session_sample_rate` to 1.0.
89
-
profile_session_sample_rate=1.0,
90
-
)
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
103
+
send_default_pii=True,
91
104
92
-
sentry_sdk.profiler.start_profiler()
105
+
traces_sample_rate=1.0,
93
106
94
-
# run some code here
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,
95
112
96
-
sentry_sdk.profiler.stop_profiler()
113
+
# Alternatively, to control sampling dynamically
114
+
profiles_sampler=profiles_sampler
115
+
)
97
116
```
98
117
99
-
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.
118
+
<Alert>
100
119
101
-
```python
102
-
import sentry_sdk
120
+
The <PlatformIdentifiername="profiles_sample_rate" /> setting is _relative_ to the <PlatformIdentifiername="traces_sample_rate" /> setting.
103
121
104
-
sentry_sdk.init(
105
-
dsn="___PUBLIC_DSN___",
106
-
send_default_pii=True,
107
-
traces_sample_rate=1.0,
108
-
# To collect profiles for all profile sessions, set `profile_session_sample_rate` to 1.0.
109
-
profile_session_sample_rate=1.0,
110
-
profile_lifecycle="trace",
111
-
)
112
-
```
122
+
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.
113
123
114
-
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.
124
+
</Alert>
125
+
126
+
### Upgrading from Older Python SDK Versions
115
127
116
-
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="/profiling/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