@@ -41,11 +41,11 @@ import sentry_sdk
41
41
42
42
with sentry_sdk.start_span(op = " process" , name = " Process Data" ):
43
43
# This code is tracked in the "Process Data" span
44
-
44
+
45
45
with sentry_sdk.start_span(op = " task" , name = " Validate Input" ):
46
46
# This is now a child span of "Process Data"
47
47
validate_data()
48
-
48
+
49
49
with sentry_sdk.start_span(op = " task" , name = " Transform Data" ):
50
50
# Another child span
51
51
transform_data()
@@ -71,7 +71,7 @@ import sentry_sdk
71
71
with sentry_sdk.start_span(op = " db" , name = " Query Users" ) as span:
72
72
# Perform a database query
73
73
users = db.query(" SELECT * FROM users" )
74
-
74
+
75
75
# You can set data on the span
76
76
span.set_data(" user_count" , len (users))
77
77
```
@@ -113,7 +113,7 @@ import sentry_sdk
113
113
114
114
with sentry_sdk.start_transaction(name = " Background Task" , op = " task" ) as transaction:
115
115
# Your code here
116
-
116
+
117
117
# You can add child spans to the transaction
118
118
with sentry_sdk.start_span(op = " subtask" , name = " Data Processing" ):
119
119
# Process data
@@ -132,7 +132,7 @@ import sentry_sdk
132
132
with sentry_sdk.start_span(op = " db" , name = " Query Users" ) as span:
133
133
# Execute the query
134
134
users = db.query(" SELECT * FROM users WHERE active = true" )
135
-
135
+
136
136
# You can add more data during execution
137
137
span.set_data(" result_count" , len (users))
138
138
```
@@ -156,32 +156,33 @@ To add attributes to all spans, use the `before_send_transaction` callback:
156
156
157
157
``` python
158
158
import sentry_sdk
159
+ from sentry_sdk.types import Event, Hint
159
160
160
- def before_send_transaction (event ) :
161
+ def before_send_transaction (event : Event, hint : Hint) -> Event | None :
161
162
# Add attributes to the root span (transaction)
162
163
if " trace" in event.get(" contexts" , {}):
163
164
if " data" not in event[" contexts" ][" trace" ]:
164
165
event[" contexts" ][" trace" ][" data" ] = {}
165
-
166
+
166
167
event[" contexts" ][" trace" ][" data" ].update({
167
168
" app_version" : " 1.2.3" ,
168
169
" environment_region" : " us-west-2"
169
170
})
170
-
171
+
171
172
# Add attributes to all child spans
172
173
for span in event.get(" spans" , []):
173
174
if " data" not in span:
174
175
span[" data" ] = {}
175
-
176
+
176
177
span[" data" ].update({
177
178
" component_version" : " 2.0.0" ,
178
179
" deployment_stage" : " production"
179
180
})
180
-
181
+
181
182
return event
182
183
183
184
sentry_sdk.init(
184
- # Your other Sentry configuration options here
185
+ # ...
185
186
before_send_transaction = before_send_transaction
186
187
)
187
188
```
@@ -202,7 +203,7 @@ with sentry_sdk.start_span(op="http.client", name="Fetch User Data"):
202
203
# Database operation
203
204
with sentry_sdk.start_span(op = " db" , name = " Save User" ):
204
205
db.execute(
205
- " INSERT INTO users (name, email) VALUES (%s , %s )" ,
206
+ " INSERT INTO users (name, email) VALUES (%s , %s )" ,
206
207
(user.name, user.email),
207
208
)
208
209
@@ -233,4 +234,3 @@ with sentry_sdk.start_span(op="task", name="Process Payment") as span:
233
234
# Span will automatically be marked as failed when an exception occurs
234
235
raise
235
236
```
236
-
0 commit comments