Skip to content

Commit e9bb6af

Browse files
author
Shannon Anahata
committed
updated examples based on feedback. Reverted yarn.lock change
1 parent 6de450f commit e9bb6af

File tree

4 files changed

+307
-444
lines changed

4 files changed

+307
-444
lines changed

docs/platforms/python/tracing/configure-sampling/index.mdx

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ It is strongly recommended when using a custom `traces_sampler` that you respect
3434

3535
</Alert>
3636

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.
3838

3939
<PlatformContent includePath="/performance/traces-sampler-as-sampler" />
4040

@@ -94,7 +94,8 @@ def traces_sampler(sampling_context):
9494
if environment == "development":
9595
return 1.0
9696

97-
# Sample more transactions if there are recent errors by using custom attributes
97+
# Sample more transactions if there are recent errors
98+
# Note: hasRecentErrors is a custom attribute that needs to be set
9899
if ctx.get("data", {}).get("hasRecentErrors"):
99100
return 0.8
100101

@@ -107,10 +108,23 @@ def traces_sampler(sampling_context):
107108
# Default sampling rate
108109
return 0.1
109110

111+
# Initialize the SDK with the sampling function
110112
sentry_sdk.init(
111113
dsn="your-dsn",
112114
traces_sampler=traces_sampler,
113115
)
116+
117+
# You can use the sampling function by setting custom attributes:
118+
# Option 1: When creating the transaction
119+
with sentry_sdk.start_transaction(name="GET /api/users", op="http.request") as transaction:
120+
# Set custom attribute
121+
transaction.set_data("hasRecentErrors", True)
122+
# Your code here
123+
124+
# Option 2: During the transaction's lifecycle
125+
with sentry_sdk.start_transaction(name="GET /api/users", op="http.request") as transaction:
126+
# Your code here
127+
transaction.set_data("hasRecentErrors", True) # Set custom attribute
114128
```
115129

116130
3. Controlling Sampling Based on User and Transaction Properties
@@ -128,28 +142,42 @@ def traces_sampler(sampling_context):
128142
data = ctx.get("data", {})
129143

130144
# Always sample for premium users
145+
# Note: user.tier is a custom attribute that needs to be set
131146
if data.get("user", {}).get("tier") == "premium":
132147
return 1.0
133148

134149
# Sample more transactions for users experiencing errors
150+
# Note: hasRecentErrors is a custom attribute
135151
if data.get("hasRecentErrors"):
136152
return 0.8
137153

138154
# Sample less for high-volume, low-value paths
139-
if (ctx.get("name") or "").startswith("/api/metrics"):
155+
# Note: name is an SDK-provided attribute
156+
if (ctx.get("name") or "").startswith("/api/metrics"):
140157
return 0.01
141158

142159
# Sample more for slow transactions
160+
# Note: duration_ms is a custom attribute
143161
if data.get("duration_ms", 0) > 1000: # Transactions over 1 second
144162
return 0.5
145163

146164
# Default sampling rate
147165
return 0.2
148-
166+
167+
# Initialize the SDK with the sampling function
149168
sentry_sdk.init(
150169
dsn="your-dsn",
151170
traces_sampler=traces_sampler,
152171
)
172+
173+
# To set custom attributes for this example:
174+
with sentry_sdk.start_transaction(name="GET /api/users", op="http.request") as transaction:
175+
# Set custom attributes
176+
transaction.set_data("user", {"tier": "premium"}) # Custom user data
177+
transaction.set_data("hasRecentErrors", True) # Custom error flag
178+
transaction.set_data("duration_ms", 1500) # Custom timing data
179+
# Your code here
180+
153181
```
154182

155183
4. Complex Business Logic Sampling
@@ -167,33 +195,47 @@ def traces_sampler(sampling_context):
167195
data = ctx.get("data", {})
168196

169197
# Always sample critical business operations
198+
# Note: op is an SDK-provided attribute
170199
if ctx.get("op") in ["payment.process", "order.create", "user.verify"]:
171200
return 1.0
172201

173202
# Sample based on user segment
203+
# Note: user.segment is a custom attribute
174204
user_segment = data.get("user", {}).get("segment")
175205
if user_segment == "enterprise":
176206
return 0.8
177207
elif user_segment == "premium":
178208
return 0.5
179209

