Skip to content

Commit 2034c7c

Browse files
committed
Add sampling
1 parent c1052f4 commit 2034c7c

File tree

9 files changed

+221
-0
lines changed

9 files changed

+221
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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_ page load or _every_ API request may add an undesirable load to your system.
28+
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.
29+
30+
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.
31+
32+
## Configuring the Transaction Sample Rate
33+
34+
The Sentry SDKs have two configuration options to control the volume of transactions sent to Sentry, allowing you to take a representative sample:
35+
36+
1. Uniform sample rate (<PlatformIdentifier name="traces-sample-rate" />):
37+
38+
- Provides an even cross-section of transactions, no matter where in your app or under what circumstances they occur.
39+
- Uses default [inheritance](#inheritance) and [precedence](#precedence) behavior
40+
41+
2. Sampling function (<PlatformIdentifier name="traces-sampler" />) which:
42+
- Samples different transactions at different rates
43+
- <PlatformLink to="/configuration/filtering/">Filters</PlatformLink> out some
44+
transactions entirely
45+
- Modifies default [precedence](#precedence) and [inheritance](#inheritance) behavior
46+
47+
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.
48+
49+
### Setting a Uniform Sample Rate
50+
51+
<PlatformContent includePath="performance/uniform-sample-rate" />
52+
53+
### Setting a Sampling Function
54+
55+
<PlatformContent includePath="performance/sampling-function-intro" />
56+
57+
## Sampling Context Data
58+
59+
### Default Sampling Context Data
60+
61+
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.
62+
63+
<PlatformContent includePath="performance/default-sampling-context" />
64+
65+
### Custom Sampling Context Data
66+
67+
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 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:
68+
69+
<PlatformContent includePath="performance/custom-sampling-context" />
70+
71+
## Inheritance
72+
73+
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.
74+
75+
(See <PlatformLink to="/tracing/trace-propagation/">Distributed Tracing</PlatformLink> for more about how that propagation is done.)
76+
77+
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.
78+
79+
If you're using a <PlatformIdentifier name="traces-sample-rate" /> rather than a <PlatformIdentifier name="traces-sampler" />, the decision will always be inherited.
80+
81+
## Precedence
82+
83+
There are multiple ways for a transaction to end up with a sampling decision.
84+
85+
- Random sampling according to a static sample rate set in <PlatformIdentifier name="traces-sample-rate" />
86+
- Random sampling according to a sample function rate returned by <PlatformIdentifier name="traces-sampler" />
87+
- Absolute decision (100% chance or 0% chance) returned by <PlatformIdentifier name="traces-sampler" />
88+
- If the transaction has a parent, inheriting its parent's sampling decision
89+
- Absolute decision passed to <PlatformIdentifier name="start-transaction" />
90+
91+
When there's the potential for more than one of these to come into play, the following precedence rules apply:
92+
93+
1. If a sampling decision is passed to <PlatformIdentifier name="start-transaction" />, that decision will be used, overriding everything else.
94+
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
95+
1. If <PlatformIdentifier name="traces-sampler" /> is not defined, but there's a parent sampling decision, the parent sampling decision will be used.
96+
1. If <PlatformIdentifier name="traces-sampler" /> is not defined and there's no parent sampling decision, <PlatformIdentifier name="traces-sample-rate" /> will be used.
97+
98+
<Alert level="info">
99+
100+
The Unreal Engine SDK doesn't currently support forcing a sampling decision by passing it into <PlatformIdentifier name="start-transaction" />.
101+
102+
</Alert>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
```cpp
2+
#include "SentrySettings.h"
3+
4+
FConfigureSettingsDelegate SettingsDelegate;
5+
SettingsDelegate.BindDynamic(this, &USomeClass::ConfigureSettingsDelegate);
6+
7+
void USomeClass::ConfigureSettingsDelegate(USentrySettings* Settings)
8+
{
9+
Settings->SampleRate = 0.25f;
10+
}
11+
12+
...
13+
14+
USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>();
15+
16+
SentrySubsystem->InitializeWithSettings(SettingsDelegate);
17+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
```cpp
2+
USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>();
3+
4+
// The following data will take part in the sampling decision
5+
TMap<FString, FString> options;
6+
options.Add(TEXT("user_id"), TEXT("12312012"));
7+
options.Add(TEXT("search_results"), TEXT("..."));
8+
9+
USentryTransactionContext* transactionContext = NewObject<USentryTransactionContext>();
10+
transactionContext->Initialize(
11+
TEXT("GET /search"),
12+
TEXT("http")
13+
);
14+
15+
USentryTransaction* transaction =
16+
SentrySubsystem->StartTransactionWithContextAndOptions(
17+
transactionContext,
18+
options
19+
);
20+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
For Unreal Engine SDK, it includes a [Transaction Context](https://github.com/getsentry/sentry-unreal/blob/main/plugin-dev/Source/Sentry/Public/SentryTransactionContext.h) and a custom sampling context (`FString` to `FString` map).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<PlatformContent includePath="performance/default-sampling-context-platform" />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
To use the sampling function, set the <PlatformIdentifier name="traces-sampler" /> option in your SDK configuration to a function that will accept a <PlatformIdentifier name="sampling-context" /> dictionary and return a sample rate between 0 and 1. For example:
2+
3+
<PlatformContent includePath="performance/traces-sampler-as-sampler" />
4+
5+
<Alert level="info">
6+
7+
The Unreal Engine SDK doesn't currently support sampling functions on Windows/Linux (<PlatformIdentifier name="traces-sampler" />).
8+
9+
</Alert>
10+
11+
Additionally, your sampling function may also return `false`, which indicates that no sampling decision has been made. In such case, the trace will retain the previous decision if it has been made (for example if it was started from an incoming trace header), or fall back to the value configured in <PlatformIdentifier name="traces-sample-rate" />.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
```cpp
2+
#include "SentrySettings.h"
3+
4+
FConfigureSettingsDelegate SettingsDelegate;
5+
SettingsDelegate.BindDynamic(this, &USomeClass::ConfigureSettingsDelegate);
6+
7+
void USomeClass::ConfigureSettingsDelegate(USentrySettings* Settings)
8+
{
9+
Settings->TracesSampleRate = 0.2f;
10+
}
11+
12+
...
13+
14+
USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>();
15+
16+
SentrySubsystem->InitializeWithSettings(SettingsDelegate);
17+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```cpp
2+
UCLASS()
3+
class USomeTraceSampler : public USentryTraceSampler
4+
{
5+
GENERATED_BODY()
6+
7+
public:
8+
virtual bool Sample_Implementation(USentrySamplingContext* samplingContext, float& samplingValue) override
9+
{
10+
const FString& path =
11+
*samplingContext->GetCustomSamplingContext().Find("__HttpPath");
12+
13+
if (path.Equals(TEXT("/payment")))
14+
{
15+
// These are important - take a big sample
16+
samplingValue = 0.5;
17+
}
18+
else
19+
{
20+
// Default sample rate
21+
samplingValue = 0.1;
22+
}
23+
24+
return true;
25+
26+
// Or return false to fallback to options.TracesSampleRate (1.0 in this case)
27+
}
28+
};
29+
30+
...
31+
32+
FConfigureSettingsDelegate SettingsDelegate;
33+
SettingsDelegate.BindDynamic(this, &USomeClass::HandleSettingsDelegate);
34+
35+
void USomeClass::HandleSettingsDelegate(USentrySettings* Settings)
36+
{
37+
// Set a uniform sample rate
38+
Settings->TracesSampleRate = 1.0f;
39+
40+
// OR: Determine traces sample rate based on the sampling context
41+
Settings->TracesSampler = USomeTraceSampler::StaticCLass();
42+
}
43+
44+
...
45+
46+
USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem<USentrySubsystem>();
47+
48+
SentrySubsystem->InitializeWithSettings(SettingsDelegate);
49+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
To do this, set the <PlatformIdentifier name="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 <PlatformIdentifier name="traces-sample-rate" /> to `0.2`, approximately 20% of your transactions will be recorded and sent. That looks like this:
2+
3+
<PlatformContent includePath="performance/traces-sample-rate" />

0 commit comments

Comments
 (0)