// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/.
use inherent::inherent; use std::{collections::HashMap, marker::PhantomData};
use glean_core::traits;
usecrate::{ErrorType, RecordedEvent};
// We need to wrap the glean-core type: otherwise if we try to implement // the trait for the metric in `glean_core::metrics` we hit error[E0117]: // only traits defined in the current crate can be implemented for arbitrary // types.
/// Developer-facing API for recording event metrics. /// /// Instances of this class type are automatically generated by the parsers /// at build time, allowing developers to record values that were previously /// registered in the metrics.yaml file. #[derive(Clone)] pubstruct EventMetric<K> { pub(crate) inner: glean_core::metrics::EventMetric,
extra_keys: PhantomData<K>,
}
impl<K: traits::ExtraKeys> EventMetric<K> { /// The public constructor used by automatically generated metrics. pubfn 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,
}
}
/// The public constructor used by runtime-defined metrics. pubfn with_runtime_extra_keys(
meta: glean_core::CommonMetricData,
allowed_extra_keys: Vec<String>,
) -> Self { let inner = glean_core::metrics::EventMetric::new(meta, allowed_extra_keys); Self {
inner,
extra_keys: PhantomData,
}
}
/// Record a new event with a provided timestamp. /// /// It's the caller's responsibility to ensure the timestamp comes from the same clock source. /// Use [`glean::get_timestamp_ms`](crate::get_timestamp_ms) to get a valid timestamp. pubfn record_with_time(&self, timestamp: u64, extra: HashMap<String, String>) { self.inner.record_with_time(timestamp, extra);
}
}
#[inherent] impl<K: traits::ExtraKeys> traits::Event for EventMetric<K> { type Extra = K;
pubfn record<M: Into<Option<<Selfas 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);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.