| 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::Arc; |
| 6 |
|
| 7 |
use uuid::Uuid; |
| 8 |
|
| 9 |
use crate::common_metric_data::CommonMetricDataInternal; |
| 10 |
use crate::error_recording::{record_error, test_get_num_recorded_errors, ErrorType}; |
| 11 |
use crate::metrics::Metric; |
| 12 |
use crate::metrics::MetricType; |
| 13 |
use crate::storage::StorageManager; |
| 14 |
use crate::CommonMetricData; |
| 15 |
use crate::Glean; |
| 16 |
|
| 17 |
/// An UUID metric. |
| 18 |
/// |
| 19 |
/// Stores UUID v4 (randomly generated) values. |
| 20 |
#[derive(Clone, Debug)] |
| 21 |
pub struct UuidMetric { |
| 22 |
meta: Arc<CommonMetricDataInternal>, |
| 23 |
} |
| 24 |
|
| 25 |
impl MetricType for UuidMetric { |
| 26 |
fn meta(&self) -> &CommonMetricDataInternal { |
| 27 |
&self.meta |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// IMPORTANT: |
| 32 |
// |
| 33 |
// When changing this implementation, make sure all the operations are |
| 34 |
// also declared in the related trait in `../traits/`. |
| 35 |
impl UuidMetric { |
| 36 |
/// Creates a new UUID metric |
| 37 |
pub fn new(meta: CommonMetricData) -> Self { |
| 38 |
Self { |
| 39 |
meta: Arc::new(meta.into()), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/// Sets to the specified value. |
| 44 |
/// |
| 45 |
/// # Arguments |
| 46 |
/// |
| 47 |
/// * `value` - The [`Uuid`] to set the metric to. |
| 48 |
pub fn set(&self, value: String) { |
| 49 |
let metric = self.clone(); |
| 50 |
crate::launch_with_glean(move |glean| metric.set_sync(glean, &value)) |
| 51 |
} |
| 52 |
|
| 53 |
/// Sets to the specified value synchronously. |
| 54 |
#[doc(hidden)] |
| 55 |
pub fn set_sync<S: Into<String>>(&self, glean: &Glean, value: S) { |
| 56 |
if !self.should_record(glean) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
let value = value.into(); |
| 61 |
|
| 62 |
if let Ok(uuid) = uuid::Uuid::parse_str(&value) { |
| 63 |
let value = Metric::Uuid(uuid.as_hyphenated().to_string()); |
| 64 |
glean.storage().record(glean, &self.meta, &value) |
| 65 |
} else { |
| 66 |
let msg = format!("Unexpected UUID value '{}'", value); |
| 67 |
record_error(glean, &self.meta, ErrorType::InvalidValue, msg, None); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/// Sets to the specified value, from a string. |
| 72 |
/// |
| 73 |
/// This should only be used from FFI. When calling directly from Rust, it |
| 74 |
/// is better to use [`set`](UuidMetric::set). |
| 75 |
/// |
| 76 |
/// # Arguments |
| 77 |
/// |
| 78 |
/// * `glean` - The Glean instance this metric belongs to. |
| 79 |
/// * `value` - The [`Uuid`] to set the metric to. |
| 80 |
#[doc(hidden)] |
| 81 |
pub fn set_from_uuid_sync(&self, glean: &Glean, value: Uuid) { |
| 82 |
self.set_sync(glean, value.to_string()) |
| 83 |
} |
| 84 |
|
| 85 |
/// Generates a new random [`Uuid`'] and sets the metric to it. |
| 86 |
pub fn generate_and_set(&self) -> String { |
| 87 |
let uuid = Uuid::new_v4(); |
| 88 |
|
| 89 |
let value = uuid.to_string(); |
| 90 |
let metric = self.clone(); |
| 91 |
crate::launch_with_glean(move |glean| metric.set_sync(glean, value)); |
| 92 |
|
| 93 |
uuid.to_string() |
| 94 |
} |
| 95 |
|
| 96 |
/// Generates a new random [`Uuid`'] and sets the metric to it synchronously. |
| 97 |
#[doc(hidden)] |
| 98 |
pub fn generate_and_set_sync(&self, storage: &Glean) -> Uuid { |
| 99 |
let uuid = Uuid::new_v4(); |
| 100 |
self.set_sync(storage, uuid.to_string()); |
| 101 |
uuid |
| 102 |
} |
| 103 |
|
| 104 |
/// Gets the current-stored value as a string, or None if there is no value. |
| 105 |
#[doc(hidden)] |
| 106 |
pub fn get_value<'a, S: Into<Option<&'a str>>>( |
| 107 |
&self, |
| 108 |
glean: &Glean, |
| 109 |
ping_name: S, |
| 110 |
) -> Option<Uuid> { |
| 111 |
let queried_ping_name = ping_name |
| 112 |
.into() |
| 113 |
.unwrap_or_else(|| &self.meta().inner.send_in_pings[0]); |
| 114 |
|
| 115 |
match StorageManager.snapshot_metric_for_test( |
| 116 |
glean.storage(), |
| 117 |
queried_ping_name, |
| 118 |
&self.meta.identifier(glean), |
| 119 |
self.meta.inner.lifetime, |
| 120 |
) { |
| 121 |
Some(Metric::Uuid(uuid)) => Uuid::parse_str(&uuid).ok(), |
| 122 |
_ => None, |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/// **Test-only API (exported for FFI purposes).** |
| 127 |
/// |
| 128 |
/// Gets the currently stored value as a string. |
| 129 |
/// |
| 130 |
/// This doesn't clear the stored value. |
| 131 |
/// |
| 132 |
/// # Arguments |
| 133 |
/// |
| 134 |
/// * `ping_name` - the optional name of the ping to retrieve the metric |
| 135 |
/// for. Defaults to the first value in `send_in_pings`. |
| 136 |
/// |
| 137 |
/// # Returns |
| 138 |
/// |
| 139 |
/// The stored value or `None` if nothing stored. |
| 140 |
pub fn test_get_value(&self, ping_name: Option<String>) -> Option<String> { |
| 141 |
crate::block_on_dispatcher(); |
| 142 |
crate::core::with_glean(|glean| { |
| 143 |
self.get_value(glean, ping_name.as_deref()) |
| 144 |
.map(|uuid| uuid.to_string()) |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
/// **Exported for test purposes.** |
| 149 |
/// |
| 150 |
/// Gets the number of recorded errors for the given metric and error type. |
| 151 |
/// |
| 152 |
/// # Arguments |
| 153 |
/// |
| 154 |
/// * `error` - The type of error |
| 155 |
/// |
| 156 |
/// # Returns |
| 157 |
/// |
| 158 |
/// The number of errors reported. |
| 159 |
pub fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32 { |
| 160 |
crate::block_on_dispatcher(); |
| 161 |
|
| 162 |
crate::core::with_glean(|glean| { |
| 163 |
test_get_num_recorded_errors(glean, self.meta(), error).unwrap_or(0) |
| 164 |
}) |
| 165 |
} |
| 166 |
} |