From 16ade3aa345b8172a3db8dad3550b4795bb2b1fe Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Thu, 23 Jan 2025 14:28:26 +0100 Subject: [PATCH] feat(metrics): Add sets Inspired by https://github.com/getsentry/relay/pull/4446/commits/2fce818c498ff090ffdf4b1fbc7f83a0981d8495. --- crates/symbolicator-service/src/metrics.rs | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/symbolicator-service/src/metrics.rs b/crates/symbolicator-service/src/metrics.rs index 267b299fc..0b0a836d8 100644 --- a/crates/symbolicator-service/src/metrics.rs +++ b/crates/symbolicator-service/src/metrics.rs @@ -1,5 +1,5 @@ //! Provides access to the metrics sytem. -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::fmt::Write; use std::net::ToSocketAddrs; use std::ops::Deref; @@ -211,6 +211,7 @@ struct AggregationKey { } type AggregatedCounters = FxHashMap; +type AggregatedSets = FxHashMap>; type AggregatedDistributions = FxHashMap>; pub trait IntoDistributionValue { @@ -248,6 +249,8 @@ pub struct LocalAggregator { buf: String, /// A map of all the `counter` and `gauge` metrics we have aggregated thus far. aggregated_counters: AggregatedCounters, + /// A map of all the `set` metrics we have aggregated thus far. + aggregated_sets: AggregatedSets, /// A map of all the `timer` and `histogram` metrics we have aggregated thus far. aggregated_distributions: AggregatedDistributions, } @@ -288,6 +291,20 @@ impl LocalAggregator { *aggregation += value; } + /// Emit a `set` metric, which is aggregated in a set. + pub fn emit_set(&mut self, name: &'static str, value: u64, tags: &[(&'static str, &str)]) { + let tags = self.format_tags(tags); + + let key = AggregationKey { + ty: "|s", + name, + tags, + }; + + let aggregation = self.aggregated_sets.entry(key).or_default(); + aggregation.insert(value); + } + /// Emit a `gauge` metric, for which only the latest value is retained. pub fn emit_gauge(&mut self, name: &'static str, value: u64, tags: &[(&'static str, &str)]) { let tags = self.format_tags(tags); @@ -400,6 +417,16 @@ macro_rules! metric { }); }}; + // sets + (set($id:expr) = $value:expr $(, $k:ident = $v:expr)* $(,)?) => { + $crate::with_client(|local| { + let tags: &[(&'static str, &str)] = &[ + $(($k, $v)),* + ]; + local.emit_set(&$crate::types::SetMetric::name(&$id), $value, tags); + }); + }; + // gauges (gauge($id:expr) = $value:expr $(, $k:expr => $v:expr)* $(,)?) => {{ $crate::metrics::with_client(|local| {