diff options
Diffstat (limited to 'toolkit/components/glean/api/src/private/mod.rs')
-rw-r--r-- | toolkit/components/glean/api/src/private/mod.rs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/toolkit/components/glean/api/src/private/mod.rs b/toolkit/components/glean/api/src/private/mod.rs new file mode 100644 index 0000000000..1e77e4b49e --- /dev/null +++ b/toolkit/components/glean/api/src/private/mod.rs @@ -0,0 +1,90 @@ +// 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/. + +//! The different metric types supported by the Glean SDK to handle data. + +use std::convert::TryFrom; +use std::time::{SystemTime, UNIX_EPOCH}; + +use serde::{Deserialize, Serialize}; + +// Re-export of `glean_core` types we can re-use. +// That way a user only needs to depend on this crate, not on glean_core (and there can't be a +// version mismatch). +pub use glean_core::{ + metrics::DistributionData, metrics::MemoryUnit, metrics::RecordedEvent, metrics::TimeUnit, + CommonMetricData, ErrorType, Lifetime, +}; + +mod boolean; +mod counter; +mod datetime; +mod event; +mod labeled; +mod memory_distribution; +mod ping; +pub(crate) mod string; +mod string_list; +mod timespan; +mod timing_distribution; +mod uuid; + +pub use self::boolean::BooleanMetric; +pub use self::counter::CounterMetric; +pub use self::datetime::DatetimeMetric; +pub use self::event::{EventMetric, EventRecordingError, ExtraKeys, NoExtraKeys}; +pub use self::labeled::LabeledMetric; +pub use self::memory_distribution::MemoryDistributionMetric; +pub use self::ping::Ping; +pub use self::string::StringMetric; +pub use self::string_list::StringListMetric; +pub use self::timespan::TimespanMetric; +pub use self::timing_distribution::TimingDistributionMetric; +pub use self::uuid::UuidMetric; + +/// An instant in time. +/// +/// Similar to [`std::time::Instant`](https://doc.rust-lang.org/std/time/struct.Instant.html), +/// but much simpler in that we explicitly expose that it's just an integer. +/// +/// This is needed, as the current `glean-core` API expects timestamps as integers. +/// We probably should move this API into `glean-core` directly. +/// See [Bug 1619253](https://bugzilla.mozilla.org/show_bug.cgi?id=1619253). +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Instant(u64); + +impl Instant { + /// Returns an instant corresponding to "now". + fn now() -> Instant { + let now = SystemTime::now() + .duration_since(UNIX_EPOCH) + .expect("SystemTime before UNIX epoch!"); + let now = now.as_nanos(); + + match u64::try_from(now) { + Ok(now) => Instant(now), + Err(_) => { + // Greetings to 2554 from 2020! + panic!("timestamp exceeds value range") + } + } + } +} + +/// Uniquely identifies a single metric within its metric type. +#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Deserialize, Serialize)] +#[repr(transparent)] +pub struct MetricId(u32); + +impl MetricId { + pub fn new(id: u32) -> Self { + Self(id) + } +} + +impl From<u32> for MetricId { + fn from(id: u32) -> Self { + Self(id) + } +} |