Skip to content

Commit

Permalink
ref(rules): Move rules DSL to relay-protocol (#2608)
Browse files Browse the repository at this point in the history
  • Loading branch information
iambriccardo authored Oct 16, 2023
1 parent e5bc283 commit 4ed7d5f
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- Introduce a dedicated usage metric for transactions that replaces the duration metric. ([#2571](https://github.com/getsentry/relay/pull/2571), [#2589](https://github.com/getsentry/relay/pull/2589))
- Restore the profiling killswitch. ([#2573](https://github.com/getsentry/relay/pull/2573))
- Add `scraping_attempts` field to the event schema. ([#2575](https://github.com/getsentry/relay/pull/2575))
- Move `condition.rs` from `relay-sampling` to `relay-protocol`. ([#2608](https://github.com/getsentry/relay/pull/2608))

## 23.9.1

Expand Down
8 changes: 6 additions & 2 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions relay-cabi/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use relay_event_schema::protocol::{Event, VALID_PLATFORMS};
use relay_pii::{
selector_suggestions_from_value, DataScrubbingConfig, PiiConfig, PiiConfigError, PiiProcessor,
};
use relay_protocol::{Annotated, Remark};
use relay_sampling::condition::RuleCondition;
use relay_protocol::{Annotated, Remark, RuleCondition};
use relay_sampling::SamplingConfig;

use crate::core::{RelayBuf, RelayStr};
Expand Down
1 change: 1 addition & 0 deletions relay-dynamic-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ relay-common = { path = "../relay-common" }
relay-filter = { path = "../relay-filter" }
relay-event-normalization = { path = "../relay-event-normalization" }
relay-pii = { path = "../relay-pii" }
relay-protocol = { path = "../relay-protocol" }
relay-quotas = { path = "../relay-quotas" }
relay-sampling = { path = "../relay-sampling" }
serde = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion relay-dynamic-config/src/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use relay_base_schema::data_category::DataCategory;
use relay_common::glob2::LazyGlob;
use relay_sampling::condition::RuleCondition;
use relay_protocol::RuleCondition;

use crate::feature::Feature;
use crate::metrics::{MetricExtractionConfig, MetricSpec, TagMapping, TagSpec};
Expand Down
2 changes: 1 addition & 1 deletion relay-dynamic-config/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeSet;

use relay_base_schema::data_category::DataCategory;
use relay_common::glob2::LazyGlob;
use relay_sampling::condition::RuleCondition;
use relay_protocol::RuleCondition;
use serde::{Deserialize, Serialize};

use crate::project::ProjectConfig;
Expand Down
3 changes: 3 additions & 0 deletions relay-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ publish = false

[dependencies]
num-traits = "0.2.12"
relay-common = { path = "../relay-common" }
relay-protocol-derive = { path = "../relay-protocol-derive", optional = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
serde_json = { workspace = true }
smallvec = { workspace = true }
uuid = { workspace = true }
unicase = "2.6.0"

[dev-dependencies]
insta = { workspace = true }
similar-asserts = { workspace = true }

[features]
Expand Down
100 changes: 52 additions & 48 deletions relay-sampling/src/condition.rs → relay-protocol/src/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
//! The root type is [`RuleCondition`].

use relay_common::glob3::GlobPatterns;
use relay_protocol::{Getter, Val};
use serde::{Deserialize, Serialize};
use serde_json::{Number, Value};

use crate::utils;
use crate::{Getter, Val};

/// Options for [`EqCondition`].
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
Expand Down Expand Up @@ -326,7 +326,7 @@ impl NotCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = !RuleCondition::eq("obj.status", "invalid");
/// ```
Expand All @@ -343,7 +343,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::eq("obj.status", "invalid");
/// ```
Expand All @@ -354,7 +354,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::gte("obj.length", 10);
/// ```
Expand All @@ -365,7 +365,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::lte("obj.length", 10);
/// ```
Expand All @@ -376,7 +376,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::gt("obj.length", 10);
/// ```
Expand All @@ -387,7 +387,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::lt("obj.length", 10);
/// ```
Expand All @@ -398,7 +398,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::glob("obj.name", "error: *");
/// ```
Expand All @@ -409,7 +409,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::eq("obj.status", "invalid")
/// | RuleCondition::eq("obj.status", "unknown");
Expand All @@ -421,7 +421,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::eq("obj.status", "invalid")
/// & RuleCondition::gte("obj.length", 10);
Expand All @@ -433,7 +433,7 @@ pub enum RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = !RuleCondition::eq("obj.status", "invalid");
/// ```
Expand Down Expand Up @@ -465,7 +465,7 @@ impl RuleCondition {
/// # Examples
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// // Matches if the value is identical to the given string:
/// let condition = RuleCondition::eq("obj.status", "invalid");
Expand All @@ -485,7 +485,7 @@ impl RuleCondition {
/// # Examples
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// // Matches if the value is identical to the given string:
/// let condition = RuleCondition::eq_ignore_case("obj.status", "invalid");
Expand All @@ -502,7 +502,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// // Match a single pattern:
/// let condition = RuleCondition::glob("obj.name", "error: *");
Expand All @@ -519,7 +519,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::gt("obj.length", 10);
/// ```
Expand All @@ -532,7 +532,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::gte("obj.length", 10);
/// ```
Expand All @@ -545,7 +545,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::lt("obj.length", 10);
/// ```
Expand All @@ -558,7 +558,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::lte("obj.length", 10);
/// ```
Expand All @@ -573,7 +573,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::eq("obj.status", "invalid")
/// & RuleCondition::gte("obj.length", 10);
Expand All @@ -596,7 +596,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = RuleCondition::eq("obj.status", "invalid")
/// | RuleCondition::eq("obj.status", "unknown");
Expand All @@ -619,7 +619,7 @@ impl RuleCondition {
/// # Example
///
/// ```
/// use relay_sampling::condition::RuleCondition;
/// use relay_protocol::RuleCondition;
///
/// let condition = !RuleCondition::eq("obj.status", "invalid");
/// ```
Expand Down Expand Up @@ -698,31 +698,35 @@ impl std::ops::Not for RuleCondition {

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;

use relay_base_schema::project::ProjectKey;
use uuid::Uuid;
use super::*;

use crate::dsc::TraceUserContext;
use crate::DynamicSamplingContext;
struct MockDSC {
transaction: String,
release: String,
environment: String,
user_segment: String,
}

use super::*;
impl Getter for MockDSC {
fn get_value(&self, path: &str) -> Option<Val<'_>> {
Some(match path.strip_prefix("trace.")? {
"transaction" => self.transaction.as_str().into(),
"release" => self.release.as_str().into(),
"environment" => self.environment.as_str().into(),
"user.segment" => self.user_segment.as_str().into(),
_ => {
return None;
}
})
}
}

fn dsc_dummy() -> DynamicSamplingContext {
DynamicSamplingContext {
trace_id: Uuid::new_v4(),
public_key: ProjectKey::parse("abd0f232775f45feab79864e580d160b").unwrap(),
release: Some("1.1.1".to_string()),
user: TraceUserContext {
user_segment: "vip".to_owned(),
user_id: "user-id".to_owned(),
},
replay_id: Some(Uuid::new_v4()),
environment: Some("debug".to_string()),
transaction: Some("transaction1".into()),
sample_rate: None,
sampled: None,
other: BTreeMap::new(),
fn mock_dsc() -> MockDSC {
MockDSC {
transaction: "transaction1".to_string(),
release: "1.1.1".to_string(),
environment: "debug".to_string(),
user_segment: "vip".to_string(),
}
}

Expand Down Expand Up @@ -928,7 +932,7 @@ mod tests {
("match no conditions", RuleCondition::all()),
];

let dsc = dsc_dummy();
let dsc = mock_dsc();

for (rule_test_name, condition) in conditions.iter() {
let failure_name = format!("Failed on test: '{rule_test_name}'!!!");
Expand Down Expand Up @@ -971,7 +975,7 @@ mod tests {
("never", false, RuleCondition::never()),
];

let dsc = dsc_dummy();
let dsc = mock_dsc();

for (rule_test_name, expected, condition) in conditions.iter() {
let failure_name = format!("Failed on test: '{rule_test_name}'!!!");
Expand Down Expand Up @@ -1014,7 +1018,7 @@ mod tests {
("all", true, RuleCondition::all()),
];

let dsc = dsc_dummy();
let dsc = mock_dsc();

for (rule_test_name, expected, condition) in conditions.iter() {
let failure_name = format!("Failed on test: '{rule_test_name}'!!!");
Expand All @@ -1037,7 +1041,7 @@ mod tests {
),
];

let dsc = dsc_dummy();
let dsc = mock_dsc();

for (rule_test_name, expected, condition) in conditions.iter() {
let failure_name = format!("Failed on test: '{rule_test_name}'!!!");
Expand Down Expand Up @@ -1075,7 +1079,7 @@ mod tests {
),
];

let dsc = dsc_dummy();
let dsc = mock_dsc();

for (rule_test_name, condition) in conditions.iter() {
let failure_name = format!("Failed on test: '{rule_test_name}'!!!");
Expand Down
Loading

0 comments on commit 4ed7d5f

Please sign in to comment.