Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sampling): Reservoir sampling #2550

Merged
merged 34 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Remove filtering for Android events with missing close events. ([#2524](https://github.com/getsentry/relay/pull/2524))
- Exclude more spans fron metrics extraction. ([#2522](https://github.com/getsentry/relay/pull/2522), [#2525](https://github.com/getsentry/relay/pull/2525), [#2545](https://github.com/getsentry/relay/pull/2545))
- Fix hot-loop burning CPU when upstream service is unavailable. ([#2518](https://github.com/getsentry/relay/pull/2518))
- Introduce reservoir sampling rule. ([#2550](https://github.com/getsentry/relay/pull/2550))

## 23.9.1

Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion relay-kafka/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//!
//! // build the client
//! let kafka_client = builder.build();
//!
TBS1996 marked this conversation as resolved.
Show resolved Hide resolved
//!
//! // send the message
//! kafka_client.send_message(KafkaTopic::Events, 1u64, &kafka_message).unwrap();
//! ```
Expand Down
7 changes: 7 additions & 0 deletions relay-sampling/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ edition = "2021"
license-file = "../LICENSE"
publish = false

[features]
default = []
redis = ["dep:anyhow", "dep:relay-redis"]


[dependencies]
anyhow = { workspace = true, optional = true }
chrono = { workspace = true }
rand = { workspace = true }
rand_pcg = "0.3.1"
relay-base-schema = { path = "../relay-base-schema" }
relay-common = { path = "../relay-common" }
relay-log = { path = "../relay-log" }
relay-protocol = { path = "../relay-protocol" }
relay-redis = { path = "../relay-redis", optional = true }
serde = { workspace = true }
serde_json = { workspace = true }
unicase = "2.6.0"
Expand Down
96 changes: 63 additions & 33 deletions relay-sampling/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::condition::RuleCondition;
use crate::evaluation::ReservoirEvaluator;
use crate::utils;

/// Represents the dynamic sampling configuration available to a project.
Expand Down Expand Up @@ -81,14 +82,28 @@ impl SamplingRule {
self.condition.supported() && self.ty != RuleType::Unsupported
}

/// Returns the sample rate if the rule is active.
pub fn sample_rate(&self, now: DateTime<Utc>) -> Option<SamplingValue> {
/// Returns the updated [`SamplingValue`] if it's valid.
pub fn evaluate(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed the sample_rate function.

&self,
now: DateTime<Utc>,
reservoir: Option<&ReservoirEvaluator>,
) -> Option<SamplingValue> {
if !self.time_range.contains(now) {
// Return None if rule is inactive.
return None;
}

let sampling_base_value = self.sampling_value.value();
let sampling_base_value = match self.sampling_value {
SamplingValue::SampleRate { value } => value,
SamplingValue::Factor { value } => value,
SamplingValue::Reservoir { limit } => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugly: we return SamplingValue::Reservoir{limit}, even though the limit no longer has any meaning passed this point. It was the easiest solution, will refactor in the future.

return reservoir.and_then(|reservoir| {
reservoir
.evaluate(self.id, limit, self.time_range.end.as_ref())
.then_some(SamplingValue::Reservoir { limit })
});
}
};

let value = match self.decaying_fn {
DecayingFunction::Linear { decayed_value } => {
Expand All @@ -114,6 +129,9 @@ impl SamplingRule {
match self.sampling_value {
SamplingValue::SampleRate { .. } => Some(SamplingValue::SampleRate { value }),
SamplingValue::Factor { .. } => Some(SamplingValue::Factor { value }),
// This should be impossible.
// Todo(tor): refactor so we don't run into this invalid state.
_ => None,
}
}
}
Expand All @@ -140,18 +158,18 @@ pub enum SamplingValue {
/// 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 fator to apply on another matched sample rate.
/// The factor to apply on another matched sample rate.
value: f64,
},
}

impl SamplingValue {
pub(crate) fn value(&self) -> f64 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function no longer makes sense because reservoir limit doesn't have an analogous value to these

*match self {
SamplingValue::SampleRate { value } => value,
SamplingValue::Factor { value } => value,
}
}
/// 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.
Expand All @@ -174,7 +192,7 @@ pub enum RuleType {
///
/// 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)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
jan-auer marked this conversation as resolved.
Show resolved Hide resolved
pub struct RuleId(pub u32);

impl fmt::Display for RuleId {
Expand Down Expand Up @@ -515,19 +533,19 @@ mod tests {

// At the start of the time range, sample rate is equal to the rule's initial sampling value.
assert_eq!(
rule.sample_rate(start).unwrap(),
rule.evaluate(start, None).unwrap(),
SamplingValue::SampleRate { value: 1.0 }
);

// Halfway in the time range, the value is exactly between 1.0 and 0.5.
assert_eq!(
rule.sample_rate(halfway).unwrap(),
rule.evaluate(halfway, None).unwrap(),
SamplingValue::SampleRate { value: 0.75 }
);

// Approaches 0.5 at the end.
assert_eq!(
rule.sample_rate(end).unwrap(),
rule.evaluate(end, None).unwrap(),
SamplingValue::SampleRate {
// It won't go to exactly 0.5 because the time range is end-exclusive.
value: 0.5000028935185186
Expand All @@ -541,15 +559,15 @@ mod tests {
rule
};

assert!(rule_without_start.sample_rate(halfway).is_none());
assert!(rule_without_start.evaluate(halfway, None).is_none());

let rule_without_end = {
let mut rule = rule.clone();
rule.time_range.end = None;
rule
};

assert!(rule_without_end.sample_rate(halfway).is_none());
assert!(rule_without_end.evaluate(halfway, None).is_none());
}

/// If the decayingfunction is set to `Constant` then it shouldn't adjust the sample rate.
Expand All @@ -571,7 +589,7 @@ mod tests {

let halfway = Utc.with_ymd_and_hms(1970, 10, 11, 0, 0, 0).unwrap();

assert_eq!(rule.sample_rate(halfway), Some(sampling_value));
assert_eq!(rule.evaluate(halfway, None), Some(sampling_value));
}

/// Validates the `sample_rate` method for different time range configurations.
Expand All @@ -597,30 +615,42 @@ mod tests {
time_range,
decaying_fn: DecayingFunction::Constant,
};
assert!(rule.sample_rate(before_time_range).is_none());
assert!(rule.sample_rate(during_time_range).is_some());
assert!(rule.sample_rate(after_time_range).is_none());
assert!(rule.evaluate(before_time_range, None).is_none());
assert!(rule.evaluate(during_time_range, None).is_some());
assert!(rule.evaluate(after_time_range, None).is_none());

// [start..]
let mut rule_without_end = rule.clone();
rule_without_end.time_range.end = None;
assert!(rule_without_end.sample_rate(before_time_range).is_none());
assert!(rule_without_end.sample_rate(during_time_range).is_some());
assert!(rule_without_end.sample_rate(after_time_range).is_some());
assert!(rule_without_end.evaluate(before_time_range, None).is_none());
assert!(rule_without_end.evaluate(during_time_range, None).is_some());
assert!(rule_without_end.evaluate(after_time_range, None).is_some());

// [..end]
let mut rule_without_start = rule.clone();
rule_without_start.time_range.start = None;
assert!(rule_without_start.sample_rate(before_time_range).is_some());
assert!(rule_without_start.sample_rate(during_time_range).is_some());
assert!(rule_without_start.sample_rate(after_time_range).is_none());
assert!(rule_without_start
.evaluate(before_time_range, None)
.is_some());
assert!(rule_without_start
.evaluate(during_time_range, None)
.is_some());
assert!(rule_without_start
.evaluate(after_time_range, None)
.is_none());

// [..]
let mut rule_without_range = rule.clone();
rule_without_range.time_range = TimeRange::default();
assert!(rule_without_range.sample_rate(before_time_range).is_some());
assert!(rule_without_range.sample_rate(during_time_range).is_some());
assert!(rule_without_range.sample_rate(after_time_range).is_some());
assert!(rule_without_range
.evaluate(before_time_range, None)
.is_some());
assert!(rule_without_range
.evaluate(during_time_range, None)
.is_some());
assert!(rule_without_range
.evaluate(after_time_range, None)
.is_some());
}

/// You can pass in a SamplingValue of either variant, and it should return the same one if
Expand All @@ -639,13 +669,13 @@ mod tests {
};

matches!(
rule.sample_rate(Utc::now()).unwrap(),
rule.evaluate(Utc::now(), None).unwrap(),
SamplingValue::SampleRate { .. }
);

rule.sampling_value = SamplingValue::Factor { value: 0.42 };
matches!(
rule.sample_rate(Utc::now()).unwrap(),
rule.evaluate(Utc::now(), None).unwrap(),
SamplingValue::Factor { .. }
);
}
Expand Down
Loading