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 5 use inherent::inherent; 6 use std::sync::Arc; 7 8 use glean_core::metrics::MetricType; 9 use glean_core::ErrorType; 10 11 // We need to wrap the glean-core type, otherwise if we try to implement 12 // the trait for the metric in `glean_core::metrics` we hit error[E0117]: 13 // only traits defined in the current crate can be implemented for arbitrary 14 // types. 15 16 /// Developer-facing API for recording UUID metrics. 17 /// 18 /// Instances of this class type are automatically generated by the parsers 19 /// at build time, allowing developers to record values that were previously 20 /// registered in the metrics.yaml file. 21 #[derive(Clone)] 22 pub struct UuidMetric(pub(crate) Arc<glean_core::metrics::UuidMetric>); 23 24 impl UuidMetric { 25 /// The public constructor used by automatically generated metrics. new(meta: glean_core::CommonMetricData) -> Self26 pub fn new(meta: glean_core::CommonMetricData) -> Self { 27 Self(Arc::new(glean_core::metrics::UuidMetric::new(meta))) 28 } 29 } 30 31 #[inherent(pub)] 32 impl glean_core::traits::Uuid for UuidMetric { set(&self, value: uuid::Uuid)33 fn set(&self, value: uuid::Uuid) { 34 let metric = Arc::clone(&self.0); 35 crate::launch_with_glean(move |glean| metric.set(glean, value)); 36 } 37 generate_and_set(&self) -> uuid::Uuid38 fn generate_and_set(&self) -> uuid::Uuid { 39 // TODO: We can use glean-core's generate_and_set after bug 1673017. 40 let uuid = uuid::Uuid::new_v4(); 41 self.set(uuid); 42 uuid 43 } 44 test_get_value<'a, S: Into<Option<&'a str>>>(&self, ping_name: S) -> Option<uuid::Uuid>45 fn test_get_value<'a, S: Into<Option<&'a str>>>(&self, ping_name: S) -> Option<uuid::Uuid> { 46 crate::block_on_dispatcher(); 47 48 let queried_ping_name = ping_name 49 .into() 50 .unwrap_or_else(|| &self.0.meta().send_in_pings[0]); 51 52 crate::with_glean(|glean| self.0.test_get_value(glean, queried_ping_name)) 53 } 54 test_get_num_recorded_errors<'a, S: Into<Option<&'a str>>>( &self, error: ErrorType, ping_name: S, ) -> i3255 fn test_get_num_recorded_errors<'a, S: Into<Option<&'a str>>>( 56 &self, 57 error: ErrorType, 58 ping_name: S, 59 ) -> i32 { 60 crate::block_on_dispatcher(); 61 62 crate::with_glean_mut(|glean| { 63 glean_core::test_get_num_recorded_errors(&glean, self.0.meta(), error, ping_name.into()) 64 .unwrap_or(0) 65 }) 66 } 67 } 68