1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
//! Dynamic sampling rule configuration.
use std::fmt;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use relay_protocol::RuleCondition;
/// Maximum supported version of dynamic sampling.
///
/// The version is an integer scalar, incremented by one on each new version:
/// - 1: Initial version that uses `rules_v2`.
/// - 2: Moves back to `rules` and adds support for `RuleConfigs` with string comparisons.
const SAMPLING_CONFIG_VERSION: u16 = 2;
/// Represents the dynamic sampling configuration available to a project.
///
/// Note: This comes from the organization data
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SamplingConfig {
/// The required version to run dynamic sampling.
///
/// Defaults to legacy version (`1`) when missing.
#[serde(default = "SamplingConfig::legacy_version")]
pub version: u16,
/// The ordered sampling rules for the project.
#[serde(default)]
pub rules: Vec<SamplingRule>,
/// **Deprecated**. The ordered sampling rules for the project in legacy format.
///
/// Removed in favor of `Self::rules` in version `2`. This field remains here to parse rules
/// from old Sentry instances and convert them into the new format. The legacy format contained
/// both an empty `rules` as well as the actual rules in `rules_v2`. During normalization, these
/// two arrays are merged together.
#[serde(default, skip_serializing)]
pub rules_v2: Vec<SamplingRule>,
}
impl SamplingConfig {
/// Creates an enabled configuration with empty defaults and the latest version.
pub fn new() -> Self {
Self::default()
}
/// Returns `true` if any of the rules in this configuration is unsupported.
pub fn unsupported(&self) -> bool {
debug_assert!(self.version > 1, "SamplingConfig not normalized");
self.version > SAMPLING_CONFIG_VERSION || !self.rules.iter().all(SamplingRule::supported)
}
/// Filters the sampling rules by the given [`RuleType`].
pub fn filter_rules(&self, rule_type: RuleType) -> impl Iterator<Item = &SamplingRule> {
self.rules.iter().filter(move |rule| rule.ty == rule_type)
}
/// Upgrades legacy sampling configs into the latest format.
pub fn normalize(&mut self) {
if self.version == Self::legacy_version() {
self.rules.append(&mut self.rules_v2);
self.version = SAMPLING_CONFIG_VERSION;
}
}
const fn legacy_version() -> u16 {
1
}
}
impl Default for SamplingConfig {
fn default() -> Self {
Self {
version: SAMPLING_CONFIG_VERSION,
rules: vec![],
rules_v2: vec![],
}
}
}
/// A sampling rule as it is deserialized from the project configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SamplingRule {
/// A condition to match for this sampling rule.
///
/// Sampling rules do not run if their condition does not match.
pub condition: RuleCondition,
/// The sample rate to apply when this rule matches.
pub sampling_value: SamplingValue,
/// The rule type declares what to apply a dynamic sampling rule to and how.
#[serde(rename = "type")]
pub ty: RuleType,
/// The unique identifier of this rule.
pub id: RuleId,
/// The time range the rule should be applicable in.
///
/// The time range is open on both ends by default. If a time range is
/// closed on at least one end, the rule is considered a decaying rule.
#[serde(default, skip_serializing_if = "TimeRange::is_empty")]
pub time_range: TimeRange,
/// Declares how to interpolate the sample rate for rules with bounded time range.
#[serde(default, skip_serializing_if = "is_default")]
pub decaying_fn: DecayingFunction,
}
impl SamplingRule {
fn supported(&self) -> bool {
self.condition.supported() && self.ty != RuleType::Unsupported
}
/// Applies its decaying function to the given sample rate.
pub fn apply_decaying_fn(&self, sample_rate: f64, now: DateTime<Utc>) -> Option<f64> {
self.decaying_fn
.adjust_sample_rate(sample_rate, now, self.time_range)
}
}
/// Returns `true` if this value is equal to `Default::default()`.
fn is_default<T: Default + PartialEq>(t: &T) -> bool {
*t == T::default()
}
/// A sampling strategy definition.
///
/// A sampling strategy refers to the strategy that we want to use for sampling a specific rule.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "type")]
pub enum SamplingValue {
/// A direct sample rate to apply.
///
/// A rule with a sample rate will be matched and the final sample rate will be computed by
/// multiplying its sample rate with the accumulated factors from previous rules.
SampleRate {
/// The sample rate to apply to the rule.
value: f64,
},
/// A factor to apply on a subsequently matching rule.
///
/// A rule with a factor will be matched and the matching will continue onto the next rules
/// until a sample rate rule is found. The matched rule's factor will be multiplied with the
/// accumulated factors before moving onto the next possible match.
Factor {
/// The factor to apply on another matched sample rate.
value: f64,
},
/// A reservoir limit.
///
/// A rule with a reservoir limit will be sampled if the rule have been matched fewer times
/// than the limit.
Reservoir {
/// The limit of how many times this rule will be sampled before this rule is invalid.
limit: i64,
},
}
/// Defines what a dynamic sampling rule applies to.
#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum RuleType {
/// A trace rule matches on the [`DynamicSamplingContext`](crate::DynamicSamplingContext) and
/// applies to all transactions in a trace.
Trace,
/// A transaction rule matches directly on the transaction event independent of the trace.
Transaction,
// NOTE: If you add a new `RuleType` that is not supposed to sample transactions, you need to
// edit the `sample_envelope` function in `EnvelopeProcessorService`.
/// If the sampling config contains new rule types, do not sample at all.
#[serde(other)]
Unsupported,
}
/// The identifier of a [`SamplingRule`].
///
/// This number must be unique within a Sentry organization, as it is recorded in outcomes and used
/// to infer which sampling rule caused data to be dropped.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RuleId(pub u32);
impl fmt::Display for RuleId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
/// A range of time.
///
/// The time range should be applicable between the start time, inclusive, and
/// end time, exclusive. There aren't any explicit checks to ensure the end
/// time is equal to or greater than the start time; the time range isn't valid
/// in such cases.
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct TimeRange {
/// The inclusive start of the time range.
pub start: Option<DateTime<Utc>>,
/// The exclusive end of the time range.
pub end: Option<DateTime<Utc>>,
}
impl TimeRange {
/// Returns true if neither the start nor end time limits are set.
pub fn is_empty(&self) -> bool {
self.start.is_none() && self.end.is_none()
}
/// Returns whether the provided time matches the time range.
///
/// For a time to match a time range, the following conditions must match:
/// - The start time must be smaller than or equal to the given time, if provided.
/// - The end time must be greater than the given time, if provided.
///
/// If one of the limits isn't provided, the range is considered open in
/// that limit. A time range open on both sides matches with any given time.
pub fn contains(&self, time: DateTime<Utc>) -> bool {
self.start.map_or(true, |s| s <= time) && self.end.map_or(true, |e| time < e)
}
}
/// Specifies how to interpolate sample rates for rules with bounded time window.
#[derive(Default, Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "type")]
pub enum DecayingFunction {
/// Apply linear interpolation of the sample rate in the time window.
///
/// The rule will start to apply with the configured sample rate at the beginning of the time
/// window and end with `decayed_value` at the end of the time window.
#[serde(rename_all = "camelCase")]
Linear {
/// The target value at the end of the time window.
decayed_value: f64,
},
/// Apply the sample rate of the rule for the full time window with hard cutoff.
#[default]
Constant,
}
impl DecayingFunction {
/// Applies the decaying function to the given sample rate.
pub fn adjust_sample_rate(
&self,
sample_rate: f64,
now: DateTime<Utc>,
time_range: TimeRange,
) -> Option<f64> {
match self {
DecayingFunction::Linear { decayed_value } => {
let (Some(start), Some(end)) = (time_range.start, time_range.end) else {
return None;
};
if sample_rate < *decayed_value {
return None;
}
let now = now.timestamp() as f64;
let start = start.timestamp() as f64;
let end = end.timestamp() as f64;
let progress_ratio = ((now - start) / (end - start)).clamp(0.0, 1.0);
// This interval will always be < 0.
let interval = decayed_value - sample_rate;
Some(sample_rate + (interval * progress_ratio))
}
DecayingFunction::Constant => Some(sample_rate),
}
}
}
#[cfg(test)]
mod tests {
use chrono::TimeZone;
use super::*;
#[test]
fn config_deserialize() {
let json = include_str!("../tests/fixtures/sampling_config.json");
serde_json::from_str::<SamplingConfig>(json).unwrap();
}
#[test]
fn test_supported() {
let rule: SamplingRule = serde_json::from_value(serde_json::json!({
"id": 1,
"type": "trace",
"samplingValue": {"type": "sampleRate", "value": 1.0},
"condition": {"op": "and", "inner": []}
}))
.unwrap();
assert!(rule.supported());
}
#[test]
fn test_unsupported_rule_type() {
let rule: SamplingRule = serde_json::from_value(serde_json::json!({
"id": 1,
"type": "new_rule_type_unknown_to_this_relay",
"samplingValue": {"type": "sampleRate", "value": 1.0},
"condition": {"op": "and", "inner": []}
}))
.unwrap();
assert!(!rule.supported());
}
#[test]
fn test_non_decaying_sampling_rule_deserialization() {
let serialized_rule = r#"{
"condition":{
"op":"and",
"inner": [
{ "op" : "glob", "name": "releases", "value":["1.1.1", "1.1.2"]}
]
},
"samplingValue": {"type": "sampleRate", "value": 0.7},
"type": "trace",
"id": 1
}"#;
let rule: SamplingRule = serde_json::from_str(serialized_rule).unwrap();
assert_eq!(
rule.sampling_value,
SamplingValue::SampleRate { value: 0.7f64 }
);
assert_eq!(rule.ty, RuleType::Trace);
}
#[test]
fn test_non_decaying_sampling_rule_deserialization_with_factor() {
let serialized_rule = r#"{
"condition":{
"op":"and",
"inner": [
{ "op" : "glob", "name": "releases", "value":["1.1.1", "1.1.2"]}
]
},
"samplingValue": {"type": "factor", "value": 5.0},
"type": "trace",
"id": 1
}"#;
let rule: SamplingRule = serde_json::from_str(serialized_rule).unwrap();
assert_eq!(rule.sampling_value, SamplingValue::Factor { value: 5.0 });
assert_eq!(rule.ty, RuleType::Trace);
}
#[test]
fn test_sampling_rule_with_constant_decaying_function_deserialization() {
let serialized_rule = r#"{
"condition":{
"op":"and",
"inner": [
{ "op" : "glob", "name": "releases", "value":["1.1.1", "1.1.2"]}
]
},
"samplingValue": {"type": "factor", "value": 5.0},
"type": "trace",
"id": 1,
"timeRange": {
"start": "2022-10-10T00:00:00.000000Z",
"end": "2022-10-20T00:00:00.000000Z"
}
}"#;
let rule: Result<SamplingRule, _> = serde_json::from_str(serialized_rule);
let rule = rule.unwrap();
let time_range = rule.time_range;
let decaying_function = rule.decaying_fn;
assert_eq!(
time_range.start,
Some(Utc.with_ymd_and_hms(2022, 10, 10, 0, 0, 0).unwrap())
);
assert_eq!(
time_range.end,
Some(Utc.with_ymd_and_hms(2022, 10, 20, 0, 0, 0).unwrap())
);
assert_eq!(decaying_function, DecayingFunction::Constant);
}
#[test]
fn test_sampling_rule_with_linear_decaying_function_deserialization() {
let serialized_rule = r#"{
"condition":{
"op":"and",
"inner": [
{ "op" : "glob", "name": "releases", "value":["1.1.1", "1.1.2"]}
]
},
"samplingValue": {"type": "sampleRate", "value": 1.0},
"type": "trace",
"id": 1,
"timeRange": {
"start": "2022-10-10T00:00:00.000000Z",
"end": "2022-10-20T00:00:00.000000Z"
},
"decayingFn": {
"type": "linear",
"decayedValue": 0.9
}
}"#;
let rule: Result<SamplingRule, _> = serde_json::from_str(serialized_rule);
let rule = rule.unwrap();
let decaying_function = rule.decaying_fn;
assert_eq!(
decaying_function,
DecayingFunction::Linear { decayed_value: 0.9 }
);
}
#[test]
fn test_legacy_deserialization() {
let serialized_rule = r#"{
"rules": [],
"rulesV2": [
{
"samplingValue":{
"type": "sampleRate",
"value": 0.5
},
"type": "trace",
"active": true,
"condition": {
"op": "and",
"inner": []
},
"id": 1000
}
],
"mode": "received"
}"#;
let mut config: SamplingConfig = serde_json::from_str(serialized_rule).unwrap();
config.normalize();
// We want to make sure that we serialize an empty array of rule, irrespectively of the
// received payload.
assert_eq!(config.version, SAMPLING_CONFIG_VERSION);
assert_eq!(
config.rules[0].sampling_value,
SamplingValue::SampleRate { value: 0.5 }
);
assert!(config.rules_v2.is_empty());
}
#[test]
fn test_sampling_config_with_rules_and_rules_v2_serialization() {
let config = SamplingConfig {
rules: vec![SamplingRule {
condition: RuleCondition::all(),
sampling_value: SamplingValue::Factor { value: 2.0 },
ty: RuleType::Transaction,
id: RuleId(1),
time_range: Default::default(),
decaying_fn: Default::default(),
}],
..SamplingConfig::new()
};
let serialized_config = serde_json::to_string_pretty(&config).unwrap();
let expected_serialized_config = r#"{
"version": 2,
"rules": [
{
"condition": {
"op": "and",
"inner": []
},
"samplingValue": {
"type": "factor",
"value": 2.0
},
"type": "transaction",
"id": 1
}
]
}"#;
assert_eq!(serialized_config, expected_serialized_config)
}
/// Checks that the sample rate stays constant if `DecayingFunction::Constant` is set.
#[test]
fn test_decay_fn_constant() {
let sample_rate = 0.5;
assert_eq!(
DecayingFunction::Constant.adjust_sample_rate(
sample_rate,
Utc::now(),
TimeRange::default()
),
Some(sample_rate)
);
}
/// Checks if the sample rate decays linearly if `DecayingFunction::Linear` is set.
#[test]
fn test_decay_fn_linear() {
let decaying_fn = DecayingFunction::Linear { decayed_value: 0.5 };
let time_range = TimeRange {
start: Some(Utc.with_ymd_and_hms(1970, 10, 10, 0, 0, 0).unwrap()),
end: Some(Utc.with_ymd_and_hms(1970, 10, 12, 0, 0, 0).unwrap()),
};
let start = Utc.with_ymd_and_hms(1970, 10, 10, 0, 0, 0).unwrap();
let halfway = Utc.with_ymd_and_hms(1970, 10, 11, 0, 0, 0).unwrap();
let end = Utc.with_ymd_and_hms(1970, 10, 11, 23, 59, 59).unwrap();
// At the start of the time range, sample rate is equal to the rule's initial sampling value.
assert_eq!(
decaying_fn.adjust_sample_rate(1.0, start, time_range),
Some(1.0)
);
// Halfway in the time range, the value is exactly between 1.0 and 0.5.
assert_eq!(
decaying_fn.adjust_sample_rate(1.0, halfway, time_range),
Some(0.75)
);
// Approaches 0.5 at the end.
assert_eq!(
decaying_fn.adjust_sample_rate(1.0, end, time_range),
// It won't go to exactly 0.5 because the time range is end-exclusive.
Some(0.5000028935185186)
);
// If the end or beginning is missing, the linear decay shouldn't be run.
let mut time_range_without_start = time_range;
time_range_without_start.start = None;
assert!(decaying_fn
.adjust_sample_rate(1.0, halfway, time_range_without_start)
.is_none());
let mut time_range_without_end = time_range;
time_range_without_end.end = None;
assert!(decaying_fn
.adjust_sample_rate(1.0, halfway, time_range_without_end)
.is_none());
}
}