glean_core/metrics/
boolean.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::sync::Arc;
6
7use crate::common_metric_data::{CommonMetricDataInternal, DynamicLabelType};
8use crate::error_recording::{test_get_num_recorded_errors, ErrorType};
9use crate::metrics::MetricType;
10use crate::metrics::{Metric, TestGetValue};
11use crate::storage::StorageManager;
12use crate::CommonMetricData;
13use crate::Glean;
14
15/// A boolean metric.
16///
17/// Records a simple flag.
18#[derive(Clone, Debug)]
19pub struct BooleanMetric {
20    meta: Arc<CommonMetricDataInternal>,
21}
22
23impl MetricType for BooleanMetric {
24    fn meta(&self) -> &CommonMetricDataInternal {
25        &self.meta
26    }
27
28    fn with_name(&self, name: String) -> Self {
29        let mut meta = (*self.meta).clone();
30        meta.inner.name = name;
31        Self {
32            meta: Arc::new(meta),
33        }
34    }
35
36    fn with_dynamic_label(&self, label: DynamicLabelType) -> Self {
37        let mut meta = (*self.meta).clone();
38        meta.inner.dynamic_label = Some(label);
39        Self {
40            meta: Arc::new(meta),
41        }
42    }
43}
44
45// IMPORTANT:
46//
47// When changing this implementation, make sure all the operations are
48// also declared in the related trait in `../traits/`.
49impl BooleanMetric {
50    /// Creates a new boolean metric.
51    pub fn new(meta: CommonMetricData) -> Self {
52        Self {
53            meta: Arc::new(meta.into()),
54        }
55    }
56
57    /// Sets to the specified boolean value.
58    ///
59    /// # Arguments
60    ///
61    /// * `glean` - the Glean instance this metric belongs to.
62    /// * `value` - the value to set.
63    #[doc(hidden)]
64    pub fn set_sync(&self, glean: &Glean, value: bool) {
65        if !self.should_record(glean) {
66            return;
67        }
68
69        let value = Metric::Boolean(value);
70        glean.storage().record(glean, &self.meta, &value)
71    }
72
73    /// Sets to the specified boolean value.
74    ///
75    /// # Arguments
76    ///
77    /// * `value` - the value to set.
78    pub fn set(&self, value: bool) {
79        let metric = self.clone();
80        crate::launch_with_glean(move |glean| metric.set_sync(glean, value))
81    }
82
83    /// **Test-only API (exported for FFI purposes).**
84    ///
85    /// Gets the currently stored value as a boolean.
86    ///
87    /// This doesn't clear the stored value.
88    #[doc(hidden)]
89    pub fn get_value(&self, glean: &Glean, ping_name: Option<&str>) -> Option<bool> {
90        let queried_ping_name = ping_name.unwrap_or_else(|| &self.meta().inner.send_in_pings[0]);
91
92        match StorageManager.snapshot_metric_for_test(
93            glean.storage(),
94            queried_ping_name,
95            &self.meta.identifier(glean),
96            self.meta.inner.lifetime,
97        ) {
98            Some(Metric::Boolean(b)) => Some(b),
99            _ => None,
100        }
101    }
102
103    /// **Exported for test purposes.**
104    ///
105    /// Gets the number of recorded errors for the given metric and error type.
106    ///
107    /// # Arguments
108    ///
109    /// * `error` - The type of error
110    ///
111    /// # Returns
112    ///
113    /// The number of errors reported.
114    pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 {
115        crate::block_on_dispatcher();
116
117        crate::core::with_glean(|glean| {
118            test_get_num_recorded_errors(glean, self.meta(), error).unwrap_or(0)
119        })
120    }
121}
122
123impl TestGetValue<bool> for BooleanMetric {
124    /// **Test-only API (exported for FFI purposes).**
125    ///
126    /// Gets the currently stored value as a boolean.
127    ///
128    /// This doesn't clear the stored value.
129    ///
130    /// # Arguments
131    ///
132    /// * `ping_name` - the optional name of the ping to retrieve the metric
133    ///                 for. Defaults to the first value in `send_in_pings`.
134    ///
135    /// # Returns
136    ///
137    /// The stored value or `None` if nothing stored.
138    fn test_get_value(&self, ping_name: Option<String>) -> Option<bool> {
139        crate::block_on_dispatcher();
140        crate::core::with_glean(|glean| self.get_value(glean, ping_name.as_deref()))
141    }
142}