relay_sampling/
lib.rs

1//! Sampling logic for performing sampling decisions of incoming events.
2//!
3//! In order to allow Sentry to offer performance at scale, Relay extracts key `metrics` from all
4//! transactions, but only forwards a random sample of raw transaction payloads to the upstream.
5//! What exact percentage is sampled is determined by `dynamic sampling rules`, and depends on the
6//! project, the environment, the transaction name, etc.
7//!
8//! In order to determine the sample rate, Relay uses a [`SamplingConfig`] which contains a set of
9//! [`SamplingRule`](crate::config::SamplingRule)s that are matched against incoming data.
10//!
11//! # Types of Sampling
12//!
13//! There are two main types of dynamic sampling:
14//!
15//! 1. **Trace sampling** ensures that either all transactions of a trace are sampled or none. Rules
16//!    have access to information in the [`DynamicSamplingContext`].
17//! 2. **Transaction sampling** does not guarantee complete traces and instead applies to individual
18//!   transactions. Rules have access to the full data in transaction events.
19//!
20//! # Components
21//!
22//! The sampling system implemented in Relay is composed of the following components:
23//!
24//! - [`DynamicSamplingContext`]: a container for information associated with the trace.
25//! - [`SamplingRule`](crate::config::SamplingRule): a rule that is matched against incoming data.
26//!   It specifies a condition that acts as predicate on the incoming payload.
27//! - [`SamplingMatch`](crate::evaluation::SamplingMatch): the result of the matching of one or more
28//!   rules.
29//!
30//! # How It Works
31//!
32//! - The incoming data is received by Relay.
33//! - Relay resolves the [`SamplingConfig`] of the project to which the incoming data belongs.
34//!   Additionally, it tries to resolve the configuration of the project that started the trace, the
35//!   so-called "root project".
36//! - The two [`SamplingConfig`]s are merged together and dynamic sampling evaluates a result. The
37//!   algorithm goes over each rule and compute either a factor or sample rate based on the
38//!   value of the rule.
39//! - The [`SamplingMatch`](crate::evaluation::SamplingMatch) is finally returned containing the
40//!   final `sample_rate` and some additional data that will be used in `relay_server` to perform
41//!   the sampling decision.
42//!
43//! # Determinism
44//!
45//! The concept of determinism is a core concept for dynamic sampling. Deterministic sampling
46//! decisions allow to:
47//!
48//! - Run dynamic sampling repeatedly over a **chain of Relays** (e.g., we don't want to drop an
49//! event that was retained by a previous Relay and vice-versa).
50//! - Run dynamic sampling independently on **transactions of the same trace** (e.g., we want to be
51//! able to sample all the transactions of the same trace, even though some exceptions apply).
52//!
53//! In order to perform deterministic sampling, Relay uses teh trace ID or event ID as the seed for
54//! the random number generator.
55//!
56//! # Examples
57//!
58//! ## [`SamplingConfig`]
59//!
60//! ```json
61#![doc = include_str!("../tests/fixtures/sampling_config.json")]
62//! ```
63//!
64//! ## [`DynamicSamplingContext`]
65//!
66//! ```json
67#![doc = include_str!("../tests/fixtures/dynamic_sampling_context.json")]
68//! ```
69
70#![doc(
71    html_logo_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png",
72    html_favicon_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png"
73)]
74#![warn(missing_docs)]
75
76pub mod config;
77pub mod dsc;
78pub mod evaluation;
79#[cfg(feature = "redis")]
80mod redis_sampling;
81
82pub use config::SamplingConfig;
83pub use dsc::DynamicSamplingContext;