diff --git a/relay-metrics/src/aggregator/mod.rs b/relay-metrics/src/aggregator/mod.rs index 4ce590ba01f..7f61cbbb30c 100644 --- a/relay-metrics/src/aggregator/mod.rs +++ b/relay-metrics/src/aggregator/mod.rs @@ -23,6 +23,7 @@ mod stats; pub use self::config::*; use self::inner::{BucketData, BucketKey}; +/// Default amount of partitions per second when there are no partitions configured. const DEFAULT_PARTITIONS_PER_SECOND: u32 = 64; /// Any error that may occur during aggregation. diff --git a/relay-server/src/services/metrics/aggregator.rs b/relay-server/src/services/metrics/aggregator.rs index 9803414e4a3..961f28c4fc9 100644 --- a/relay-server/src/services/metrics/aggregator.rs +++ b/relay-server/src/services/metrics/aggregator.rs @@ -544,70 +544,83 @@ mod tests { assert_eq!(receiver.bucket_count(), 1); } - // #[test] - // fn test_validate_bucket_key_str_length() { - // relay_test::setup(); - // let project_key = ProjectKey::parse("a94ae32be2584e0bbd7a4cbb95971fee").unwrap(); - // - // let short_metric = BucketKey { - // project_key, - // timestamp: UnixTimestamp::now(), - // metric_name: "c:transactions/a_short_metric".into(), - // tags: BTreeMap::new(), - // extracted_from_indexed: false, - // }; - // assert!(validate_bucket_key(short_metric, &test_config()).is_ok()); - // - // let long_metric = BucketKey { - // project_key, - // timestamp: UnixTimestamp::now(), - // metric_name: "c:transactions/long_name_a_very_long_name_its_super_long_really_but_like_super_long_probably_the_longest_name_youve_seen_and_even_the_longest_name_ever_its_extremly_long_i_cant_tell_how_long_it_is_because_i_dont_have_that_many_fingers_thus_i_cant_count_the_many_characters_this_long_name_is".into(), - // tags: BTreeMap::new(), - // extracted_from_indexed: false, - // }; - // let validation = validate_bucket_key(long_metric, &test_config()); - // - // assert!(matches!( - // validation.unwrap_err(), - // AggregateMetricsError::InvalidStringLength(_), - // )); - // - // let short_metric_long_tag_key = BucketKey { - // project_key, - // timestamp: UnixTimestamp::now(), - // metric_name: "c:transactions/a_short_metric_with_long_tag_key".into(), - // tags: BTreeMap::from([("i_run_out_of_creativity_so_here_we_go_Lorem_Ipsum_is_simply_dummy_text_of_the_printing_and_typesetting_industry_Lorem_Ipsum_has_been_the_industrys_standard_dummy_text_ever_since_the_1500s_when_an_unknown_printer_took_a_galley_of_type_and_scrambled_it_to_make_a_type_specimen_book".into(), "tag_value".into())]), - // extracted_from_indexed: false, - // }; - // let validation = validate_bucket_key(short_metric_long_tag_key, &test_config()).unwrap(); - // assert_eq!(validation.tags.len(), 0); - // - // let short_metric_long_tag_value = BucketKey { - // project_key, - // timestamp: UnixTimestamp::now(), - // metric_name: "c:transactions/a_short_metric_with_long_tag_value".into(), - // tags: BTreeMap::from([("tag_key".into(), "i_run_out_of_creativity_so_here_we_go_Lorem_Ipsum_is_simply_dummy_text_of_the_printing_and_typesetting_industry_Lorem_Ipsum_has_been_the_industrys_standard_dummy_text_ever_since_the_1500s_when_an_unknown_printer_took_a_galley_of_type_and_scrambled_it_to_make_a_type_specimen_book".into())]), - // extracted_from_indexed: false, - // }; - // let validation = validate_bucket_key(short_metric_long_tag_value, &test_config()).unwrap(); - // assert_eq!(validation.tags.len(), 0); - // } - // - // #[test] - // fn test_validate_tag_values_special_chars() { - // relay_test::setup(); - // let project_key = ProjectKey::parse("a94ae32be2584e0bbd7a4cbb95971fee").unwrap(); - // - // let tag_value = "x".repeat(199) + "ø"; - // assert_eq!(tag_value.chars().count(), 200); // Should be allowed - // let short_metric = BucketKey { - // project_key, - // timestamp: UnixTimestamp::now(), - // metric_name: "c:transactions/a_short_metric".into(), - // tags: BTreeMap::from([("foo".into(), tag_value.clone())]), - // extracted_from_indexed: false, - // }; - // let validated_bucket = validate_metric_tags(short_metric, &test_config()); - // assert_eq!(validated_bucket.tags["foo"], tag_value); - // } + fn test_config() -> AggregatorServiceConfig { + AggregatorServiceConfig { + max_name_length: 200, + max_tag_key_length: 200, + max_tag_value_length: 200, + ..Default::default() + } + } + + #[test] + fn test_validate_bucket_key_str_length() { + relay_test::setup(); + let mut short_metric = Bucket { + timestamp: UnixTimestamp::now(), + name: "c:transactions/a_short_metric".into(), + tags: BTreeMap::new(), + metadata: Default::default(), + width: 0, + value: BucketValue::Counter(0.into()), + }; + assert!(validate_bucket(&mut short_metric, &test_config())); + + let mut long_metric = Bucket { + timestamp: UnixTimestamp::now(), + name: "c:transactions/long_name_a_very_long_name_its_super_long_really_but_like_super_long_probably_the_longest_name_youve_seen_and_even_the_longest_name_ever_its_extremly_long_i_cant_tell_how_long_it_is_because_i_dont_have_that_many_fingers_thus_i_cant_count_the_many_characters_this_long_name_is".into(), + tags: BTreeMap::new(), + metadata: Default::default(), + width: 0, + value: BucketValue::Counter(0.into()), + }; + assert!(!validate_bucket(&mut long_metric, &test_config())); + + let mut short_metric_long_tag_key = Bucket { + timestamp: UnixTimestamp::now(), + name: "c:transactions/a_short_metric_with_long_tag_key".into(), + tags: BTreeMap::from([("i_run_out_of_creativity_so_here_we_go_Lorem_Ipsum_is_simply_dummy_text_of_the_printing_and_typesetting_industry_Lorem_Ipsum_has_been_the_industrys_standard_dummy_text_ever_since_the_1500s_when_an_unknown_printer_took_a_galley_of_type_and_scrambled_it_to_make_a_type_specimen_book".into(), "tag_value".into())]), + metadata: Default::default(), + width: 0, + value: BucketValue::Counter(0.into()), + }; + assert!(validate_bucket( + &mut short_metric_long_tag_key, + &test_config() + )); + assert_eq!(short_metric_long_tag_key.tags.len(), 0); + + let mut short_metric_long_tag_value = Bucket { + timestamp: UnixTimestamp::now(), + name: "c:transactions/a_short_metric_with_long_tag_value".into(), + tags: BTreeMap::from([("tag_key".into(), "i_run_out_of_creativity_so_here_we_go_Lorem_Ipsum_is_simply_dummy_text_of_the_printing_and_typesetting_industry_Lorem_Ipsum_has_been_the_industrys_standard_dummy_text_ever_since_the_1500s_when_an_unknown_printer_took_a_galley_of_type_and_scrambled_it_to_make_a_type_specimen_book".into())]), + metadata: Default::default(), + width: 0, + value: BucketValue::Counter(0.into()), + }; + assert!(validate_bucket( + &mut short_metric_long_tag_value, + &test_config() + )); + assert_eq!(short_metric_long_tag_value.tags.len(), 0); + } + + #[test] + fn test_validate_tag_values_special_chars() { + relay_test::setup(); + + let tag_value = "x".repeat(199) + "ø"; + assert_eq!(tag_value.chars().count(), 200); // Should be allowed + + let mut short_metric = Bucket { + timestamp: UnixTimestamp::now(), + name: "c:transactions/a_short_metric".into(), + tags: BTreeMap::from([("foo".into(), tag_value.clone())]), + metadata: Default::default(), + width: 0, + value: BucketValue::Counter(0.into()), + }; + assert!(validate_bucket(&mut short_metric, &test_config())); + assert_eq!(short_metric.tags["foo"], tag_value); + } }