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::CommonMetricData; 10 use glean_core::ErrorType; 11 12 // We need to wrap the glean-core type: otherwise if we try to implement 13 // the trait for the metric in `glean_core::metrics` we hit error[E0117]: 14 // only traits defined in the current crate can be implemented for arbitrary 15 // types. 16 17 /// Developer-facing API for recording counter metrics that are acting as 18 /// external denominators for rate metrics. 19 /// 20 /// Instances of this class type are automatically generated by the parsers 21 /// at build time, allowing developers to record values that were previously 22 /// registered in the metrics.yaml file. 23 #[derive(Clone)] 24 pub struct DenominatorMetric(pub(crate) Arc<glean_core::metrics::DenominatorMetric>); 25 26 impl DenominatorMetric { 27 /// The public constructor used by automatically generated metrics. new(meta: CommonMetricData, numerators: Vec<CommonMetricData>) -> Self28 pub fn new(meta: CommonMetricData, numerators: Vec<CommonMetricData>) -> Self { 29 Self(Arc::new(glean_core::metrics::DenominatorMetric::new( 30 meta, numerators, 31 ))) 32 } 33 } 34 35 #[inherent(pub)] 36 impl glean_core::traits::Counter for DenominatorMetric { add(&self, amount: i32)37 fn add(&self, amount: i32) { 38 let metric = Arc::clone(&self.0); 39 crate::launch_with_glean(move |glean| metric.add(glean, amount)); 40 } 41 test_get_value<'a, S: Into<Option<&'a str>>>(&self, ping_name: S) -> Option<i32>42 fn test_get_value<'a, S: Into<Option<&'a str>>>(&self, ping_name: S) -> Option<i32> { 43 crate::block_on_dispatcher(); 44 45 let queried_ping_name = ping_name 46 .into() 47 .unwrap_or_else(|| &self.0.meta().send_in_pings[0]); 48 49 crate::with_glean(|glean| self.0.test_get_value(glean, queried_ping_name)) 50 } 51 test_get_num_recorded_errors<'a, S: Into<Option<&'a str>>>( &self, error: ErrorType, ping_name: S, ) -> i3252 fn test_get_num_recorded_errors<'a, S: Into<Option<&'a str>>>( 53 &self, 54 error: ErrorType, 55 ping_name: S, 56 ) -> i32 { 57 crate::block_on_dispatcher(); 58 59 crate::with_glean_mut(|glean| { 60 glean_core::test_get_num_recorded_errors(glean, self.0.meta(), error, ping_name.into()) 61 .unwrap_or(0) 62 }) 63 } 64 } 65