glean_core/metrics/
boolean.rs1use 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#[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
45impl BooleanMetric {
50 pub fn new(meta: CommonMetricData) -> Self {
52 Self {
53 meta: Arc::new(meta.into()),
54 }
55 }
56
57 #[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 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 #[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 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 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}