Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Jun 7, 2022
1 parent 44f0baf commit ec63f36
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
3 changes: 3 additions & 0 deletions relay-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,8 @@ pub enum KafkaTopic {
OutcomesBilling,
/// Session health updates.
Sessions,
/// The default topic for metrics. We use this mainly for tests at the moment.
MetricsDefault,
/// Any metric that is extracted from sessions.
MetricsSessions,
/// Any metric that is extracted from transactions.
Expand Down Expand Up @@ -821,6 +823,7 @@ impl TopicAssignments {
KafkaTopic::Outcomes => &self.outcomes,
KafkaTopic::OutcomesBilling => self.outcomes_billing.as_ref().unwrap_or(&self.outcomes),
KafkaTopic::Sessions => &self.sessions,
KafkaTopic::MetricsDefault => &self.metrics,
KafkaTopic::MetricsSessions => self.metrics_sessions.as_ref().unwrap_or(&self.metrics),
KafkaTopic::MetricsTransactions => {
self.metrics_transactions.as_ref().unwrap_or(&self.metrics)
Expand Down
34 changes: 17 additions & 17 deletions relay-metrics/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl std::str::FromStr for MetricResourceIdentifier {
let (raw_ty, rest) = name.split_once(':').ok_or(ParseMetricError(()))?;
let ty = raw_ty.parse()?;

let (raw_namespace, rest) = rest.split_once('/').ok_or(ParseMetricError(()))?;
let (raw_namespace, rest) = rest.split_once('/').unwrap_or(("custom", rest));
let namespace = raw_namespace.to_owned();

let (name, unit) = parse_name_unit(rest).ok_or(ParseMetricError(()))?;
Expand Down Expand Up @@ -552,12 +552,12 @@ mod tests {

#[test]
fn test_parse_counter() {
let s = "transactions/foo:42|c";
let s = "foo:42|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "c:transactions/foo",
name: "c:foo",
unit: None,
value: Counter(
42.0,
Expand All @@ -570,12 +570,12 @@ mod tests {

#[test]
fn test_parse_distribution() {
let s = "transactions/foo:17.5|d";
let s = "foo:17.5|d";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "d:transactions/foo",
name: "d:foo",
unit: None,
value: Distribution(
17.5,
Expand All @@ -588,20 +588,20 @@ mod tests {

#[test]
fn test_parse_histogram() {
let s = "transactions/foo:17.5|h"; // common alias for distribution
let s = "foo:17.5|h"; // common alias for distribution
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
assert_eq!(metric.value, MetricValue::Distribution(17.5));
}

#[test]
fn test_parse_set() {
let s = "transactions/foo:e2546e4c-ecd0-43ad-ae27-87960e57a658|s";
let s = "foo:e2546e4c-ecd0-43ad-ae27-87960e57a658|s";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "s:transactions/foo",
name: "s:foo",
unit: None,
value: Set(
4267882815,
Expand All @@ -614,12 +614,12 @@ mod tests {

#[test]
fn test_parse_gauge() {
let s = "transactions/foo:42|g";
let s = "foo:42|g";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "g:transactions/foo",
name: "g:foo",
unit: None,
value: Gauge(
42.0,
Expand All @@ -632,23 +632,23 @@ mod tests {

#[test]
fn test_parse_unit() {
let s = "transactions/foo@second:17.5|d";
let s = "foo@second:17.5|d";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
assert_eq!(metric.unit, MetricUnit::Duration(DurationUnit::Second));
}

#[test]
fn test_parse_unit_regression() {
let s = "transactions/foo@s:17.5|d";
let s = "foo@s:17.5|d";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
assert_eq!(metric.unit, MetricUnit::Duration(DurationUnit::Second));
}

#[test]
fn test_parse_tags() {
let s = "transactions/foo:17.5|d|#foo,bar:baz";
let s = "foo:17.5|d|#foo,bar:baz";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric.tags, @r###"
Expand Down Expand Up @@ -745,7 +745,7 @@ mod tests {

#[test]
fn test_parse_all() {
let s = "transactions/foo:42|c\nbar:17|c";
let s = "foo:42|c\nbar:17|c";
let timestamp = UnixTimestamp::from_secs(4711);

let metrics: Vec<Metric> = Metric::parse_all(s.as_bytes(), timestamp)
Expand All @@ -757,7 +757,7 @@ mod tests {

#[test]
fn test_parse_all_crlf() {
let s = "transactions/foo:42|c\r\nbar:17|c";
let s = "foo:42|c\r\nbar:17|c";
let timestamp = UnixTimestamp::from_secs(4711);

let metrics: Vec<Metric> = Metric::parse_all(s.as_bytes(), timestamp)
Expand All @@ -769,7 +769,7 @@ mod tests {

#[test]
fn test_parse_all_empty_lines() {
let s = "transactions/foo:42|c\n\n\nbar:17|c";
let s = "foo:42|c\n\n\nbar:17|c";
let timestamp = UnixTimestamp::from_secs(4711);

let metric_count = Metric::parse_all(s.as_bytes(), timestamp).count();
Expand All @@ -778,7 +778,7 @@ mod tests {

#[test]
fn test_parse_all_trailing() {
let s = "transactions/foo:42|c\nbar:17|c\n";
let s = "foo:42|c\nbar:17|c\n";
let timestamp = UnixTimestamp::from_secs(4711);

let metric_count = Metric::parse_all(s.as_bytes(), timestamp).count();
Expand Down
15 changes: 8 additions & 7 deletions relay-server/src/actors/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct Producers {
attachments: Producer,
transactions: Producer,
sessions: Producer,
metrics_default: Producer,
metrics_sessions: Producer,
metrics_transactions: Producer,
profiles: Producer,
Expand All @@ -75,6 +76,7 @@ impl Producers {
None
}
KafkaTopic::Sessions => Some(&self.sessions),
KafkaTopic::MetricsDefault => Some(&self.metrics_default),
KafkaTopic::MetricsSessions => Some(&self.metrics_sessions),
KafkaTopic::MetricsTransactions => Some(&self.metrics_transactions),
KafkaTopic::Profiles => Some(&self.profiles),
Expand Down Expand Up @@ -133,6 +135,11 @@ impl StoreForwarder {
events: make_producer(&*config, &mut reused_producers, KafkaTopic::Events)?,
transactions: make_producer(&*config, &mut reused_producers, KafkaTopic::Transactions)?,
sessions: make_producer(&*config, &mut reused_producers, KafkaTopic::Sessions)?,
metrics_default: make_producer(
&*config,
&mut reused_producers,
KafkaTopic::MetricsDefault,
)?,
metrics_sessions: make_producer(
&*config,
&mut reused_producers,
Expand Down Expand Up @@ -392,13 +399,7 @@ impl StoreForwarder {
Ok(MetricResourceIdentifier { namespace, .. }) if namespace == "sessions" => {
KafkaTopic::MetricsSessions
}
_ => {
relay_log::configure_scope(|scope| {
scope.set_extra("metric_message.name", message.name.into());
});
relay_log::error!("Dropping unknown metric usecase");
return Ok(());
}
_ => KafkaTopic::MetricsDefault,
};

relay_log::trace!("Sending metric message to kafka");
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_metrics(mini_sentry, relay):
mini_sentry.add_basic_project_config(project_id)

timestamp = int(datetime.now(tz=timezone.utc).timestamp())
metrics_payload = f"foo:42|c\ntransactions/bar:17|c"
metrics_payload = f"foo:42|c\nbar:17|c"
relay.send_metrics(project_id, metrics_payload, timestamp)

envelope = mini_sentry.captured_events.get(timeout=3)
Expand Down Expand Up @@ -111,7 +111,7 @@ def test_metrics_with_processing(mini_sentry, relay_with_processing, metrics_con
mini_sentry.add_full_project_config(project_id)

timestamp = int(datetime.now(tz=timezone.utc).timestamp())
metrics_payload = f"foo:42|c\ntransactions/bar@second:17|c"
metrics_payload = f"foo:42|c\nbar@second:17|c"
relay.send_metrics(project_id, metrics_payload, timestamp)

metrics = metrics_by_name(metrics_consumer, 2)
Expand Down

0 comments on commit ec63f36

Please sign in to comment.