summaryrefslogtreecommitdiffstats
path: root/toolkit/components/glean/api/src/private/mod.rs
blob: 1e77e4b49e31036c278ba4396709312b94f73d9d (plain)
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
// 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)
    }
}