From 1133cff10ecc8724140913074404b781af8d66f7 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 7 Jul 2025 14:51:35 +0200 Subject: [PATCH 1/3] feat(python): Add a 3.0 version of the sampling page --- .../python/configuration/sampling.mdx | 22 ++- .../python/configuration/sampling__v3.x.mdx | 125 ++++++++++++++++++ .../custom-sampling-context/python.mdx | 24 ++-- 3 files changed, 158 insertions(+), 13 deletions(-) create mode 100644 docs/platforms/python/configuration/sampling__v3.x.mdx diff --git a/docs/platforms/python/configuration/sampling.mdx b/docs/platforms/python/configuration/sampling.mdx index b1846ad87163d..6b5aeda960c03 100644 --- a/docs/platforms/python/configuration/sampling.mdx +++ b/docs/platforms/python/configuration/sampling.mdx @@ -83,7 +83,27 @@ The information contained in the When using custom instrumentation to create a transaction, you can add data to the by passing it as an optional second argument to . This is useful if there's data to which you want the sampler to have access but which you don't want to attach to the transaction as `tags` or `data`, such as information that's sensitive or that’s too large to send with the transaction. For example: - +```python +sentry_sdk.start_transaction( + # kwargs passed to Transaction constructor - will be recorded on transaction + name="GET /search", + op="search", + data={ + "query_params": { + "animal": "dog", + "type": "very good" + } + }, + # `custom_sampling_context` - won't be recorded + custom_sampling_context={ + # PII + "user_id": "12312012", + # too big to send + "search_results": { ... } + } +) +``` + ## Inheritance diff --git a/docs/platforms/python/configuration/sampling__v3.x.mdx b/docs/platforms/python/configuration/sampling__v3.x.mdx new file mode 100644 index 0000000000000..d515e332e2d1e --- /dev/null +++ b/docs/platforms/python/configuration/sampling__v3.x.mdx @@ -0,0 +1,125 @@ +--- +title: Sampling +description: "Learn how to configure the volume of error and transaction events sent to Sentry." +sidebar_order: 60 +--- + +Adding Sentry to your app gives you a great deal of very valuable information about errors and performance you wouldn't otherwise get. And lots of information is good -- as long as it's the right information, at a reasonable volume. + +## Sampling Error Events + +To send a representative sample of your errors to Sentry, set the option in your SDK configuration to a number between `0` (0% of errors sent) and `1` (100% of errors sent). This is a static rate, which will apply equally to all errors. For example, to sample 25% of your errors: + + + +The error sample rate defaults to `1.0`, meaning all errors are sent to Sentry. + +Changing the error sample rate requires re-deployment. In addition, setting an SDK sample rate limits visibility into the source of events. Setting a [rate limit](/pricing/quotas/manage-event-stream-guide/#rate-limiting) for your project (which only drops events when volume is high) may better suit your needs. + +### Dynamically Sampling Error Events + +To sample error events dynamically, set the to a function that returns the desired sample rate for the event. The takes two arguments, and . `event` is the [Event](https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/_types.py) that will be sent to Sentry, `hint` includes Python's [sys.exc_info()](https://docs.python.org/3/library/sys.html#sys.exc_info) information in `hint["exc_info"]`. + + + +Your function **must return a valid value**. A valid value is either: + +- A **floating-point number** between `0.0` and `1.0` (inclusive) indicating the probability an error gets sampled, **or** +- A **boolean** indicating whether or not to sample the error. + + + +One potential use case for the is to apply different sample rates for different exception types. For instance, if you would like to sample some exception called `MyException` at 50%, discard all events of another exception called `MyIgnoredException`, and sample all other exception types at 100%, you could use the following code when initializing the SDK: + + + + + +You can define at most one of the and the . If both are set, the will control sampling, and the will be ignored. + + + +## Sampling Transaction Events + +We recommend sampling your transactions for two reasons: + +1. Capturing a single trace involves minimal overhead, but capturing traces for _every_ page load or _every_ API request may add an undesirable load to your system. +2. Enabling sampling allows you to better manage the number of events sent to Sentry, so you can tailor your volume to your organization's needs. + +Choose a sampling rate with the goal of finding a balance between performance and volume concerns with data accuracy. You don't want to collect _too_ much data, but you want to collect sufficient data from which to draw meaningful conclusions. If you’re not sure what rate to choose, start with a low value and gradually increase it as you learn more about your traffic patterns and volume. + +## Configuring the Transaction Sample Rate + +The Sentry SDKs have two configuration options to control the volume of transactions sent to Sentry, allowing you to take a representative sample: + +1. Uniform sample rate (): + - Provides an even cross-section of transactions, no matter where in your app or under what circumstances they occur. + - Uses default [inheritance](#inheritance) and [precedence](#precedence) behavior +2. Sampling function () which: + - Samples different transactions at different rates + - Filters out some + transactions entirely + - Modifies default [precedence](#precedence) and [inheritance](#inheritance) behavior + +By default, none of these options are set, meaning no transactions will be sent to Sentry. You must set either one of the options to start sending transactions. + +### Setting a Uniform Sample Rate + + + +### Setting a Sampling Function + + + +## Sampling Context Data + +### Default Sampling Context Data + +The information contained in the object passed to the when a transaction is created varies by integration. + + + +### Custom Sampling Context Data + +When using custom instrumentation to create a transaction, you can add data to the by providing additional `attributes` to . All span attributes provided at span start are accessible via the and will also ultimately be sent to Sentry. If you want to exclude an attribute, you can filter it out in a `before_send`. + + + +## Inheritance + +Whatever a transaction's sampling decision, that decision will be passed to its child spans and from there to any transactions they subsequently cause in other services. + +(See Distributed Tracing for more about how that propagation is done.) + +If the transaction currently being created is one of those subsequent transactions (in other words, if it has a parent transaction), the upstream (parent) sampling decision will be included in the sampling context data. Your can use this information to choose whether to inherit that decision. In most cases, inheritance is the right choice, to avoid breaking distributed traces. A broken trace will not include all your services. + + + +In some SDKs, for convenience, the function can return a boolean, so that a parent's decision can be returned directly if that's the desired behavior. + + + +If you're using a rather than a , the decision will always be inherited. + +## Forcing a Sampling Decision + +If you know at transaction creation time whether or not you want the transaction sent to Sentry, you also have the option of passing a sampling decision directly to the transaction constructor (note, not in the object). If you do that, the transaction won't be subject to the , nor will be run, so you can count on the decision that's passed not to be overwritten. + + + +## Precedence + +There are multiple ways for a transaction to end up with a sampling decision. + +- Random sampling according to a static sample rate set in +- Random sampling according to a sample function rate returned by +- Absolute decision (100% chance or 0% chance) returned by +- If the transaction has a parent, inheriting its parent's sampling decision +- Absolute decision passed to + +When there's the potential for more than one of these to come into play, the following precedence rules apply: + +1. If a sampling decision is passed to , that decision will be used, overriding everything else. +2. If is defined, its decision will be used. It can choose to keep or ignore any parent sampling decision, use the sampling context data to make its own decision, or choose a sample rate for the transaction. We advise against overriding the parent sampling decision because it will break distributed traces. +3. If is not defined, but there's a parent sampling decision, the parent sampling decision will be used. +4. If is not defined and there's no parent sampling decision, will be used. diff --git a/platform-includes/performance/custom-sampling-context/python.mdx b/platform-includes/performance/custom-sampling-context/python.mdx index 640b3abdd266d..91744c3b6a754 100644 --- a/platform-includes/performance/custom-sampling-context/python.mdx +++ b/platform-includes/performance/custom-sampling-context/python.mdx @@ -1,20 +1,20 @@ ```python -sentry_sdk.start_transaction( - # kwargs passed to Transaction constructor - will be recorded on transaction +sentry_sdk.start_span( name="GET /search", op="search", data={ - "query_params": { - "animal": "dog", - "type": "very good" - } + "query_params": { + "animal": "dog", + "type": "very good" + } }, - # `custom_sampling_context` - won't be recorded - custom_sampling_context={ - # PII + # Attributes defined at root span start will be accessible in traces_sampler + # and they will be sent to Sentry unless filtered out manually. + # Note that these can only be primitive types or a list of a single primitive + # type. + attributes={ "user_id": "12312012", - # too big to send - "search_results": { ... } + "foo": "bar", } -); +) ``` From 386f922589012ec2be9670503b0c063677fd6d5c Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 7 Jul 2025 14:58:50 +0200 Subject: [PATCH 2/3] feat(python): Add a 3.0 version of the sampling page --- docs/platforms/python/configuration/sampling.mdx | 7 ++++++- docs/platforms/python/configuration/sampling__v3.x.mdx | 8 ++++---- .../performance/force-sampling-decision/python.mdx | 10 ++++++---- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/platforms/python/configuration/sampling.mdx b/docs/platforms/python/configuration/sampling.mdx index 6b5aeda960c03..928bfb6e072b8 100644 --- a/docs/platforms/python/configuration/sampling.mdx +++ b/docs/platforms/python/configuration/sampling.mdx @@ -125,7 +125,12 @@ If you're using a rather than a If you know at transaction creation time whether or not you want the transaction sent to Sentry, you also have the option of passing a sampling decision directly to the transaction constructor (note, not in the object). If you do that, the transaction won't be subject to the , nor will be run, so you can count on the decision that's passed not to be overwritten. - +```python +sentry_sdk.start_transaction( + name="GET /search", + sampled=True +) +``` ## Precedence diff --git a/docs/platforms/python/configuration/sampling__v3.x.mdx b/docs/platforms/python/configuration/sampling__v3.x.mdx index d515e332e2d1e..22eec99af45dd 100644 --- a/docs/platforms/python/configuration/sampling__v3.x.mdx +++ b/docs/platforms/python/configuration/sampling__v3.x.mdx @@ -99,23 +99,23 @@ In some SDKs, for convenience, the -If you're using a rather than a , the decision will always be inherited. +If you're using a rather than a , the decision will always be inherited. The provided will only be used to generate a sample rate if there is sampling decision coming in from upstream. ## Forcing a Sampling Decision -If you know at transaction creation time whether or not you want the transaction sent to Sentry, you also have the option of passing a sampling decision directly to the transaction constructor (note, not in the object). If you do that, the transaction won't be subject to the , nor will be run, so you can count on the decision that's passed not to be overwritten. +If you know at span creation time whether or not you want the root span (transaction) sent to Sentry, you also have the option of passing a sampling decision directly in the `start_span` API. If you do that, the root span won't be subject to the , nor will be run, so you can count on the decision that's passed not to be overwritten. ## Precedence -There are multiple ways for a transaction to end up with a sampling decision. +There are multiple ways for a root span (transaction) to end up with a sampling decision. - Random sampling according to a static sample rate set in - Random sampling according to a sample function rate returned by - Absolute decision (100% chance or 0% chance) returned by - If the transaction has a parent, inheriting its parent's sampling decision -- Absolute decision passed to +- Absolute decision passed to When there's the potential for more than one of these to come into play, the following precedence rules apply: diff --git a/platform-includes/performance/force-sampling-decision/python.mdx b/platform-includes/performance/force-sampling-decision/python.mdx index 9a1f333fbd906..6939f5d20ff45 100644 --- a/platform-includes/performance/force-sampling-decision/python.mdx +++ b/platform-includes/performance/force-sampling-decision/python.mdx @@ -1,6 +1,8 @@ ```python -sentry_sdk.start_transaction( - name="GET /search", - sampled=True -); +sentry_sdk.start_span( + name="GET /search", + # Sample this span. Note that the `sampled` parameter is only taken into + # account for root-level spans (transactions). + sampled=True, +) ``` From 3889322f07ef5085a51c5c180aad92f3cb80a473 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 7 Jul 2025 15:47:05 +0200 Subject: [PATCH 3/3] some transaction -> root span renames --- docs/platforms/python/configuration/sampling__v3.x.mdx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/platforms/python/configuration/sampling__v3.x.mdx b/docs/platforms/python/configuration/sampling__v3.x.mdx index 22eec99af45dd..f6313b14e2d6c 100644 --- a/docs/platforms/python/configuration/sampling__v3.x.mdx +++ b/docs/platforms/python/configuration/sampling__v3.x.mdx @@ -41,7 +41,7 @@ You can define at most one of the an ## Sampling Transaction Events -We recommend sampling your transactions for two reasons: +We recommend sampling your transactions (root spans) for two reasons: 1. Capturing a single trace involves minimal overhead, but capturing traces for _every_ page load or _every_ API request may add an undesirable load to your system. 2. Enabling sampling allows you to better manage the number of events sent to Sentry, so you can tailor your volume to your organization's needs. @@ -75,23 +75,23 @@ By default, none of these options are set, meaning no transactions will be sent ### Default Sampling Context Data -The information contained in the object passed to the when a transaction is created varies by integration. +The information contained in the object passed to the when a root span is created varies by integration. ### Custom Sampling Context Data -When using custom instrumentation to create a transaction, you can add data to the by providing additional `attributes` to . All span attributes provided at span start are accessible via the and will also ultimately be sent to Sentry. If you want to exclude an attribute, you can filter it out in a `before_send`. +When using custom instrumentation to create a root span, you can add data to the by providing additional `attributes` to . All span attributes provided at span start are accessible via the and will also ultimately be sent to Sentry. If you want to exclude an attribute, you can filter it out in a `before_send`. ## Inheritance -Whatever a transaction's sampling decision, that decision will be passed to its child spans and from there to any transactions they subsequently cause in other services. +Whatever a root span's sampling decision, that decision will be passed to its child spans and from there to any root spans they subsequently cause in other services. (See Distributed Tracing for more about how that propagation is done.) -If the transaction currently being created is one of those subsequent transactions (in other words, if it has a parent transaction), the upstream (parent) sampling decision will be included in the sampling context data. Your can use this information to choose whether to inherit that decision. In most cases, inheritance is the right choice, to avoid breaking distributed traces. A broken trace will not include all your services. +If the root span currently being created is one of those subsequent root spans (in other words, if it has a parent root span), the upstream (parent) sampling decision will be included in the sampling context data. Your can use this information to choose whether to inherit that decision. In most cases, inheritance is the right choice, to avoid breaking distributed traces. A broken trace will not include all your services.