| 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::{collections::HashMap, marker::PhantomData}; |
| 7 |
|
| 8 |
use malloc_size_of::MallocSizeOf; |
| 9 |
|
| 10 |
use glean_core::{metrics::MetricIdentifier, traits}; |
| 11 |
|
| 12 |
use crate::{ErrorType, RecordedEvent}; |
| 13 |
|
| 14 |
// We need to wrap the glean-core type: otherwise if we try to implement |
| 15 |
// the trait for the metric in `glean_core::metrics` we hit error[E0117]: |
| 16 |
// only traits defined in the current crate can be implemented for arbitrary |
| 17 |
// types. |
| 18 |
|
| 19 |
/// Developer-facing API for recording event metrics. |
| 20 |
/// |
| 21 |
/// Instances of this class type are automatically generated by the parsers |
| 22 |
/// at build time, allowing developers to record values that were previously |
| 23 |
/// registered in the metrics.yaml file. |
| 24 |
#[derive(Clone)] |
| 25 |
pub struct EventMetric<K> { |
| 26 |
pub(crate) inner: glean_core::metrics::EventMetric, |
| 27 |
extra_keys: PhantomData<K>, |
| 28 |
} |
| 29 |
|
| 30 |
impl<K> MallocSizeOf for EventMetric<K> { |
| 31 |
fn size_of(&self, ops: &mut malloc_size_of::MallocSizeOfOps) -> usize { |
| 32 |
self.inner.size_of(ops) |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl<'a, K> MetricIdentifier<'a> for EventMetric<K> { |
| 37 |
fn get_identifiers(&'a self) -> (&'a str, &'a str, Option<&'a str>) { |
| 38 |
self.inner.get_identifiers() |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl<K: traits::ExtraKeys> EventMetric<K> { |
| 43 |
/// The public constructor used by automatically generated metrics. |
| 44 |
pub fn new(meta: glean_core::CommonMetricData) -> Self { |
| 45 |
let allowed_extra_keys = K::ALLOWED_KEYS.iter().map(|s| s.to_string()).collect(); |
| 46 |
let inner = glean_core::metrics::EventMetric::new(meta, allowed_extra_keys); |
| 47 |
Self { |
| 48 |
inner, |
| 49 |
extra_keys: PhantomData, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/// The public constructor used by runtime-defined metrics. |
| 54 |
pub fn with_runtime_extra_keys( |
| 55 |
meta: glean_core::CommonMetricData, |
| 56 |
allowed_extra_keys: Vec<String>, |
| 57 |
) -> Self { |
| 58 |
let inner = glean_core::metrics::EventMetric::new(meta, allowed_extra_keys); |
| 59 |
Self { |
| 60 |
inner, |
| 61 |
extra_keys: PhantomData, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/// Record a new event with a provided timestamp. |
| 66 |
/// |
| 67 |
/// It's the caller's responsibility to ensure the timestamp comes from the same clock source. |
| 68 |
/// Use [`glean::get_timestamp_ms`](crate::get_timestamp_ms) to get a valid timestamp. |
| 69 |
pub fn record_with_time(&self, timestamp: u64, extra: HashMap<String, String>) { |
| 70 |
self.inner.record_with_time(timestamp, extra); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#[inherent] |
| 75 |
impl<K: traits::ExtraKeys> traits::Event for EventMetric<K> { |
| 76 |
type Extra = K; |
| 77 |
|
| 78 |
pub fn record<M: Into<Option<<Self as traits::Event>::Extra>>>(&self, extra: M) { |
| 79 |
let extra = extra |
| 80 |
.into() |
| 81 |
.map(|e| e.into_ffi_extra()) |
| 82 |
.unwrap_or_else(HashMap::new); |
| 83 |
self.inner.record(extra); |
| 84 |
} |
| 85 |
|
| 86 |
pub fn test_get_value<'a, S: Into<Option<&'a str>>>( |
| 87 |
&self, |
| 88 |
ping_name: S, |
| 89 |
) -> Option<Vec<RecordedEvent>> { |
| 90 |
let ping_name = ping_name.into().map(|s| s.to_string()); |
| 91 |
self.inner.test_get_value(ping_name) |
| 92 |
} |
| 93 |
|
| 94 |
pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 { |
| 95 |
self.inner.test_get_num_recorded_errors(error) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[cfg(test)] |
| 100 |
mod test { |
| 101 |
use super::*; |
| 102 |
use crate::common_test::{lock_test, new_glean}; |
| 103 |
use crate::CommonMetricData; |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn no_extra_keys() { |
| 107 |
let _lock = lock_test(); |
| 108 |
let _t = new_glean(None, true); |
| 109 |
|
| 110 |
let metric: EventMetric<traits::NoExtraKeys> = EventMetric::new(CommonMetricData { |
| 111 |
name: "event".into(), |
| 112 |
category: "test".into(), |
| 113 |
send_in_pings: vec!["store1".into()], |
| 114 |
..Default::default() |
| 115 |
}); |
| 116 |
|
| 117 |
metric.record(None); |
| 118 |
metric.record(None); |
| 119 |
|
| 120 |
let data = metric.test_get_value(None).expect("no event recorded"); |
| 121 |
assert_eq!(2, data.len()); |
| 122 |
assert!(data[0].timestamp <= data[1].timestamp); |
| 123 |
} |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn with_extra_keys() { |
| 127 |
let _lock = lock_test(); |
| 128 |
let _t = new_glean(None, true); |
| 129 |
|
| 130 |
#[derive(Default, Debug, Clone, Hash, Eq, PartialEq)] |
| 131 |
struct SomeExtra { |
| 132 |
key1: Option<String>, |
| 133 |
key2: Option<String>, |
| 134 |
} |
| 135 |
|
| 136 |
impl glean_core::traits::ExtraKeys for SomeExtra { |
| 137 |
const ALLOWED_KEYS: &'static [&'static str] = &["key1", "key2"]; |
| 138 |
|
| 139 |
fn into_ffi_extra(self) -> HashMap<String, String> { |
| 140 |
let mut map = HashMap::new(); |
| 141 |
self.key1.and_then(|key1| map.insert("key1".into(), key1)); |
| 142 |
self.key2.and_then(|key2| map.insert("key2".into(), key2)); |
| 143 |
map |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
let metric: EventMetric<SomeExtra> = EventMetric::new(CommonMetricData { |
| 148 |
name: "event".into(), |
| 149 |
category: "test".into(), |
| 150 |
send_in_pings: vec!["store1".into()], |
| 151 |
..Default::default() |
| 152 |
}); |
| 153 |
|
| 154 |
let map1 = SomeExtra { |
| 155 |
key1: Some("1".into()), |
| 156 |
..Default::default() |
| 157 |
}; |
| 158 |
metric.record(map1); |
| 159 |
|
| 160 |
let map2 = SomeExtra { |
| 161 |
key1: Some("1".into()), |
| 162 |
key2: Some("2".into()), |
| 163 |
}; |
| 164 |
metric.record(map2); |
| 165 |
|
| 166 |
metric.record(None); |
| 167 |
|
| 168 |
let data = metric.test_get_value(None).expect("no event recorded"); |
| 169 |
assert_eq!(3, data.len()); |
| 170 |
assert!(data[0].timestamp <= data[1].timestamp); |
| 171 |
assert!(data[1].timestamp <= data[2].timestamp); |
| 172 |
|
| 173 |
let mut map = HashMap::new(); |
| 174 |
map.insert("key1".into(), "1".into()); |
| 175 |
assert_eq!(Some(map), data[0].extra); |
| 176 |
|
| 177 |
let mut map = HashMap::new(); |
| 178 |
map.insert("key1".into(), "1".into()); |
| 179 |
map.insert("key2".into(), "2".into()); |
| 180 |
assert_eq!(Some(map), data[1].extra); |
| 181 |
|
| 182 |
assert_eq!(None, data[2].extra); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn with_runtime_extra_keys() { |
| 187 |
let _lock = lock_test(); |
| 188 |
let _t = new_glean(None, true); |
| 189 |
|
| 190 |
#[derive(Default, Debug, Clone, Hash, Eq, PartialEq)] |
| 191 |
struct RuntimeExtra {} |
| 192 |
|
| 193 |
impl glean_core::traits::ExtraKeys for RuntimeExtra { |
| 194 |
const ALLOWED_KEYS: &'static [&'static str] = &[]; |
| 195 |
|
| 196 |
fn into_ffi_extra(self) -> HashMap<String, String> { |
| 197 |
HashMap::new() |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
let metric: EventMetric<RuntimeExtra> = EventMetric::with_runtime_extra_keys( |
| 202 |
CommonMetricData { |
| 203 |
name: "event".into(), |
| 204 |
category: "test".into(), |
| 205 |
send_in_pings: vec!["store1".into()], |
| 206 |
..Default::default() |
| 207 |
}, |
| 208 |
vec!["key1".into(), "key2".into()], |
| 209 |
); |
| 210 |
|
| 211 |
let map1 = HashMap::from([("key1".into(), "1".into())]); |
| 212 |
metric.record_with_time(0, map1); |
| 213 |
|
| 214 |
let map2 = HashMap::from([("key1".into(), "1".into()), ("key2".into(), "2".into())]); |
| 215 |
metric.record_with_time(1, map2); |
| 216 |
|
| 217 |
metric.record_with_time(2, HashMap::new()); |
| 218 |
|
| 219 |
let data = metric.test_get_value(None).expect("no event recorded"); |
| 220 |
assert_eq!(3, data.len()); |
| 221 |
assert!(data[0].timestamp <= data[1].timestamp); |
| 222 |
assert!(data[1].timestamp <= data[2].timestamp); |
| 223 |
|
| 224 |
let mut map = HashMap::new(); |
| 225 |
map.insert("key1".into(), "1".into()); |
| 226 |
assert_eq!(Some(map), data[0].extra); |
| 227 |
|
| 228 |
let mut map = HashMap::new(); |
| 229 |
map.insert("key1".into(), "1".into()); |
| 230 |
map.insert("key2".into(), "2".into()); |
| 231 |
assert_eq!(Some(map), data[1].extra); |
| 232 |
|
| 233 |
assert_eq!(None, data[2].extra); |
| 234 |
} |
| 235 |
} |