Skip to content

Commit

Permalink
bug 1925346 - Implement new metric type labeled_quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
chutten committed Oct 22, 2024
1 parent 15c650d commit 97cc67a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion glean-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub use crate::error_recording::{test_get_num_recorded_errors, ErrorType};
pub use crate::histogram::HistogramType;
pub use crate::metrics::labeled::{
AllowLabeled, LabeledBoolean, LabeledCounter, LabeledCustomDistribution,
LabeledMemoryDistribution, LabeledMetric, LabeledMetricData, LabeledString,
LabeledMemoryDistribution, LabeledMetric, LabeledMetricData, LabeledQuantity, LabeledString,
LabeledTimingDistribution,
};
pub use crate::metrics::{
Expand Down
16 changes: 14 additions & 2 deletions glean-core/src/metrics/labeled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,6 +37,9 @@ pub type LabeledMemoryDistribution = LabeledMetric<MemoryDistributionMetric>;
/// A labeled timing_distribution.
pub type LabeledTimingDistribution = LabeledMetric<TimingDistributionMetric>;

/// A labeled quantity
pub type LabeledQuantity = LabeledMetric<QuantityMetric>;

/// The metric data needed to construct inner submetrics.
///
/// Different Labeled metrics require different amounts and kinds of information to
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<QuantityMetric>"),
}
}
}
}

/// Trait for metrics that can be nested inside a labeled metric.
Expand Down
2 changes: 1 addition & 1 deletion glean-core/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 22 additions & 2 deletions glean-core/src/metrics/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,13 +17,29 @@ use crate::Glean;
/// Used to store explicit non-negative integers.
#[derive(Clone, Debug)]
pub struct QuantityMetric {
meta: CommonMetricDataInternal,
meta: Arc<CommonMetricDataInternal>,
}

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:
Expand All @@ -31,7 +49,9 @@ 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.
Expand Down
34 changes: 34 additions & 0 deletions glean-core/tests/labeled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions samples/rust/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 97cc67a

Please sign in to comment.