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
Select which Sentry features you'd like to install in addition to Error
22
-
Monitoring to get the corresponding installation and configuration
23
-
instructions below.
24
-
</p>
18
+
In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/).
19
+
20
+
Send <PlatformLinkto="/platforms/python/logs/">structured logs</PlatformLink> to Sentry and correlate them with errors and traces.
21
+
22
+
Select which Sentry features you'd like to configure to get the corresponding setup instructions below.
However, in async applications, you need to call `sentry_sdk.init()` inside an `async` function to ensure async code is instrumented properly. We recommend calling `sentry_sdk.init()` at the beginning of the first `async` function you call, as demonstrated in the example below.
72
+
## Step 3: Capture Python Errors
73
+
74
+
The Sentry SDK automatically captures unhandled exceptions. You can also manually capture errors and add context:
75
+
76
+
### Automatic Error Capture
77
+
78
+
```python
79
+
# This will be automatically captured by Sentry
80
+
division_by_zero =1/0
81
+
```
82
+
83
+
### Manual Error Capture
76
84
77
85
```python
78
-
import asyncio
79
86
import sentry_sdk
80
87
81
-
asyncdefmain():
82
-
sentry_sdk.init(
83
-
...# same as above
88
+
try:
89
+
risky_operation()
90
+
exceptExceptionas e:
91
+
# Capture the exception with additional context
92
+
sentry_sdk.capture_exception(e)
93
+
```
94
+
95
+
<Alert>
96
+
97
+
Learn more about manually capturing errors in our <PlatformLinkto="/usage/">Usage documentation</PlatformLink>.
Now let's add structured logging to capture application insights. Logs are enabled in your configuration above.
131
+
132
+
Use Python's logging module to capture structured logs with meaningful attributes that help you debug issues and understand user behavior.
133
+
134
+
```python
135
+
import logging
136
+
137
+
# Configure Python logging
138
+
logger = logging.getLogger(__name__)
139
+
140
+
# Send structured logs with attributes
141
+
logger.info(
142
+
"User completed checkout",
143
+
extra={
144
+
"user_id": 123,
145
+
"order_id": "order_456",
146
+
"amount": 99.99
147
+
}
148
+
)
149
+
150
+
logger.error(
151
+
"Payment processing failed",
152
+
extra={
153
+
"error_code": "CARD_DECLINED",
154
+
"user_id": 123,
155
+
"attempt_count": 3
156
+
}
157
+
)
158
+
159
+
# Log with exception information
160
+
try:
161
+
process_payment()
162
+
except PaymentError as e:
163
+
logger.exception(
164
+
"Payment failed with exception",
165
+
extra={"transaction_id": "txn_789"}
84
166
)
167
+
```
168
+
169
+
</OnboardingOption>
170
+
171
+
## Step 5: Custom Traces with Attributes
85
172
86
-
asyncio.run(main())
173
+
Create custom spans to measure specific operations and add meaningful attributes. This helps you understand performance bottlenecks and debug issues with detailed context.
174
+
175
+
```python
176
+
import sentry_sdk
177
+
178
+
# Create custom spans to measure specific operations
179
+
defprocess_user_data(user_id):
180
+
with sentry_sdk.start_span(
181
+
op="function",
182
+
description="Process User Data",
183
+
data={
184
+
"user_id": user_id,
185
+
"operation": "data_processing",
186
+
"version": "2.1"
187
+
}
188
+
) as span:
189
+
# Your business logic here
190
+
user_data = fetch_user_data(user_id)
191
+
192
+
# Nested span for specific operations
193
+
with sentry_sdk.start_span(
194
+
op="transform",
195
+
description="Transform Data",
196
+
data={
197
+
"record_count": len(user_data),
198
+
"transform_type": "normalize"
199
+
}
200
+
) as inner_span:
201
+
result = transform_user_data(user_data)
202
+
203
+
return result
204
+
205
+
# Add attributes to existing spans
206
+
span = sentry_sdk.get_current_span()
207
+
if span:
208
+
span.set_data("cache_hit", True)
209
+
span.set_data("region", "us-west-2")
210
+
span.set_data("performance_score", 0.95)
87
211
```
88
212
89
-
## Verify
213
+
</OnboardingOption>
90
214
91
-
Add this intentional error to your application to test that everything is working right away.
215
+
## Verify Your Setup
92
216
93
-
```py
94
-
division_by_zero =1/0
217
+
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
218
+
219
+
### Test Error Capturing
220
+
221
+
Add the following test code to one of your Python files to trigger an error that Sentry will capture:
222
+
223
+
```python
224
+
deftrigger_error():
225
+
# ___PRODUCT_OPTION_START___ logs
226
+
division_by_zero =1/0
227
+
# ___PRODUCT_OPTION_END___ logs
228
+
229
+
# Call this function to test
230
+
trigger_error()
95
231
```
96
232
97
-
<Alert>
233
+
Run your application and trigger the error to send it to Sentry.
98
234
99
-
Learn more about manually capturing an error or message in our <PlatformLinkto="/usage/">Usage documentation</PlatformLink>.
To view and resolve the recorded error, log into [sentry.io](https://sentry.io) and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.
238
+
### View Captured Data in Sentry
104
239
105
-
<Alert>
240
+
Now, head over to your project on [Sentry.io](https://sentry.io) to view the collected data (it takes a couple of moments for the data to appear).
106
241
107
-
Not seeing your error in Sentry? Make sure you're running the above example from a file and not from a Python shell like IPython.
At this point, you should have integrated Sentry into your Python application and should already be sending error and performance data to your Sentry project.
247
+
248
+
Now's a good time to customize your setup and look into more advanced topics:
249
+
250
+
- Extend Sentry to your frontend using one of our [JavaScript SDKs](/)
251
+
- Continue to <PlatformLinkto="/configuration">customize your configuration</PlatformLink>
252
+
- Learn about [instrumenting your specific framework](/platforms/python/integrations/)
253
+
- Learn how to <PlatformLinkto="/usage">manually capture errors</PlatformLink>
254
+
- Set up <PlatformLinkto="/configuration/releases/">release tracking</PlatformLink>
0 commit comments