|
| 1 | +--- |
| 2 | +title: Sampling |
| 3 | +description: "Learn how to configure the volume of error and transaction events sent to Sentry." |
| 4 | +sidebar_order: 50 |
| 5 | +--- |
| 6 | + |
| 7 | +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. |
| 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% 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: |
| 12 | + |
| 13 | +<PlatformContent includePath="configuration/sample-rate" /> |
| 14 | + |
| 15 | +The error sample rate defaults to `1`, meaning all errors are sent to Sentry. |
| 16 | + |
| 17 | +<Note> |
| 18 | + |
| 19 | +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 for your project (which only drops events when volume is high) may better suit your needs. |
| 20 | + |
| 21 | +</Note> |
| 22 | + |
| 23 | +## Sampling Transaction Events |
| 24 | + |
| 25 | +We recommend sampling your transactions for two reasons: |
| 26 | + |
| 27 | +1. Capturing a single trace involves minimal overhead, but capturing traces for _every_ level loaded or _every_ API request may add an undesirable load to your system. |
| 28 | + |
| 29 | +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. |
| 30 | + |
| 31 | +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. |
| 32 | + |
| 33 | +## Configuring the Transaction Sample Rate |
| 34 | + |
| 35 | +The Sentry SDKs have two configuration options to control the volume of transactions sent to Sentry, allowing you to take a representative sample: |
| 36 | + |
| 37 | +1. Uniform sample rate (<PlatformIdentifier name="traces-sample-rate" />): |
| 38 | + |
| 39 | + - Provides an even cross-section of transactions, no matter where in your app or under what circumstances they occur. |
| 40 | + - Uses default [inheritance](#inheritance) and [precedence](#precedence) behavior |
| 41 | + |
| 42 | +2. Sampling function (<PlatformIdentifier name="traces-sampler" />) which: |
| 43 | + - Samples different transactions at different rates |
| 44 | + - <PlatformLink to="/configuration/filtering/">Filters</PlatformLink> out some |
| 45 | + transactions entirely |
| 46 | + - Modifies default [precedence](#precedence) and [inheritance](#inheritance) behavior |
| 47 | + |
| 48 | +By default, none of these options are set, meaning no transactions will be sent to Sentry. You must set one of the options to start sending transactions. |
| 49 | + |
| 50 | +### Setting a Uniform Sample Rate |
| 51 | + |
| 52 | +<PlatformContent includePath="performance/uniform-sample-rate" /> |
| 53 | + |
| 54 | +### Setting a Sampling Function |
| 55 | + |
| 56 | +<PlatformContent includePath="performance/sampling-function-intro" /> |
| 57 | + |
| 58 | +## Sampling Context Data |
| 59 | + |
| 60 | +### Default Sampling Context Data |
| 61 | + |
| 62 | +The information contained in the <PlatformIdentifier name="sampling-context" /> object passed to the <PlatformIdentifier name="traces-sampler" /> when a transaction is created varies by platform and integration. |
| 63 | + |
| 64 | +<PlatformContent includePath="performance/default-sampling-context" /> |
| 65 | + |
| 66 | +### Custom Sampling Context Data |
| 67 | + |
| 68 | +When using custom instrumentation to create a transaction, you can add data to the <PlatformIdentifier name="sampling-context" /> by passing it as an optional second argument to <PlatformIdentifier name="start-transaction" />. This is useful if there's data you want the sampler to have access to, but you don't want to attach it 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: |
| 69 | + |
| 70 | +<PlatformContent includePath="performance/custom-sampling-context" /> |
| 71 | + |
| 72 | +## Inheritance |
| 73 | + |
| 74 | +Whatever a transaction's sampling decision, that decision will be passed to its child spans and from there to any transactions they subsequently trigger in other services. |
| 75 | + |
| 76 | +(See <PlatformLink to="/tracing/trace-propagation/">Distributed Tracing</PlatformLink> for more about how that propagation is done.) |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | +If you're using a <PlatformIdentifier name="traces-sample-rate" /> rather than a <PlatformIdentifier name="traces-sampler" />, the decision will always be inherited. |
| 81 | + |
| 82 | +## Precedence |
| 83 | + |
| 84 | +There are multiple ways for a transaction to end up with a sampling decision. |
| 85 | + |
| 86 | +- Random sampling according to a static sample rate set in <PlatformIdentifier name="traces-sample-rate" /> |
| 87 | +- Random sampling according to a sample function rate returned by <PlatformIdentifier name="traces-sampler" /> |
| 88 | +- Absolute decision (100% chance or 0% chance) returned by <PlatformIdentifier name="traces-sampler" /> |
| 89 | +- If the transaction has a parent, inheriting its parent's sampling decision |
| 90 | +- Absolute decision passed to <PlatformIdentifier name="start-transaction" /> |
| 91 | + |
| 92 | +When there's the potential for more than one of these to come into play, the following precedence rules apply: |
| 93 | + |
| 94 | +1. If a sampling decision is passed to <PlatformIdentifier name="start-transaction" />, that decision will be used, overriding everything else. |
| 95 | +1. 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 |
| 96 | +1. If <PlatformIdentifier name="traces-sampler" /> is not defined, but there's a parent sampling decision, the parent sampling decision will be used. |
| 97 | +1. If <PlatformIdentifier name="traces-sampler" /> is not defined and there's no parent sampling decision, <PlatformIdentifier name="traces-sample-rate" /> will be used. |
| 98 | + |
| 99 | +<Alert level="info"> |
| 100 | + |
| 101 | +Currently there is no support for forcing a sampling decision by passing it into <PlatformIdentifier name="start-transaction" />. |
| 102 | + |
| 103 | +</Alert> |
0 commit comments