From 762abe893da537c763f12d52c40fab6095d741a0 Mon Sep 17 00:00:00 2001 From: Chris H-C Date: Fri, 18 Oct 2024 16:02:03 -0400 Subject: [PATCH] bug 1925346 - Implement new metric type labeled_quantity --- CHANGELOG.md | 2 ++ glean-core/src/lib.rs | 2 +- glean-core/src/metrics/labeled.rs | 16 ++++++++++++-- glean-core/src/metrics/mod.rs | 2 +- glean-core/src/metrics/quantity.rs | 22 +++++++++++++++++-- glean-core/tests/labeled.rs | 34 ++++++++++++++++++++++++++++++ samples/rust/metrics.yaml | 8 +++++++ 7 files changed, 80 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6de8152893..55e848612a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ * **BREAKING**: Remove LMDB-to-safe-mode migration. Safe-mode became the default in Glean v51. ([#TODO]()) * **BREAKING**: Stop sending buckets with 0 counts in memory_distribution and timing_distribution metric payloads ([bug 1898336](https://bugzilla.mozilla.org/show_bug.cgi?id=1898336)) +* Rust + * New Metric Type: `labeled_quantity` ([bug 1925346](https://bugzilla.mozilla.org/show_bug.cgi?id=1925346)) # v61.2.0 (2024-10-07) diff --git a/glean-core/src/lib.rs b/glean-core/src/lib.rs index c557342d29..a554f462f4 100644 --- a/glean-core/src/lib.rs +++ b/glean-core/src/lib.rs @@ -65,7 +65,7 @@ pub use crate::histogram::HistogramType; pub use crate::metrics::labeled::{ AllowLabeled, LabeledBoolean, LabeledCounter, LabeledCustomDistribution, LabeledMemoryDistribution, LabeledMetric, LabeledMetricData, LabeledString, - LabeledTimingDistribution, + LabeledTimingDistribution, LabeledQuantity }; pub use crate::metrics::{ BooleanMetric, CounterMetric, CustomDistributionMetric, Datetime, DatetimeMetric, diff --git a/glean-core/src/metrics/labeled.rs b/glean-core/src/metrics/labeled.rs index 7b8846e1d1..4824a6999d 100644 --- a/glean-core/src/metrics/labeled.rs +++ b/glean-core/src/metrics/labeled.rs @@ -11,7 +11,7 @@ use crate::error_recording::{record_error, test_get_num_recorded_errors, ErrorTy use crate::histogram::HistogramType; use crate::metrics::{ BooleanMetric, CounterMetric, CustomDistributionMetric, MemoryDistributionMetric, MemoryUnit, - Metric, MetricType, StringMetric, TimeUnit, TimingDistributionMetric, + Metric, MetricType, QuantityMetric, StringMetric, TimeUnit, TimingDistributionMetric, }; use crate::Glean; @@ -37,6 +37,9 @@ pub type LabeledMemoryDistribution = LabeledMetric; /// A labeled timing_distribution. pub type LabeledTimingDistribution = LabeledMetric; +/// A labeled quantity +pub type LabeledQuantity = LabeledMetric; + /// The metric data needed to construct inner submetrics. /// /// Different Labeled metrics require different amounts and kinds of information to @@ -90,7 +93,7 @@ mod private { use super::LabeledMetricData; use crate::metrics::{ BooleanMetric, CounterMetric, CustomDistributionMetric, MemoryDistributionMetric, - StringMetric, TimingDistributionMetric, + QuantityMetric, StringMetric, TimingDistributionMetric, }; /// The sealed labeled trait. @@ -162,6 +165,15 @@ mod private { } } } + + impl Sealed for QuantityMetric { + fn new_inner(meta: LabeledMetricData) -> Self { + match meta { + LabeledMetricData::Common { cmd } => Self::new(cmd), + _ => panic!("Incorrect construction of Labeled"), + } + } + } } /// Trait for metrics that can be nested inside a labeled metric. diff --git a/glean-core/src/metrics/mod.rs b/glean-core/src/metrics/mod.rs index dfd8350bed..741fe1fbc5 100644 --- a/glean-core/src/metrics/mod.rs +++ b/glean-core/src/metrics/mod.rs @@ -54,7 +54,7 @@ pub use self::event::EventMetric; pub(crate) use self::experiment::ExperimentMetric; pub use self::labeled::{ LabeledBoolean, LabeledCounter, LabeledCustomDistribution, LabeledMemoryDistribution, - LabeledMetric, LabeledString, LabeledTimingDistribution, + LabeledMetric, LabeledQuantity, LabeledString, LabeledTimingDistribution, }; pub use self::memory_distribution::{LocalMemoryDistribution, MemoryDistributionMetric}; pub use self::memory_unit::MemoryUnit; diff --git a/glean-core/src/metrics/quantity.rs b/glean-core/src/metrics/quantity.rs index 92216625d6..2e3556f483 100644 --- a/glean-core/src/metrics/quantity.rs +++ b/glean-core/src/metrics/quantity.rs @@ -2,6 +2,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +use std::sync::Arc; + use crate::common_metric_data::CommonMetricDataInternal; use crate::error_recording::{record_error, test_get_num_recorded_errors, ErrorType}; use crate::metrics::Metric; @@ -15,13 +17,29 @@ use crate::Glean; /// Used to store explicit non-negative integers. #[derive(Clone, Debug)] pub struct QuantityMetric { - meta: CommonMetricDataInternal, + meta: Arc, } impl MetricType for QuantityMetric { fn meta(&self) -> &CommonMetricDataInternal { &self.meta } + + fn with_name(&self, name: String) -> Self { + let mut meta = (*self.meta).clone(); + meta.inner.name = name; + Self { + meta: Arc::new(meta), + } + } + + fn with_dynamic_label(&self, label: String) -> Self { + let mut meta = (*self.meta).clone(); + meta.inner.dynamic_label = Some(label); + Self { + meta: Arc::new(meta), + } + } } // IMPORTANT: @@ -31,7 +49,7 @@ impl MetricType for QuantityMetric { impl QuantityMetric { /// Creates a new quantity metric. pub fn new(meta: CommonMetricData) -> Self { - Self { meta: meta.into() } + Self { meta: Arc::new(meta.into()) } } /// Sets the value. Must be non-negative. diff --git a/glean-core/tests/labeled.rs b/glean-core/tests/labeled.rs index c506a1aa7a..0a0075a055 100644 --- a/glean-core/tests/labeled.rs +++ b/glean-core/tests/labeled.rs @@ -222,6 +222,40 @@ fn can_create_labeled_timing_distribution_metric() { ); } +#[test] +fn can_create_labeled_quantity_metric() { + let (glean, _t) = new_glean(None); + let labeled = LabeledQuantity::new( + LabeledMetricData::Common { + cmd: CommonMetricData { + name: "labeled_metric".into(), + category: "telemetry".into(), + send_in_pings: vec!["store1".into()], + disabled: false, + lifetime: Lifetime::Ping, + ..Default::default() + } + }, + Some(vec!["label1".into()]), + ); + + let metric = labeled.get("label1"); + metric.set_sync(&glean, 42); + + let snapshot = StorageManager + .snapshot_as_json(glean.storage(), "store1", true) + .unwrap(); + + assert_eq!( + json!({ + "labeled_quantity": { + "telemetry.labeled_metric": { "label1": 42, }, + } + }), + snapshot + ); +} + #[test] fn can_use_multiple_labels() { let (glean, _t) = new_glean(None); diff --git a/samples/rust/metrics.yaml b/samples/rust/metrics.yaml index acb1cef412..e4d1c48269 100644 --- a/samples/rust/metrics.yaml +++ b/samples/rust/metrics.yaml @@ -73,6 +73,14 @@ test.metrics: - aLabel - 2label + sample_labeled_quantity: + <<: *defaults + type: labeled_quantity + unit: prtime + labels: + - aLabel + - 2label + sample_event_no_keys: <<: *defaults type: event