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(metric-stats): Add metric stats namespace #3267

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 29 additions & 0 deletions relay-base-schema/src/metrics/mri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ pub enum MetricNamespace {
Profiles,
/// User-defined metrics directly sent by SDKs and applications.
Custom,
/// Metric stats.
///
/// Metrics about metrics.
MetricStats,
Dav1dde marked this conversation as resolved.
Show resolved Hide resolved
/// An unknown and unsupported metric.
///
/// Metrics that Relay either doesn't know or recognize the namespace of will be dropped before
Expand All @@ -129,6 +133,19 @@ pub enum MetricNamespace {
}

impl MetricNamespace {
/// Returns all namespaces/variants of this enum.
pub fn all() -> [Self; 7] {
[
MetricNamespace::Sessions,
MetricNamespace::Transactions,
MetricNamespace::Spans,
MetricNamespace::Profiles,
MetricNamespace::Custom,
MetricNamespace::MetricStats,
MetricNamespace::Unsupported,
]
}

/// Returns the string representation for this metric type.
pub fn as_str(&self) -> &'static str {
match self {
Expand All @@ -137,6 +154,7 @@ impl MetricNamespace {
MetricNamespace::Spans => "spans",
MetricNamespace::Profiles => "profiles",
MetricNamespace::Custom => "custom",
MetricNamespace::MetricStats => "metric_stats",
MetricNamespace::Unsupported => "unsupported",
}
}
Expand All @@ -152,6 +170,7 @@ impl std::str::FromStr for MetricNamespace {
"spans" => Ok(Self::Spans),
"profiles" => Ok(Self::Profiles),
"custom" => Ok(Self::Custom),
"metric_stats" => Ok(Self::MetricStats),
_ => Ok(Self::Unsupported),
}
}
Expand Down Expand Up @@ -337,6 +356,16 @@ mod tests {
assert_eq!(std::mem::align_of::<MetricUnit>(), 1);
}

#[test]
fn test_metric_namespaces_conversion() {
for namespace in MetricNamespace::all() {
assert_eq!(
namespace,
namespace.as_str().parse::<MetricNamespace>().unwrap()
);
}
}

#[test]
fn test_parse_mri_lenient() {
assert_eq!(
Expand Down
3 changes: 3 additions & 0 deletions relay-cogs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ pub enum AppFeature {
MetricsSessions,
/// Metrics in the custom namespace.
MetricsCustom,
/// Metrics in the `metric_stats` namespace.
MetricsMetricStats,
Dav1dde marked this conversation as resolved.
Show resolved Hide resolved
/// Metrics in the unsupported namespace.
///
/// This is usually not emitted, since metrics in the unsupported
Expand Down Expand Up @@ -168,6 +170,7 @@ impl AppFeature {
Self::MetricsProfiles => "metrics_profiles",
Self::MetricsSessions => "metrics_sessions",
Self::MetricsCustom => "metrics_custom",
Self::MetricsMetricStats => "metrics_metric_stats",
Self::MetricsUnsupported => "metrics_unsupported",
}
}
Expand Down
10 changes: 8 additions & 2 deletions relay-dynamic-config/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub struct BucketEncodings {
spans: BucketEncoding,
profiles: BucketEncoding,
custom: BucketEncoding,
metric_stats: BucketEncoding,
}

impl BucketEncodings {
Expand All @@ -196,6 +197,7 @@ impl BucketEncodings {
MetricNamespace::Spans => self.spans,
MetricNamespace::Profiles => self.profiles,
MetricNamespace::Custom => self.custom,
MetricNamespace::MetricStats => self.metric_stats,
// Always force the legacy encoding for sessions,
// sessions are not part of the generic metrics platform with different
// consumer which are not (yet) updated to support the new data.
Expand Down Expand Up @@ -231,6 +233,7 @@ where
spans: encoding,
profiles: encoding,
custom: encoding,
metric_stats: encoding,
})
}

Expand Down Expand Up @@ -424,7 +427,8 @@ mod tests {
transactions: BucketEncoding::Legacy,
spans: BucketEncoding::Legacy,
profiles: BucketEncoding::Legacy,
custom: BucketEncoding::Legacy
custom: BucketEncoding::Legacy,
metric_stats: BucketEncoding::Legacy,
}
);
assert_eq!(
Expand All @@ -433,7 +437,8 @@ mod tests {
transactions: BucketEncoding::Zstd,
spans: BucketEncoding::Zstd,
profiles: BucketEncoding::Zstd,
custom: BucketEncoding::Zstd
custom: BucketEncoding::Zstd,
metric_stats: BucketEncoding::Zstd,
}
);
}
Expand All @@ -445,6 +450,7 @@ mod tests {
spans: BucketEncoding::Zstd,
profiles: BucketEncoding::Base64,
custom: BucketEncoding::Zstd,
metric_stats: BucketEncoding::Base64,
};
let s = serde_json::to_string(&original).unwrap();
let s = format!(
Expand Down
1 change: 1 addition & 0 deletions relay-metrics/src/cogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn to_app_feature(ns: MetricNamespace) -> AppFeature {
MetricNamespace::Spans => AppFeature::MetricsSpans,
MetricNamespace::Profiles => AppFeature::MetricsProfiles,
MetricNamespace::Custom => AppFeature::MetricsCustom,
MetricNamespace::MetricStats => AppFeature::MetricsMetricStats,
MetricNamespace::Unsupported => AppFeature::MetricsUnsupported,
}
}
Expand Down
1 change: 1 addition & 0 deletions relay-server/src/services/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ fn is_metric_namespace_valid(state: &ProjectState, namespace: &MetricNamespace)
}
MetricNamespace::Profiles => true,
MetricNamespace::Custom => state.has_feature(Feature::CustomMetrics),
MetricNamespace::MetricStats => false,
MetricNamespace::Unsupported => false,
}
}
Expand Down
15 changes: 5 additions & 10 deletions relay-server/src/utils/metric_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct MetricStats {
spans: Stat,
profiles: Stat,
transactions: Stat,
metric_stats: Stat,
unsupported: Stat,
}

Expand All @@ -38,16 +39,9 @@ impl MetricStats {
/// - `calls`: incremented by one for each seen namespace.
/// - `count`: total amount of metric buckets by namespace.
/// - `cost`: total cost of metric buckets by namespace.
pub fn emit(self, calls: RelayCounters, count: RelayCounters, cost: RelayCounters) {
let stats = [
(MetricNamespace::Custom, self.custom),
(MetricNamespace::Sessions, self.sessions),
(MetricNamespace::Spans, self.spans),
(MetricNamespace::Transactions, self.transactions),
(MetricNamespace::Unsupported, self.unsupported),
];

for (namespace, stat) in stats {
pub fn emit(mut self, calls: RelayCounters, count: RelayCounters, cost: RelayCounters) {
for namespace in MetricNamespace::all() {
let stat = self.stat(namespace);
if stat.count > 0 {
metric!(counter(calls) += 1, namespace = namespace.as_str());
metric!(counter(count) += stat.count, namespace = namespace.as_str());
Expand All @@ -63,6 +57,7 @@ impl MetricStats {
MetricNamespace::Spans => &mut self.spans,
MetricNamespace::Profiles => &mut self.profiles,
MetricNamespace::Custom => &mut self.custom,
MetricNamespace::MetricStats => &mut self.metric_stats,
MetricNamespace::Unsupported => &mut self.unsupported,
}
}
Expand Down
Loading