180210
# Sample based on transaction value
211+
# Note: transaction.value is a custom attribute
181212
transaction_value = data.get("transaction", {}).get("value", 0)
182213
if transaction_value > 1000: # High-value transactions
183214
return 0.7
184215

185216
# Sample based on error rate in the service
217+
# Note: service.error_rate is a custom attribute
186218
error_rate = data.get("service", {}).get("error_rate", 0)
187219
if error_rate > 0.05: # Error rate above 5%
188220
return 0.9
189221

190222
# Default sampling rate
191223
return 0.1
192-
224+
225+
# Initialize the SDK with the sampling function
193226
sentry_sdk.init(
194227
dsn="your-dsn",
195228
traces_sampler=traces_sampler,
196229
)
230+
231+
s# To set custom attributes for this example:
232+
with sentry_sdk.start_transaction(name="Process Payment", op="payment.process") as transaction:
233+
# Set custom attributes
234+
transaction.set_data("user", {"segment": "enterprise"}) # Custom user data
235+
transaction.set_data("transaction", {"value": 1500}) # Custom transaction data
236+
transaction.set_data("service", {"error_rate": 0.03}) # Custom service data
237+
# Your code here
238+
197239
```
198240

199241
5. Performance-Based Sampling
@@ -211,28 +253,43 @@ def traces_sampler(sampling_context):
211253
data = ctx.get("data", {})
212254

213255
# Sample all slow transactions
256+
# Note: duration_ms is a custom attribute
214257
if data.get("duration_ms", 0) > 2000: # Over 2 seconds
215258
return 1.0
216259

217260
# Sample more transactions with high memory usage
261+
# Note: memory_usage_mb is a custom attribute
218262
if data.get("memory_usage_mb", 0) > 500: # Over 500MB
219263
return 0.8
220264

221265
# Sample more transactions with high CPU usage
266+
# Note: cpu_percent is a custom attribute
222267
if data.get("cpu_percent", 0) > 80: # Over 80% CPU
223268
return 0.8
224269

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
226272
if data.get("db_connections", 0) > 100: # Over 100 connections
227273
return 0.7
228274

229275
# Default sampling rate
230276
return 0.1
231-
232-
sentry_sdk.init(
277+
278+
# Initialize the SDK with the sampling function
279+
sentry_sdk.init(
233280
dsn="your-dsn",
234281
traces_sampler=traces_sampler,
235282
)
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+
236293
```
237294
</details>
238295

@@ -243,23 +300,29 @@ When the `traces_sampler` function is called, the Sentry SDK passes a `sampling_
243300
```python
244301
{
245302
"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
249306
},
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)
253309
"custom_sampling_context": Optional[Dict[str, Any]] # additional custom data for sampling
254310
}
255311
```
256312

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).
263326

264327
## Sampling Decision Precedence
265328

docs/platforms/python/tracing/span-lifecycle/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The following options can be used when creating spans:
6464
| ------------- | --------------- | ----------------------------------------------- |
6565
| `op` | `string` | The operation of the span. |
6666
| `name` | `string` | The name of the span. |
67-
| `start_time` | `datetime/float`| The start time of the span. |
67+
| `start_timestamp` | `datetime/float`| The start time of the span. |
6868

6969
## Using the Context Manager
7070

docs/platforms/python/tracing/troubleshooting/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ For example, a 200+ character tag like this:
6060

6161
`https://empowerplant.io/api/0/projects/ep/setup_form/?user_id=314159265358979323846264338327&tracking_id=EasyAsABC123OrSimpleAsDoReMi&product_name=PlantToHumanTranslator&product_id=1618033988749894848`
6262

63-
Instead, using `span.set_tag` and `span.set_data` preserves the details:
63+
Using `span.set_tag` for shorter values, in combination with `span.set_data` maintains the details.
6464

6565
```python
6666
import sentry_sdk
@@ -76,7 +76,7 @@ parameters = {
7676
"product_id": 161803398874989484820458683436563811772030917980576,
7777
}
7878

79-
with sentry_sdk.start_span(op="request", transaction="setup form") as span:
79+
with sentry_sdk.start_span(op="request", name="setup form") as span:
8080
span.set_tag("base_url", base_url)
8181
span.set_tag("endpoint", endpoint)
8282
span.set_data("parameters", parameters)

0 commit comments

Comments
 (0)