# Sampling

## Learn how to configure the volume of error and transaction events sent to Sentry.

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](#sampling-error-events)

To send a representative sample of your errors to Sentry, set the `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:

```c
sentry_options_t *options = sentry_options_new();
sentry_options_set_sample_rate(0.25);
sentry_init(options);
```

The error sample rate defaults to `1`, 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 for your project (which only drops events when volume is high) may better suit your needs.

## [Sampling Transaction Events](#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](#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 (`traces_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 (`traces_sampler`) which:

   * Samples different transactions at different rates
   * [Filters](/platforms/native/configuration/filtering/) 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-uniform-sample-rate)

To do this, set the `traces_sample_rate` option to a number between 0 and 1. With this option set, every transaction created will have that percentage chance of being sent to Sentry. For example, if you set `traces_sample_rate` to `0.2`, approximately 20% of your transactions will be recorded and sent. That looks like this:

```c
sentry_options_t *options = sentry_options_new();
sentry_options_set_traces_sample_rate(options, 0.2);
// ...
sentry_init(options);
```

### [Setting a Sampling Function](#setting-a-sampling-function)

To use the sampling function, set the `traces_sampler` option to a function that will accept a `sentry_transaction_context_t *`, a `custom_sampling_ctx` and an `int *` for the transaction's parent sampling decision. This function should return a sample rate between 0 and 1. For example:

```c
static double
traces_sampler_callback(const sentry_transaction_context_t *transaction_ctx,
    sentry_value_t custom_sampling_ctx, const int *parent_sampled)
{
    // check if there was a parent sampling decision
    if (parent_sampled != NULL) {
        return *parent_sampled; // Inherit parent's sampling decision
    }
    // a sampling decision can be made on the transaction context name and operation
    if (strcmp(sentry_transaction_context_get_name(transaction_ctx),
        "little.teapot") == 0) {
        if (strcmp(sentry_transaction_context_get_operation(transaction_ctx),
            "Short and stout here is my handle and here is my spout") == 0) {
            // use the custom_sampling_ctx as a key-value dictionary
            if (sentry_value_as_int32(
                sentry_value_get_by_key(custom_sampling_ctx, "b")) == 42) {
                return 1;
            }
        }
    }
    return 0;
}

sentry_options_t *options = sentry_options_new();
sentry_options_set_traces_sampler(options, traces_sampler_callback);
sentry_init(options);
```

## [Sampling Context Data](#sampling-context-data)

### [Default Sampling Context Data](#default-sampling-context-data)

The information contained in the `sampling_context` object passed to the `traces_sampler` when a transaction is created varies by platform and integration.

For the native SDK, it includes

* a [Transaction Context](https://github.com/getsentry/sentry-native/blob/5cb09867668a2b1d09a13a2a13253a83127a31d9/src/sentry_tracing.c),
* a custom sampling context (a `sentry_value_t` of arbitrary structure) and
* a `parent_sampled` flag (`int*`) indicating there wasn't a sampling decision in the transaction's parent if it is `NULL`. If the pointer isn't `NULL`, a value of `0` indicates the parent wasn't sampled, and a `1` means it was.

### [Custom Sampling Context Data](#custom-sampling-context-data)

When using custom instrumentation to create a transaction, you can add data to the `sampling_context` by passing it as an optional second argument to `start_transaction`. 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:

```c
#include <sentry.h>

// a transaction_context can only be used once
// since `sentry_transaction_start` takes ownership
sentry_transaction_context_t *tx_ctx
    = sentry_transaction_context_new("little.teapot",
        "Short and stout here is my handle and here is my spout");

// a custom_sampling_context can only be used once
// since `sentry_transaction_start` takes ownership
sentry_value_t custom_sampling_ctx = sentry_value_new_object();
sentry_value_set_by_key(
    custom_sampling_ctx, "b", sentry_value_new_int32(42));


sentry_transaction_t *tx
    = sentry_transaction_start(tx_ctx, custom_sampling_ctx);
```

`sentry_transaction_start` takes ownership of the custom sampling context and frees it at the end, meaning you cannot reuse it.

## [Inheritance](#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](/platforms/native/tracing/trace-propagation/) 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 `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.

```c
#include <sentry.h>

static double
traces_sampler_callback(const sentry_transaction_context_t *transaction_ctx,
    sentry_value_t custom_sampling_ctx, const int *parent_sampled)
{
    if (parent_sampled != NULL) {
        return *parent_sampled; // inherit sampling decision
    }
    // rest of the sampling logic
}
```

If you're using a `traces_sample_rate` rather than a `traces_sampler`, the decision will always be inherited.

## [Precedence](#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 `traces_sample_rate`
* Random sampling according to a sample function rate returned by `traces_sampler`
* Absolute decision (100% chance or 0% chance) returned by `traces_sampler`
* If the transaction has a parent, inheriting its parent's sampling decision
* Absolute decision passed to `start_transaction`

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 `start_transaction`, that decision will be used, overriding everything else.
2. If `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)
3. If `traces_sampler` is not defined, but there's a parent sampling decision, the parent sampling decision will be used.
4. If `traces_sampler` is not defined and there's no parent sampling decision, `traces_sample_rate` will be used.

Forcing a sampling decision by passing it into `start_transaction` is currently not supported by this platform.
