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
+12-40Lines changed: 12 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,13 +26,16 @@ For more granular control, you can provide a `traces_sampler` function. This app
26
26
- Filter out specific transactions entirely
27
27
- Make sampling decisions based on transaction data
28
28
- Control the inheritance of sampling decisions in distributed traces
29
+
- Use custom attributes to modify sampling
29
30
30
31
<Alert>
31
32
32
33
It is strongly recommended when using a custom `traces_sampler` that you respect the parent sampling decision. This ensures your traces will be complete.
33
34
34
35
</Alert>
35
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
# Sample more transactions with high database load
225
+
# Sample more transactions with high database load using custom attributes
230
226
if data.get("db_connections", 0) >100: # Over 100 connections
231
227
return0.7
232
228
@@ -242,7 +238,7 @@ sentry_sdk.init(
242
238
243
239
## The Sampling Context Object
244
240
245
-
When the `traces_sampler` function is called, the Sentry SDK passes a `sampling_context` object with information from the relevant span to help make sampling decisions:
241
+
When the `traces_sampler` function is called, the Sentry SDK passes a `sampling_context` object with information from the relevant span to help make sampling decisions:
246
242
247
243
```python
248
244
{
@@ -251,7 +247,7 @@ When the `traces_sampler` function is called, the Sentry SDK passes a `sampling
251
247
"op": str, # short description of transaction type, like "http.request"
252
248
"data": Optional[Dict[str, Any]] # other transaction data
253
249
},
254
-
"parent_sampled ": Optional[bool]|None, # whether the parent transaction was sampled, `None` if no parent
250
+
"parent_sampled ": Optional[bool], # whether the parent transaction was sampled, `None` if no parent or if the parent has no sampling decision
255
251
"parent_sample_rate": Optional[float], # the sample rate used by the parent (if any)
256
252
"transaction_context": Optional[Dict[str, Any]], # custom context data
257
253
"custom_sampling_context": Optional[Dict[str, Any]] # additional custom data for sampling
@@ -265,30 +261,6 @@ When the `traces_sampler` function is called, the Sentry SDK passes a `sampling
265
261
- Dict[str, Any]: for dictionaries with string keys and any type of values
266
262
- Optional[Type]: for values that might be None
267
263
268
-
## Inheritance in Distributed Tracing
269
-
270
-
In distributed systems, trace information is propagated between services. You can implement inheritance logic like this:
271
-
272
-
```python
273
-
deftraces_sampler(sampling_context):
274
-
# Examine provided context data
275
-
if"transaction_context"in sampling_context:
276
-
name = sampling_context["transaction_context"].get("name", "")
277
-
278
-
# Apply specific rules first
279
-
if"critical-path"in name:
280
-
return1.0# Always sample
281
-
282
-
# Inherit parent sampling decision if available
283
-
if sampling_context.get("parent_sampled") isnotNone:
284
-
return sampling_context["parent_sampled"]
285
-
286
-
# Otherwise use a default rate
287
-
return0.1
288
-
```
289
-
290
-
This approach ensures consistent sampling decisions across your entire distributed trace. All transactions in a given trace will share the same sampling decision, preventing broken or incomplete traces.
291
-
292
264
## Sampling Decision Precedence
293
265
294
266
When multiple sampling mechanisms could apply, Sentry follows this order of precedence:
Copy file name to clipboardExpand all lines: docs/platforms/python/tracing/span-lifecycle/index.mdx
+20-67Lines changed: 20 additions & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ To capture transactions and spans customized to your organization's needs, you m
10
10
11
11
</Alert>
12
12
13
-
To add custom attributes or performance data to your applications traces, you need to attach these attributes to the trace's [spans](/concepts/key-terms/tracing/distributed-tracing/#traces-transactions-and-spans). Spans represent the individual actions that occur within an overall application interaction. This might include items loading, database calls, API returns, or more.
13
+
To add custom performance data to your application, you need to add custom instrumentation in the form of [spans](/concepts/key-terms/tracing/distributed-tracing/#traces-transactions-and-spans). Spans are a way to measure the time it takes for a specific action to occur. For example, you can create a span to measure the time it takes for a function to execute.
You can access the currently active span using `sentry_sdk.get_current_span()`:
@@ -155,7 +106,7 @@ import sentry_sdk
155
106
# Get the current active span
156
107
current_span = sentry_sdk.get_current_span()
157
108
if current_span:
158
-
current_span.set_attribute("key", "value")
109
+
current_span.set_data("key", "value")
159
110
```
160
111
161
112
## Working with Transactions
@@ -174,6 +125,10 @@ with sentry_sdk.start_transaction(name="Background Task", op="task") as transact
174
125
pass
175
126
```
176
127
128
+
## Distributed Tracing
129
+
130
+
See <PlatformLinkto="/tracing/distributed-tracing/">Distributed Tracing</PlatformLink> for details on how to propagate trace information across different services or applications.
131
+
177
132
## Improving Span Data
178
133
179
134
### Adding Span Attributes
@@ -183,16 +138,12 @@ Span attributes customize information you can get through tracing. This informat
183
138
```python
184
139
import sentry_sdk
185
140
186
-
with sentry_sdk.start_span(op="db", name="Query Users", attributes={
187
-
"db.query": "SELECT * FROM users WHERE active = true",
188
-
"db.system": "postgresql",
189
-
"server.address": "db.example.com"
190
-
}) as span:
141
+
with sentry_sdk.start_span(op="db", name="Query Users") as span:
191
142
# Execute the query
192
143
users = db.query("SELECT * FROM users WHERE active = true")
0 commit comments