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
use std::sync::atomic::{AtomicU8, Ordering};
use crate::error::{Error, ErrorKind};
use crate::metrics::labeled::validate_dynamic_label;
use crate::Glean;
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default)]
#[repr(i32)] #[serde(rename_all = "lowercase")]
pub enum Lifetime {
#[default]
Ping,
Application,
User,
}
impl Lifetime {
pub fn as_str(self) -> &'static str {
match self {
Lifetime::Ping => "ping",
Lifetime::Application => "app",
Lifetime::User => "user",
}
}
}
impl TryFrom<i32> for Lifetime {
type Error = Error;
fn try_from(value: i32) -> Result<Lifetime, Self::Error> {
match value {
0 => Ok(Lifetime::Ping),
1 => Ok(Lifetime::Application),
2 => Ok(Lifetime::User),
e => Err(ErrorKind::Lifetime(e).into()),
}
}
}
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct CommonMetricData {
pub name: String,
pub category: String,
pub send_in_pings: Vec<String>,
pub lifetime: Lifetime,
pub disabled: bool,
pub dynamic_label: Option<String>,
}
#[derive(Default, Debug)]
pub struct CommonMetricDataInternal {
pub inner: CommonMetricData,
pub disabled: AtomicU8,
}
impl Clone for CommonMetricDataInternal {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
disabled: AtomicU8::new(self.disabled.load(Ordering::Relaxed)),
}
}
}
impl From<CommonMetricData> for CommonMetricDataInternal {
fn from(input_data: CommonMetricData) -> Self {
let disabled = input_data.disabled;
Self {
inner: input_data,
disabled: AtomicU8::new(u8::from(disabled)),
}
}
}
impl CommonMetricDataInternal {
pub fn new<A: Into<String>, B: Into<String>, C: Into<String>>(
category: A,
name: B,
ping_name: C,
) -> CommonMetricDataInternal {
CommonMetricDataInternal {
inner: CommonMetricData {
name: name.into(),
category: category.into(),
send_in_pings: vec![ping_name.into()],
..Default::default()
},
disabled: AtomicU8::new(0),
}
}
pub(crate) fn base_identifier(&self) -> String {
if self.inner.category.is_empty() {
self.inner.name.clone()
} else {
format!("{}.{}", self.inner.category, self.inner.name)
}
}
pub(crate) fn identifier(&self, glean: &Glean) -> String {
let base_identifier = self.base_identifier();
if let Some(label) = &self.inner.dynamic_label {
validate_dynamic_label(glean, self, &base_identifier, label)
} else {
base_identifier
}
}
pub fn storage_names(&self) -> &[String] {
&self.inner.send_in_pings
}
}