1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use inherent::inherent;
use std::{collections::HashMap, marker::PhantomData};
use glean_core::traits;
use crate::{ErrorType, RecordedEvent};
pub use glean_core::traits::NoExtraKeys;
#[derive(Clone)]
pub struct EventMetric<K> {
pub(crate) inner: glean_core::metrics::EventMetric,
extra_keys: PhantomData<K>,
}
impl<K: traits::ExtraKeys> EventMetric<K> {
pub fn new(meta: glean_core::CommonMetricData) -> Self {
let allowed_extra_keys = K::ALLOWED_KEYS.iter().map(|s| s.to_string()).collect();
let inner = glean_core::metrics::EventMetric::new(meta, allowed_extra_keys);
Self {
inner,
extra_keys: PhantomData,
}
}
pub fn record_with_time(&self, timestamp: u64, extra: HashMap<String, String>) {
self.inner.record_with_time(timestamp, extra);
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::common_test::{lock_test, new_glean};
use crate::CommonMetricData;
#[test]
fn no_extra_keys() {
let _lock = lock_test();
let _t = new_glean(None, true);
let metric: EventMetric<NoExtraKeys> = EventMetric::new(CommonMetricData {
name: "event".into(),
category: "test".into(),
send_in_pings: vec!["test1".into()],
..Default::default()
});
metric.record(None);
metric.record(None);
let data = metric.test_get_value(None).expect("no event recorded");
assert_eq!(2, data.len());
assert!(data[0].timestamp <= data[1].timestamp);
}
#[test]
fn with_extra_keys() {
let _lock = lock_test();
let _t = new_glean(None, true);
#[derive(Default, Debug, Clone, Hash, Eq, PartialEq)]
struct SomeExtra {
key1: Option<String>,
key2: Option<String>,
}
impl glean_core::traits::ExtraKeys for SomeExtra {
const ALLOWED_KEYS: &'static [&'static str] = &["key1", "key2"];
fn into_ffi_extra(self) -> HashMap<String, String> {
let mut map = HashMap::new();
self.key1.and_then(|key1| map.insert("key1".into(), key1));
self.key2.and_then(|key2| map.insert("key2".into(), key2));
map
}
}
let metric: EventMetric<SomeExtra> = EventMetric::new(CommonMetricData {
name: "event".into(),
category: "test".into(),
send_in_pings: vec!["test1".into()],
..Default::default()
});
let map1 = SomeExtra {
key1: Some("1".into()),
..Default::default()
};
metric.record(map1);
let map2 = SomeExtra {
key1: Some("1".into()),
key2: Some("2".into()),
};
metric.record(map2);
metric.record(None);
let data = metric.test_get_value(None).expect("no event recorded");
assert_eq!(3, data.len());
assert!(data[0].timestamp <= data[1].timestamp);
assert!(data[1].timestamp <= data[2].timestamp);
let mut map = HashMap::new();
map.insert("key1".into(), "1".into());
assert_eq!(Some(map), data[0].extra);
let mut map = HashMap::new();
map.insert("key1".into(), "1".into());
map.insert("key2".into(), "2".into());
assert_eq!(Some(map), data[1].extra);
assert_eq!(None, data[2].extra);
}
}
#[inherent]
impl<K: traits::ExtraKeys> traits::Event for EventMetric<K> {
type Extra = K;
pub fn record<M: Into<Option<<Self as traits::Event>::Extra>>>(&self, extra: M) {
let extra = extra
.into()
.map(|e| e.into_ffi_extra())
.unwrap_or_else(HashMap::new);
self.inner.record(extra);
}
pub fn test_get_value<'a, S: Into<Option<&'a str>>>(
&self,
ping_name: S,
) -> Option<Vec<RecordedEvent>> {
let ping_name = ping_name.into().map(|s| s.to_string());
self.inner.test_get_value(ping_name)
}
pub fn test_get_num_recorded_errors<'a, S: Into<Option<&'a str>>>(
&self,
error: ErrorType,
ping_name: S,
) -> i32 {
let ping_name = ping_name.into().map(|s| s.to_string());
self.inner.test_get_num_recorded_errors(error, ping_name)
}
}