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
Copy file name to clipboardExpand all lines: docs/platforms/python/tracing/configure-sampling/index.mdx
+83-20Lines changed: 83 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ It is strongly recommended when using a custom `traces_sampler` that you respect
34
34
35
35
</Alert>
36
36
37
-
In distributed systems, implement inheritance logic when trace information is propagated between services. This ensures consistent sampling decisions across your entire distributed trace
37
+
In distributed systems, implementing inheritance logic when trace information is propagated between services will ensure consistent sampling decisions across your entire distributed trace.
if data.get("duration_ms", 0) >2000: # Over 2 seconds
215
258
return1.0
216
259
217
260
# Sample more transactions with high memory usage
261
+
# Note: memory_usage_mb is a custom attribute
218
262
if data.get("memory_usage_mb", 0) >500: # Over 500MB
219
263
return0.8
220
264
221
265
# Sample more transactions with high CPU usage
266
+
# Note: cpu_percent is a custom attribute
222
267
if data.get("cpu_percent", 0) >80: # Over 80% CPU
223
268
return0.8
224
269
225
-
# Sample more transactions with high database load using custom attributes
270
+
# Sample more transactions with high database load
271
+
# Note: db_connections is a custom attribute
226
272
if data.get("db_connections", 0) >100: # Over 100 connections
227
273
return0.7
228
274
229
275
# Default sampling rate
230
276
return0.1
231
-
232
-
sentry_sdk.init(
277
+
278
+
# Initialize the SDK with the sampling function
279
+
sentry_sdk.init(
233
280
dsn="your-dsn",
234
281
traces_sampler=traces_sampler,
235
282
)
283
+
284
+
# To set custom attributes for this example:
285
+
with sentry_sdk.start_transaction(name="Process Data", op="data.process") as transaction:
286
+
# Set custom attributes
287
+
transaction.set_data("duration_ms", 2500) # Custom timing data
288
+
transaction.set_data("memory_usage_mb", 600) # Custom memory data
289
+
transaction.set_data("cpu_percent", 85) # Custom CPU data
290
+
transaction.set_data("db_connections", 120) # Custom database data
291
+
# Your code here
292
+
236
293
```
237
294
</details>
238
295
@@ -243,23 +300,29 @@ When the `traces_sampler` function is called, the Sentry SDK passes a `sampling_
243
300
```python
244
301
{
245
302
"transaction_context": {
246
-
"name": str, # transaction title at creation time
247
-
"op": str, # short description of transaction type, like "http.request"
248
-
"data": Optional[Dict[str, Any]] #other transaction data
303
+
"name": str, # transaction title at creation time (SDK-provided)
304
+
"op": str, # short description of transaction type (SDK-provided)
305
+
"data": Optional[Dict[str, Any]] #custom data you've added to the transaction
249
306
},
250
-
"parent_sampled ": Optional[bool], # whether the parent transaction was sampled, `None` if no parent or if the parent has no sampling decision
251
-
"parent_sample_rate": Optional[float], # the sample rate used by the parent (if any)
252
-
"transaction_context": Optional[Dict[str, Any]], # custom context data
307
+
"parent_sampled": Optional[bool], # whether the parent transaction was sampled (SDK-provided)
308
+
"parent_sample_rate": Optional[float], # the sample rate used by the parent (SDK-provided)
253
309
"custom_sampling_context": Optional[Dict[str, Any]] # additional custom data for sampling
254
310
}
255
311
```
256
312
257
-
<b>Additional common types used in</b> `sampling_context`<b>:</b>
258
-
- str: for text values (names, operations, etc.)
259
-
- bool: for true/false values
260
-
- float: for decimal numbers (like sample rates)
261
-
- Dict[str, Any]: for dictionaries with string keys and any type of values
262
-
- Optional[Type]: for values that might be None
313
+
### SDK-Provided vs. Custom Attributes
314
+
315
+
The sampling context contains both SDK-provided attributes and custom attributes:
316
+
317
+
**SDK-Provided Attributes:**
318
+
-`transaction_context.name`: The name of the transaction
319
+
-`transaction_context.op`: The operation type
320
+
-`parent_sampled`: Whether the parent transaction was sampled
321
+
-`parent_sample_rate`: The sample rate used by the parent
322
+
323
+
**Custom Attributes:**
324
+
- Any data you add to the `set_data` method on the transaction object. Use this for data that you want to include in the transaction data that gets sent to Sentry.
325
+
- Any data you add to the `custom_sampling_context` parameter in `start_transaction`. Use this for data that you want to use for sampling decisions but don't want to include in the transaction data that gets sent to Sentry. Read more about sampling context [here](/platforms/python/configuration/sampling/#sampling-context).
0 commit comments