glean_core/metrics/
boolean.rs1use std::sync::Arc;
6
7use crate::common_metric_data::{CommonMetricDataInternal, MetricLabel};
8use crate::error_recording::{test_get_num_recorded_errors, ErrorType};
9use crate::metrics::MetricType;
10use crate::metrics::{Metric, TestGetValue};
11use crate::CommonMetricData;
12use crate::Glean;
13
14#[derive(Clone, Debug)]
18pub struct BooleanMetric {
19 meta: Arc<CommonMetricDataInternal>,
20}
21
22impl MetricType for BooleanMetric {
23 fn meta(&self) -> &CommonMetricDataInternal {
24 &self.meta
25 }
26
27 fn with_name(&self, name: String) -> Self {
28 let mut meta = (*self.meta).clone();
29 meta.inner.name = name;
30 Self {
31 meta: Arc::new(meta),
32 }
33 }
34
35 fn with_label(&self, label: MetricLabel) -> Self {
36 let mut meta = (*self.meta).clone();
37 meta.inner.label = Some(label);
38 Self {
39 meta: Arc::new(meta),
40 }
41 }
42}
43
44impl BooleanMetric {
49 pub fn new(meta: CommonMetricData) -> Self {
51 Self {
52 meta: Arc::new(meta.into()),
53 }
54 }
55
56 #[doc(hidden)]
63 pub fn set_sync(&self, glean: &Glean, value: bool) {
64 if !self.should_record(glean) {
65 return;
66 }
67
68 let value = Metric::Boolean(value);
69 glean.storage().record(glean, &self.meta, &value)
70 }
71
72 pub fn set(&self, value: bool) {
78 let metric = self.clone();
79 crate::launch_with_glean(move |glean| metric.set_sync(glean, value))
80 }
81
82 #[doc(hidden)]
88 pub fn get_value(&self, glean: &Glean, ping_name: Option<&str>) -> Option<bool> {
89 let queried_ping_name = ping_name.unwrap_or_else(|| &self.meta().inner.send_in_pings[0]);
90
91 match glean.storage().get_metric(self.meta(), queried_ping_name) {
92 Some(Metric::Boolean(b)) => Some(b),
93 _ => None,
94 }
95 }
96
97 pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 {
109 crate::block_on_dispatcher();
110
111 crate::core::with_glean(|glean| {
112 test_get_num_recorded_errors(glean, self.meta(), error).unwrap_or(0)
113 })
114 }
115}
116
117impl TestGetValue for BooleanMetric {
118 type Output = bool;
119 fn test_get_value(&self, ping_name: Option<String>) -> Option<bool> {
134 crate::block_on_dispatcher();
135 crate::core::with_glean(|glean| self.get_value(glean, ping_name.as_deref()))
136 }
137}