| 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 std::sync::atomic::{AtomicU8, Ordering}; |
| 6 |
|
| 7 |
use malloc_size_of_derive::MallocSizeOf; |
| 8 |
|
| 9 |
use crate::error::{Error, ErrorKind}; |
| 10 |
use crate::metrics::labeled::validate_dynamic_label; |
| 11 |
use crate::Glean; |
| 12 |
use serde::{Deserialize, Serialize}; |
| 13 |
|
| 14 |
/// The supported metrics' lifetimes. |
| 15 |
/// |
| 16 |
/// A metric's lifetime determines when its stored data gets reset. |
| 17 |
#[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Default, MallocSizeOf)] |
| 18 |
#[repr(i32)] // Use i32 to be compatible with our JNA definition |
| 19 |
#[serde(rename_all = "lowercase")] |
| 20 |
pub enum Lifetime { |
| 21 |
/// The metric is reset with each sent ping |
| 22 |
#[default] |
| 23 |
Ping, |
| 24 |
/// The metric is reset on application restart |
| 25 |
Application, |
| 26 |
/// The metric is reset with each user profile |
| 27 |
User, |
| 28 |
} |
| 29 |
|
| 30 |
impl Lifetime { |
| 31 |
/// String representation of the lifetime. |
| 32 |
pub fn as_str(self) -> &'static str { |
| 33 |
match self { |
| 34 |
Lifetime::Ping => "ping", |
| 35 |
Lifetime::Application => "app", |
| 36 |
Lifetime::User => "user", |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl TryFrom<i32> for Lifetime { |
| 42 |
type Error = Error; |
| 43 |
|
| 44 |
fn try_from(value: i32) -> Result<Lifetime, Self::Error> { |
| 45 |
match value { |
| 46 |
0 => Ok(Lifetime::Ping), |
| 47 |
1 => Ok(Lifetime::Application), |
| 48 |
2 => Ok(Lifetime::User), |
| 49 |
e => Err(ErrorKind::Lifetime(e).into()), |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/// The common set of data shared across all different metric types. |
| 55 |
#[derive(Default, Debug, Clone, Deserialize, Serialize, MallocSizeOf)] |
| 56 |
pub struct CommonMetricData { |
| 57 |
/// The metric's name. |
| 58 |
pub name: String, |
| 59 |
/// The metric's category. |
| 60 |
pub category: String, |
| 61 |
/// List of ping names to include this metric in. |
| 62 |
pub send_in_pings: Vec<String>, |
| 63 |
/// The metric's lifetime. |
| 64 |
pub lifetime: Lifetime, |
| 65 |
/// Whether or not the metric is disabled. |
| 66 |
/// |
| 67 |
/// Disabled metrics are never recorded. |
| 68 |
pub disabled: bool, |
| 69 |
/// Dynamic label. |
| 70 |
/// |
| 71 |
/// When a [`LabeledMetric<T>`](crate::metrics::LabeledMetric) factory creates the specific |
| 72 |
/// metric to be recorded to, dynamic labels are stored in the specific |
| 73 |
/// label so that we can validate them when the Glean singleton is |
| 74 |
/// available. |
| 75 |
pub dynamic_label: Option<String>, |
| 76 |
} |
| 77 |
|
| 78 |
#[derive(Default, Debug, MallocSizeOf)] |
| 79 |
pub struct CommonMetricDataInternal { |
| 80 |
pub inner: CommonMetricData, |
| 81 |
pub disabled: AtomicU8, |
| 82 |
} |
| 83 |
|
| 84 |
impl Clone for CommonMetricDataInternal { |
| 85 |
fn clone(&self) -> Self { |
| 86 |
Self { |
| 87 |
inner: self.inner.clone(), |
| 88 |
disabled: AtomicU8::new(self.disabled.load(Ordering::Relaxed)), |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
impl From<CommonMetricData> for CommonMetricDataInternal { |
| 94 |
fn from(input_data: CommonMetricData) -> Self { |
| 95 |
let disabled = input_data.disabled; |
| 96 |
Self { |
| 97 |
inner: input_data, |
| 98 |
disabled: AtomicU8::new(u8::from(disabled)), |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
impl CommonMetricDataInternal { |
| 104 |
/// Creates a new metadata object. |
| 105 |
pub fn new<A: Into<String>, B: Into<String>, C: Into<String>>( |
| 106 |
category: A, |
| 107 |
name: B, |
| 108 |
ping_name: C, |
| 109 |
) -> CommonMetricDataInternal { |
| 110 |
CommonMetricDataInternal { |
| 111 |
inner: CommonMetricData { |
| 112 |
name: name.into(), |
| 113 |
category: category.into(), |
| 114 |
send_in_pings: vec![ping_name.into()], |
| 115 |
..Default::default() |
| 116 |
}, |
| 117 |
disabled: AtomicU8::new(0), |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/// The metric's base identifier, including the category and name, but not the label. |
| 122 |
/// |
| 123 |
/// If `category` is empty, it's ommitted. |
| 124 |
/// Otherwise, it's the combination of the metric's `category` and `name`. |
| 125 |
pub(crate) fn base_identifier(&self) -> String { |
| 126 |
if self.inner.category.is_empty() { |
| 127 |
self.inner.name.clone() |
| 128 |
} else { |
| 129 |
format!("{}.{}", self.inner.category, self.inner.name) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/// The metric's unique identifier, including the category, name and label. |
| 134 |
/// |
| 135 |
/// If `category` is empty, it's ommitted. |
| 136 |
/// Otherwise, it's the combination of the metric's `category`, `name` and `label`. |
| 137 |
pub(crate) fn identifier(&self, glean: &Glean) -> String { |
| 138 |
let base_identifier = self.base_identifier(); |
| 139 |
|
| 140 |
if let Some(label) = &self.inner.dynamic_label { |
| 141 |
validate_dynamic_label(glean, self, &base_identifier, label) |
| 142 |
} else { |
| 143 |
base_identifier |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/// The list of storages this metric should be recorded into. |
| 148 |
pub fn storage_names(&self) -> &[String] { |
| 149 |
&self.inner.send_in_pings |
| 150 |
} |
| 151 |
} |