Sampling Transactions

You can control the volume of transactions sent to Sentry in two ways.

Uniform Sample Rate

Setting a uniform sample rate is a good option if you want an even cross-section of transactions, no matter where in your app or under what circumstances they occur, and are happy with the default inheritance and precedence behavior described below.

To do this, set the traces-sample-rate option in your Sentry.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 traces-sample-rate to 0.2, approximately 20% of your transactions will get recorded and sent.) That looks like this:

Copied
sentry.traces-sample-rate=0.2

Dynamic Sampling Function

Providing a sampling function is a good option if you:

  • want to sample different transactions at different rates
  • want to filter out some transactions entirely
  • want to modify the default precedence and inheritance behavior described below

To sample dynamically, set the traces-sampler option in your Sentry.init() to a function that will accept a sampling-context dictionary and return a sample rate between 0 and 1. For example:

Copied
import io.sentry.SamplingContext;
import io.sentry.SentryOptions.TracesSamplerCallback;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

@Component
class CustomTracesSamplerCallback implements TracesSamplerCallback {
  @Override
  public Double sample(SamplingContext context) {
    HttpServletRequest request = (HttpServletRequest) context.getCustomSamplingContext().get("request");
    String url = request.getRequestURI();
    if ("/payment".equals(url)) {
      // These are important - take a big sample
      return 0.5;
    } else if ("/search".equals(url)) {
      // Search is less important and happen much more frequently - only take 1%
      return 0.01;
    } else if ("/health".equals(url)) {
      // The health check endpoint is just noise - drop all transactions
      return 0d;
    } else {
      // Default sample rate
      return 0.1;
    }
  }
}

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 Java-based SDKs, it includes a Transaction Context and a Custom Sampling Context.

Custom Sampling Context Data

When manually creating 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:

Copied
import io.sentry.CustomSamplingContext;
import io.sentry.ITransaction;
import io.sentry.Sentry;

// sampling context - won't be recorded
CustomSamplingContext context = new CustomSamplingContext();
context.set("user_id", 12312012);
context.set("search_results", searchResults);
ITransaction transaction = Sentry.startTransaction("GET /search", context);
// transaction context - will be recorded on transaction
transaction.setOperation("http");
transaction.setDescription("search results");

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 Connecting Backend and Frontend Transactions 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 always be included in the sampling context data, so that your traces-sampler can choose whether and when to inherit that decision. (In most cases, inheritance is the right choice, so that you don't end up with partial traces.)

In some SDKs, for convenience, the traces-sampler function can return a boolean, so that a parent's decision can be returned directly if that's the desired behavior.

Copied
import io.sentry.SentryOptions;

SentryOptions.TracesSamplerCallback tracesSampler = samplingContext -> {
    Boolean parentSampled = samplingContext.getTransactionContext().getParentSampled();
    if (parentSampled != null) {
        return parentSampled ? 1.0 : 0.0;
    }
    // the rest of sampling logic
};

If you're using a traces-sample-rate rather than a traces-sampler, 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 custom-sampling-context object). If you do that, the transaction won't be subject to the traces-sample-rate, nor will traces-sampler be run, so you can count on the decision that's passed not to be overwritten.

Copied
import io.sentry.ITransaction;
import io.sentry.Sentry;
import io.sentry.TransactionContext;

TransactionContext transactionContext = new TransactionContext("GET /search", true);
ITransaction transaction = Sentry.startTransaction(transactionContext);

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 dynamic sample 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 (see Forcing a Sampling Decision above), that decision will be used, regardlesss of anything else
  2. If traces-sampler is defined, its decision will be used. It can choose to keep or ignore any parent sampling decision, or use the sampling context data to make its own decision or choose a sample rate for the transaction.
  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.
You can edit this page on GitHub.