|
| 1 | +--- |
| 2 | +title: Sampling |
| 3 | +description: "Learn how to configure the volume of error and transaction events sent to Sentry." |
| 4 | +sidebar_order: 60 |
| 5 | +--- |
| 6 | + |
| 7 | +Adding Sentry to your app gives you a great deal of 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. |
| 8 | + |
| 9 | +## Sampling Error Events |
| 10 | + |
| 11 | +To send a representative sample of your errors to Sentry, set the <PlatformIdentifier name="sample-rate" /> option in your SDK configuration to a number between `0.0` (0% of errors sent) and `1.0` (100% of errors sent). This is a static rate, which will apply equally to all errors. For example, to sample 25% of your errors: |
| 12 | + |
| 13 | +<PlatformContent includePath="configuration/sample-rate" /> |
| 14 | + |
| 15 | +The error sample rate defaults to `1.0`, meaning all errors are sent to Sentry. |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +### Dynamically Sampling Error Events |
| 20 | + |
| 21 | +To sample error events dynamically, set the <PlatformIdentifier name="error-sampler" /> to a function that returns the desired sample rate for the event. The <PlatformIdentifier name="error-sampler" /> takes two arguments, <PlatformIdentifier name="event" /> and <PlatformIdentifier name="hint" />. `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"]`. |
| 22 | + |
| 23 | +<Alert> |
| 24 | + |
| 25 | +Your <PlatformIdentifier name="error-sampler" /> function **must return a valid value**. A valid value is either: |
| 26 | + |
| 27 | +- A **floating-point number** between `0.0` and `1.0` (inclusive) indicating the probability an error gets sampled, **or** |
| 28 | +- A **boolean** indicating whether or not to sample the error. |
| 29 | + |
| 30 | +</Alert> |
| 31 | + |
| 32 | +One potential use case for the <PlatformIdentifier name="error-sampler" /> 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: |
| 33 | + |
| 34 | +<PlatformContent includePath="configuration/error-sampler" /> |
| 35 | + |
| 36 | +<Alert> |
| 37 | + |
| 38 | +You can define at most one of the <PlatformIdentifier name="error-sampler" /> and the <PlatformIdentifier name="sample-rate" />. If both are set, the <PlatformIdentifier name="error-sampler" /> will control sampling, and the <PlatformIdentifier name="sample-rate" /> will be ignored. |
| 39 | + |
| 40 | +</Alert> |
| 41 | + |
| 42 | +## Sampling Transaction Events |
| 43 | + |
| 44 | +We recommend sampling your transactions for two reasons: |
| 45 | + |
| 46 | +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. |
| 47 | +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. |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +## Configuring the Transaction Sample Rate |
| 52 | + |
| 53 | +The Sentry SDKs have two configuration options to control the volume of transactions sent to Sentry, allowing you to take a representative sample: |
| 54 | + |
| 55 | +1. Uniform sample rate (<PlatformIdentifier name="traces-sample-rate" />): |
| 56 | + - Provides an even cross-section of transactions, no matter where in your app or under what circumstances they occur. |
| 57 | + - Uses default [inheritance](#inheritance) and [precedence](#precedence) behavior |
| 58 | +2. Sampling function (<PlatformIdentifier name="traces-sampler" />) which: |
| 59 | + - Samples different transactions at different rates |
| 60 | + - <PlatformLink to="/configuration/filtering/">Filters</PlatformLink> out some |
| 61 | + transactions entirely |
| 62 | + - Modifies default [precedence](#precedence) and [inheritance](#inheritance) behavior |
| 63 | + |
| 64 | +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. |
| 65 | + |
| 66 | +### Setting a Uniform Sample Rate |
| 67 | + |
| 68 | +<PlatformContent includePath="performance/uniform-sample-rate" /> |
| 69 | + |
| 70 | +### Setting a Sampling Function |
| 71 | + |
| 72 | +<PlatformContent includePath="performance/sampling-function-intro" /> |
| 73 | + |
| 74 | +## Sampling Context Data |
| 75 | + |
| 76 | +### Default Sampling Context Data |
| 77 | + |
| 78 | +The information contained in the <PlatformIdentifier name="sampling-context" /> object passed to the <PlatformIdentifier name="traces-sampler" /> when a transaction is created varies by integration. |
| 79 | + |
| 80 | +<PlatformContent includePath="performance/default-sampling-context" /> |
| 81 | + |
| 82 | +### Custom Sampling Context Data |
| 83 | + |
| 84 | +When using custom instrumentation to create a transaction, you can add data to the <PlatformIdentifier name="sampling-context" /> by providing additional `attributes` to <PlatformIdentifier name="start-span" />. |
| 85 | + |
| 86 | +<PlatformContent includePath="performance/custom-sampling-context" /> |
| 87 | + |
| 88 | +All span attributes provided at span start are accessible via the <PlatformIdentifier name="sampling-context" /> and will also ultimately be sent to Sentry. If you want to exclude an attribute, you can filter it out in a <PlatformLink to="/configuration/filtering">`before_send`</PlatformLink>. |
| 89 | + |
| 90 | +## Inheritance |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +(See <PlatformLink to="/tracing/trace-propagation/">Distributed Tracing</PlatformLink> for more about how that propagation is done.) |
| 95 | + |
| 96 | +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 <PlatformIdentifier name="traces-sampler" /> 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. |
| 97 | + |
| 98 | +<PlatformContent includePath="performance/always-inherit-sampling-decision"> |
| 99 | + |
| 100 | +In some SDKs, for convenience, the <PlatformIdentifier name="traces-sampler" /> function can return a boolean, so that a parent's decision can be returned directly if that's the desired behavior. |
| 101 | + |
| 102 | +</PlatformContent> |
| 103 | + |
| 104 | +If you're using a <PlatformIdentifier name="traces-sample-rate" /> rather than a <PlatformIdentifier name="traces-sampler" />, the decision will always be inherited. The provided <PlatformIdentifier name="traces-sample-rate" /> will only be used to generate a sample rate if there is no sampling decision coming in from upstream. |
| 105 | + |
| 106 | +## Forcing a Sampling Decision |
| 107 | + |
| 108 | +If you know at span creation time whether or not you want the 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 transaction won't be subject to the <PlatformIdentifier name="traces-sample-rate" />, nor will <PlatformIdentifier name="traces-sampler" /> be run, so you can count on the decision that's passed not to be overwritten. |
| 109 | + |
| 110 | +<PlatformContent includePath="performance/force-sampling-decision" /> |
| 111 | + |
| 112 | +## Precedence |
| 113 | + |
| 114 | +There are multiple ways for a transaction to end up with a sampling decision. |
| 115 | + |
| 116 | +- Random sampling according to a static sample rate set in <PlatformIdentifier name="traces-sample-rate" /> |
| 117 | +- Random sampling according to a sample function rate returned by <PlatformIdentifier name="traces-sampler" /> |
| 118 | +- Absolute decision (100% chance or 0% chance) returned by <PlatformIdentifier name="traces-sampler" /> |
| 119 | +- If the transaction has a parent, inheriting its parent's sampling decision |
| 120 | +- Absolute decision passed to <PlatformIdentifier name="start-span" /> |
| 121 | + |
| 122 | +When there's the potential for more than one of these to come into play, the following precedence rules apply: |
| 123 | + |
| 124 | +1. If a sampling decision is passed to <PlatformIdentifier name="start-transaction" />, that decision will be used, overriding everything else. |
| 125 | +2. If <PlatformIdentifier name="traces-sampler" /> 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. |
| 126 | +3. If <PlatformIdentifier name="traces-sampler" /> is not defined, but there's a parent sampling decision, the parent sampling decision will be used. |
| 127 | +4. If <PlatformIdentifier name="traces-sampler" /> is not defined and there's no parent sampling decision, <PlatformIdentifier name="traces-sample-rate" /> will be used. |
0 commit comments