---
title: "Sampling"
description: "Learn how to configure the volume of error and transaction events sent to Sentry."
url: https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling/
---

# Sampling | Sentry for Flutter

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](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#sampling-error-events)

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

```dart
await SentryFlutter.init((options) {
  // Capture only 25% of events

  options.sampleRate = 0.25;

});
```

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](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#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](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#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 (`tracesSampleRate`):

   * Provides an even cross-section of transactions, no matter where in your app or under what circumstances they occur.
   * Uses default [inheritance](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#inheritance) and [precedence](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#precedence) behavior

2. Sampling function (`tracesSampler`) which:

   * Samples different transactions at different rates
   * [Filters](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/filtering.md) out some transactions entirely
   * Modifies default [precedence](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#precedence) and [inheritance](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#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](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#setting-a-uniform-sample-rate)

To do this, set the `tracesSampleRate` option in your `SentryFlutter.init()` to a number between 0 and 1. With this option set, every transaction created will have that percentage chance of being sent to Sentry. (So, for example, if you set `tracesSampleRate` to `0.2`, approximately 20% of your transactions will get recorded and sent.) That looks like this:

```dart
await SentryFlutter.init((options) => {

  options.tracesSampleRate = 0.2;

});
```

### [Setting a Sampling Function](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#setting-a-sampling-function)

To use the sampling function, set the `tracesSampler` option in your `SentryFlutter.init()` to a function that will accept a `samplingContext` dictionary and return a sample rate between 0 and 1. For example:

```dart
await SentryFlutter.init((options) {
  // Determine traces sample rate based on the sampling context
  options.tracesSampler = (samplingContext) {
    final ctx = samplingContext.customSamplingContext;
    // If this is the continuation of a trace, just use that decision (rate controlled by the caller).
    final parentSampled =
        samplingContext.transactionContext.parentSampled;
    if (parentSampled != null) {
      return parentSampled ? 1.0 : 0.0;
    }

    if ('/payment' == ctx['url']) {
      // These are important - take a big sample
      return 0.5;
    } else if ('/search' == ctx['url']) {
      // Search is less important and happen much more frequently - only take 1%
      return 0.01;
    } else if ('/health' == ctx['url']) {
      // The health check endpoint is just noise - drop all transactions
      return 0.0;
    } else {
      // Default sample rate
      return 0.1;
    }
  };
});
```

## [Sampling Context Data](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#sampling-context-data)

### [Default Sampling Context Data](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#default-sampling-context-data)

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

For Dart-based SDKs, it includes a [SentryTransactionContext](https://pub.dev/documentation/sentry/8.4.0/sentry_io/SentryTransactionContext-class.html) and a [Map\<String, dynamic>](https://api.dart.dev/stable/2.14.1/dart-core/Map-class.html) as method params for [Sentry.startTransactionWithContext](https://pub.dev/documentation/sentry/8.4.0/sentry_io/Sentry/startTransactionWithContext.html).

### [Custom Sampling Context Data](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#custom-sampling-context-data)

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

```dart
import 'package:sentry/sentry.dart';

final context = {
  'user_id': '12312012',
  'search_results': search_results,
};

final transaction = Sentry.startTransaction(
  'processOrderBatch()',
  'task',
  customSamplingContext: context,
);
```

## [Inheritance](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#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](https://docs.sentry.io/platforms/dart/guides/flutter/tracing/trace-propagation.md) 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 `tracesSampler` 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.

```dart
import 'package:sentry/sentry.dart';

double? tracesSampler(SentrySamplingContext samplingContext) {
  final parentSampled = samplingContext.transactionContext.parentSampled;
  if (parentSampled != null) {
    return parentSampled ? 1.0 : 0.0;
  }
  // the rest of sampling logic
}
```

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

## [Forcing a Sampling Decision](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#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 `customSamplingContext` object). If you do that, the transaction won't be subject to the `tracesSampleRate`, nor will `tracesSampler` be run, so you can count on the decision that's passed not to be overwritten.

```dart
import 'package:sentry/sentry.dart';

final transactionContext = SentryTransactionContext(
  'processOrderBatch()',
  'task',
  sampled: true,
);
final transaction = Sentry.startTransactionWithContext(transactionContext);
```

## [Precedence](https://docs.sentry.io/platforms/dart/guides/flutter/configuration/sampling.md#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 `tracesSampleRate`
* Random sampling according to a sample function rate returned by `tracesSampler`
* Absolute decision (100% chance or 0% chance) returned by `tracesSampler`
* If the transaction has a parent, inheriting its parent's sampling decision
* Absolute decision passed to `startTransaction`

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