Bug 1632157 - Implement the memory distribution metric type for Project FOG. r=janerik
authorTravis Long <tlong@mozilla.com>
Wed, 27 May 2020 07:52:14 +0000
changeset 532325 97c567ece28bde503d566429d64904f30c1763a9
parent 532324 7ea25549ca316cd087b31668d1958f5640b5e6aa
child 532326 9734a05d25053a0c6510606980c1aeb3cdb3aadc
push id37454
push userccoroiu@mozilla.com
push dateWed, 27 May 2020 16:14:31 +0000
treeherdermozilla-central@a1dd9afbfdf5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjanerik
bugs1632157
milestone78.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1632157 - Implement the memory distribution metric type for Project FOG. r=janerik Differential Revision: https://phabricator.services.mozilla.com/D76368
toolkit/components/glean/api/src/metrics/memory_distribution.rs
toolkit/components/glean/api/src/metrics/mod.rs
toolkit/components/glean/api/tests/memory_distribution.rs
new file mode 100644
--- /dev/null
+++ b/toolkit/components/glean/api/src/metrics/memory_distribution.rs
@@ -0,0 +1,52 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// 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 super::{CommonMetricData, DistributionData, MemoryUnit};
+
+/// A memory distribution metric.
+///
+/// Memory distributions are used to accumulate and store memory measurements for analyzing distributions of the memory data.
+#[derive(Debug)]
+pub struct MemoryDistributionMetric {
+    inner: glean_core::metrics::MemoryDistributionMetric,
+}
+
+impl MemoryDistributionMetric {
+    /// Create a new memory distribution metric.
+    pub fn new(meta: CommonMetricData, memory_unit: MemoryUnit) -> Self {
+        let inner = glean_core::metrics::MemoryDistributionMetric::new(meta, memory_unit);
+        Self { inner }
+    }
+
+    /// Accumulates the provided sample in the metric.
+    ///
+    /// ## Arguments
+    ///
+    /// * `sample` - The sample to be recorded by the metric. The sample is assumed to be in the
+    ///   configured memory unit of the metric.
+    ///
+    /// ## Notes
+    ///
+    /// Values bigger than 1 Terabyte (2<sup>40</sup> bytes) are truncated
+    /// and an `ErrorType::InvalidValue` error is recorded.
+    pub fn accumulate(&self, sample: u64) {
+        crate::with_glean(|glean| self.inner.accumulate(glean, sample))
+    }
+
+    /// **Test-only API.**
+    ///
+    /// Get the currently-stored histogram as a DistributionData of the serialized value.
+    /// This doesn't clear the stored value.
+    ///
+    /// ## Arguments
+    ///
+    /// * `storage_name` - the storage name to look into.
+    ///
+    /// ## Return value
+    ///
+    /// Returns the stored value or `None` if nothing stored.
+    pub fn test_get_value(&self, storage_name: &str) -> Option<DistributionData> {
+        crate::with_glean(move |glean| self.inner.test_get_value(glean, storage_name))
+    }
+}
--- a/toolkit/components/glean/api/src/metrics/mod.rs
+++ b/toolkit/components/glean/api/src/metrics/mod.rs
@@ -5,35 +5,40 @@
 //! The different metric types supported by the Glean SDK to handle data.
 
 use std::convert::TryFrom;
 use std::time::{SystemTime, UNIX_EPOCH};
 
 // Re-export of `glean_core` types we can re-use.
 // That way a user only needs to depend on this crate, not on glean_core (and there can't be a
 // version mismatch).
-pub use glean_core::{metrics::TimeUnit, CommonMetricData, ErrorType, Lifetime};
+pub use glean_core::{
+    metrics::DistributionData, metrics::MemoryUnit, metrics::TimeUnit, CommonMetricData, ErrorType,
+    Lifetime,
+};
 
 mod boolean;
 mod counter;
 mod datetime;
 mod event;
 mod labeled;
+mod memory_distribution;
 mod ping;
 mod string;
 mod string_list;
 mod timespan;
 mod timing_distribution;
 mod uuid;
 
 pub use self::boolean::BooleanMetric;
 pub use self::counter::CounterMetric;
 pub use self::datetime::DatetimeMetric;
 pub use self::event::{EventMetric, ExtraKeys, NoExtraKeys};
 pub use self::labeled::LabeledMetric;
+pub use self::memory_distribution::MemoryDistributionMetric;
 pub use self::ping::Ping;
 pub use self::string::StringMetric;
 pub use self::string_list::StringListMetric;
 pub use self::timespan::TimespanMetric;
 pub use self::timing_distribution::{TimerId, TimingDistributionMetric};
 pub use self::uuid::UuidMetric;
 
 /// An instant in time.
new file mode 100644
--- /dev/null
+++ b/toolkit/components/glean/api/tests/memory_distribution.rs
@@ -0,0 +1,34 @@
+// This Source Code Form is subject to the terms of the Mozilla Public
+// 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/.
+
+mod common;
+use common::*;
+
+use glean::metrics::{CommonMetricData, Lifetime, MemoryDistributionMetric, MemoryUnit};
+
+#[test]
+fn smoke_test_memory_distribution() {
+    let _lock = lock_test();
+    let _t = setup_glean(None);
+
+    let store_names: Vec<String> = vec!["store1".into()];
+    let metric = MemoryDistributionMetric::new(
+        CommonMetricData {
+            name: "memory_distribution_metric".into(),
+            category: "telemetry".into(),
+            send_in_pings: store_names.clone(),
+            disabled: false,
+            lifetime: Lifetime::Ping,
+            ..Default::default()
+        },
+        MemoryUnit::Kilobyte,
+    );
+
+    metric.accumulate(42);
+
+    let metric_data = metric.test_get_value("store1").unwrap();
+    assert_eq!(1, metric_data.values[&42494]);
+    assert_eq!(0, metric_data.values[&44376]);
+    assert_eq!(43008, metric_data.sum);
+}