Sampling

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

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:

The platform or SDK you've selected either does not support this functionality, or it is missing from documentation.

If you think this is an error, feel free to let us know on GitHub.

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

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 and precedence behavior
  2. Sampling function (tracesSampler) which:

Setting a Uniform Sample Rate

To do this, set the TracesSampleRate option in your sentry.Init() to a number between 0.0 and 1.0. 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:

Copied
err := sentry.Init(sentry.ClientOptions{
	TracesSampleRate: 0.2,
})

Setting a Sampling Function

To use the sampling function, set the TracesSampler option in your sentry.Init() to a function that will accept a SamplingContext and return a sampling decision: true, false or undefined. For example:

Copied
err := sentry.Init(sentry.ClientOptions{
	// ...
	TracesSampler: sentry.TracesSamplerFunc(func(ctx sentry.SamplingContext) sentry.Sampled {
		switch {
		case condition1:
			return sentry.UniformTracesSampler(0.2).Sample(ctx)
		case condition2:
			return sentry.UniformTracesSampler(0.01).Sample(ctx)
		case condition3:
			return sentry.SampledFalse
		default:
			return sentry.UniformTracesSampler(0.1).Sample(ctx)
		}
	}),
})

Sampling Context Data

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 the Go SDK, it is:

Copied
type SamplingContext struct {
	Span   *Span // The current span, always non-nil.
	Parent *Span // The parent span, may be nil.
}

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:

Copied
type myContextKey struct{}
type myContextData struct {
	// Store information for custom TracesSampler.
	request *http.Request
	// ...
}

err := sentry.Init(sentry.ClientOptions{
	// A custom TracesSampler can access data from the span's context:
	TracesSampler: sentry.TracesSamplerFunc(func(ctx sentry.SamplingContext) sentry.Sampled {
		data, ok := ctx.Span.Context().Value(myContextKey{}).(*myContextData)
		if !ok {
			return sentry.SampledFalse
		}
		if data.request.URL.Hostname() == "example.com" {
			return sentry.SampledTrue
		}
		return sentry.SampledFalse
	}),
})
// ...

r, err := http.NewRequest("GET", "http://example.com", nil)
// ...
data := &myContextData{
	request: r,
}
ctx := context.WithValue(context.Background(), myContextKey{}, data)
span := sentry.StartSpan(ctx, "operation")
// When sampling the above span, the custom TracesSampler will have
// access to data.

// ...
span.Finish()

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 Services 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 TracesSampler can choose whether and when to inherit that decision. (In most cases, inheritance is the right choice, to avoid partial traces.)

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

Copied
err := sentry.Init(sentry.ClientOptions{
	TracesSampler: sentry.TracesSamplerFunc(func(ctx sentry.SamplingContext) sentry.Sampled {
		// Inherit decision from parent.
		if ctx.Parent != nil && ctx.Parent.Sampled != sentry.SampledUndefined {
			return ctx.Parent.Sampled
		}
		// Or continue with a custom decision...
	}),
})

If you're using a TracesSampleRate rather than a TracesSampler, 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 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.

Copied
// If starting the first span in a transaction (no other spans in the context):
sentry.StartSpan(context.Background(), "operation",
	sentry.TransactionName("name"),
	func(s *sentry.Span) { s.Sampled = sentry.SampledTrue },
)

// If the transaction was already started, for example in an HTTP handler:
transaction := sentry.TransactionFromContext(request.Context())
transaction.Sampled = sentry.SampledTrue

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 (see Forcing a Sampling Decision above), that decision will be used, regardlesss of anything else
  2. If TracesSampler 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 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.
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").