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(metrics): Add sets #1600

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all 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
29 changes: 28 additions & 1 deletion crates/symbolicator-service/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -211,6 +211,7 @@ struct AggregationKey {
}

type AggregatedCounters = FxHashMap<AggregationKey, i64>;
type AggregatedSets = FxHashMap<AggregationKey, BTreeSet<u64>>;
type AggregatedDistributions = FxHashMap<AggregationKey, Vec<f64>>;

pub trait IntoDistributionValue {
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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| {
Expand Down
Loading