Skip to content

Commit d808dd9

Browse files
author
Shannon Anahata
committed
break out span lifecycle doc and resolve feedback from PR
1 parent f69c5ea commit d808dd9

File tree

8 files changed

+356
-387
lines changed

8 files changed

+356
-387
lines changed

docs/platforms/python/profiling/index.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ For Profiling to work, you have to first enable [Sentry’s tracing](/concepts/k
5151

5252
</Alert>
5353

54-
### Upgrading from Older Python SDK Versions
55-
56-
Profiling was experimental in SDK versions `1.17.0` and older. Learn how to upgrade <PlatformLink to="/troubleshooting">here</PlatformLink>.
57-
5854
## Enable Continuous Profiling
5955

6056
<Include name="feature-stage-beta.mdx" />

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

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,53 @@
11
---
22
title: Configure Sampling
33
description: "Learn how to configure sampling in your app."
4-
sidebar_order: 30
4+
sidebar_order: 40
55
---
66

7-
Sentry's tracing functionality helps you monitor application performance by capturing distributed traces, attaching attributes, and adding span performance metrics across your application. However, capturing traces for every transaction can generate significant volumes of data. Sampling allows you to control the amount of spans that are sent to Sentry from your application.
7+
If you find that Sentry's tracing functionality is generating too much data, for example, if you notice your spans quota is quickly being exhausted, you can choose to sample your traces.
88

9-
Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The `traces_sampler` function gives you precise control over which transactions to record, allowing you to focus on the most important parts of your application.
9+
Effective sampling is key to getting the most value from Sentry's performance monitoring while minimizing overhead. The Python SDK provides two ways to control the sampling rate. You can review the options and [examples](#trace-sampler-examples) below.
1010

1111
## Sampling Configuration Options
1212

13-
The Python SDK provides two main options for controlling the sampling rate:
13+
### 1. Uniform Sample Rate (`traces_sample_rate`)
1414

15-
1. Uniform Sample Rate (`traces_sample_rate`)
16-
17-
This option sets a fixed percentage of transactions to be captured:
15+
`traces_sample_rate` is a floating-point value between `0.0` and `1.0`, inclusive, which controls the probability with which each transaction will be sampled:
1816

1917
<PlatformContent includePath="/performance/traces-sample-rate" />
2018

21-
With `traces_sample_rate` set to `0.25`, approximately 25% of transactions will be recorded and sent to Sentry. This provides an even cross-section of transactions regardless of where in your app they occur.
19+
With `traces_sample_rate` set to `0.25`, each transaction in your application is randomly sampled with a probability of `0.25`, so you can expect that one in every four transactions will be sent to Sentry.
2220

23-
2. Sampling Function (`traces_sampler`)
21+
### 2. Sampling Function (`traces_sampler`)
2422

25-
For more granular control, you can use the `traces_sampler` function. This approach allows you to:
23+
For more granular control, you can provide a `traces_sampler` function. This approach allows you to:
2624

2725
- Apply different sampling rates to different types of transactions
2826
- Filter out specific transactions entirely
2927
- Make sampling decisions based on transaction data
3028
- Control the inheritance of sampling decisions in distributed traces
3129

30+
<Alert>
31+
32+
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+
</Alert>
35+
36+
```python
37+
# Use the parent sampling decision if we have an incoming trace.
38+
# Note: we strongly recommend respecting the parent sampling decision,
39+
# as this ensures your traces will be complete!
40+
parent_sampling_decision = sampling_context.get("parent_sampled")
41+
if parent_sampling_decision is not None:
42+
return float(parent_sampling_decision)
43+
```
44+
3245
<PlatformContent includePath="/performance/traces-sampler-as-sampler" />
3346

34-
### Trace Sampler Examples
47+
<details>
48+
<summary className="text-xl font-semibold">Trace Sampler Examples</summary>
49+
50+
#### Trace Sampler Examples
3551

3652
1. Prioritizing Critical User Flows
3753

@@ -196,6 +212,7 @@ sentry_sdk.init(
196212
traces_sampler=traces_sampler,
197213
)
198214
```
215+
</details>
199216

200217
## The Sampling Context Object
201218

@@ -204,22 +221,23 @@ When the `traces_sampler` function is called, the Sentry SDK passes a `sampling
204221
```python
205222
{
206223
"transaction_context": {
207-
"name": str, # transaction title at creation time
208-
"op": str, # short description of transaction type, like "http.request"
209-
# other transaction data...
224+
"name": str, # transaction title at creation time
225+
"op": str, # short description of transaction type, like "http.request"
226+
"data": Optional[Dict[str, Any]] # other transaction data
210227
},
211-
"parent_sampled": bool, # whether the parent transaction was sampled (if any)
212-
"parent_sample_rate": float, # the sample rate used by the parent (if any)
213-
# Custom context as passed to start_transaction
228+
"parent_sampled ": Optional[bool] | None, # whether the parent transaction was sampled, `None` if no parent
229+
"parent_sample_rate": Optional[float], # the sample rate used by the parent (if any)
230+
"transaction_context": Optional[Dict[str, Any]], # custom context data
231+
"custom_sampling_context": Optional[Dict[str, Any]] # additional custom data for sampling
214232
}
215233
```
216234

217-
The sampling context contains:
218-
219-
- `transaction_context`: Includes the transaction name, operation type, and other metadata
220-
- `parent_sampled`: Whether the parent transaction was sampled (for distributed tracing)
221-
- `parent_sample_rate`: The sample rate used in the parent transaction
222-
- Any custom sampling context data passed to `start_transaction`
235+
<b>Additional common types used in</b> `sampling_context`<b>:</b>
236+
- str: for text values (names, operations, etc.)
237+
- bool: for true/false values
238+
- float: for decimal numbers (like sample rates)
239+
- Dict[str, Any]: for dictionaries with string keys and any type of values
240+
- Optional[Type]: for values that might be None
223241

224242
## Inheritance in Distributed Tracing
225243

@@ -250,39 +268,20 @@ This approach ensures consistent sampling decisions across your entire distribut
250268
When multiple sampling mechanisms could apply, Sentry follows this order of precedence:
251269

252270
1. If a sampling decision is passed to `start_transaction`, that decision is used
253-
2. If `traces_sampler` is defined, its decision is used (can consider parent sampling)
254-
3. If no `traces_sampler` but parent sampling is available, parent decision is used
271+
2. If `traces_sampler` is defined, its decision is used. Although the `traces_sampler` can override the parent sampling decision, most users will want to ensure their `traces_sampler` respects the parent sampling decision
272+
3. If no `traces_sampler` is defined, but there is a parent sampling decision from an incoming distributed trace, we use the parent sampling decision
255273
4. If neither of the above, `traces_sample_rate` is used
256-
5. If none of the above are set, no transactions are sampled (0%)
274+
5. If none of the above are set, no transactions are sampled. This is equivalent to setting `traces_sample_rate=0.0`
257275

258276
## How Sampling Propagates in Distributed Traces
259277

260278
Sentry uses a "head-based" sampling approach:
261279

262280
- A sampling decision is made in the originating service (the "head")
263-
- This decision is propagated to all downstream services via HTTP headers
281+
- This decision is propagated to all downstream services
264282

265283
The two key headers are:
266284
- `sentry-trace`: Contains trace ID, span ID, and sampling decision
267285
- `baggage`: Contains additional trace metadata including sample rate
268286

269-
The Sentry Python SDK automatically attaches these headers to outgoing HTTP requests when using auto-instrumentation with libraries like `requests`, `urllib3`, or `httpx`. For other communication channels, you can manually propagate trace information:
270-
271-
```python
272-
# Extract trace data from the current scope
273-
trace_data = sentry_sdk.get_current_scope().get_trace_context()
274-
sentry_trace_header = trace_data.get("sentry-trace")
275-
baggage_header = trace_data.get("baggage")
276-
277-
# Add to your custom request (example using a message queue)
278-
message = {
279-
"data": "Your data here",
280-
"metadata": {
281-
"sentry_trace": sentry_trace_header,
282-
"baggage": baggage_header
283-
}
284-
}
285-
queue.send(json.dumps(message))
286-
```
287-
288-
By implementing a thoughtful sampling strategy, you'll get the performance insights you need without overwhelming your systems or your Sentry quota.
287+
The Sentry Python SDK automatically attaches these headers to outgoing HTTP requests when using auto-instrumentation with libraries like `requests`, `urllib3`, or `httpx`. For other communication channels, you can manually propagate trace information. Learn more about customizing tracing in [custom trace propagation](/platforms/python/tracing/distributed-tracing/custom-trace-propagation/).

docs/platforms/python/tracing/distributed-tracing/custom-instrumentation/index.mdx renamed to docs/platforms/python/tracing/distributed-tracing/custom-trace-propagation/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Custom Instrumentation
2+
title: Custom Trace Propagation
33
sidebar_order: 10
44
---
55

0 commit comments

Comments
 (0)