glean_core/traits/
timespan.rs

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
5use crate::{ErrorType, TestGetValue};
6use std::time::Duration;
7
8/// A description for the [`TimespanMetric`](crate::metrics::TimespanMetric) type.
9///
10/// When changing this trait, make sure all the operations are
11/// implemented in the related type in `../metrics/`.
12pub trait Timespan: TestGetValue<u64> {
13    /// Starts tracking time for the provided metric.
14    ///
15    /// This uses an internal monotonic timer.
16    ///
17    /// This records an error if it's already tracking time (i.e.
18    /// [`start`](Timespan::start) was already called with no corresponding
19    /// [`stop`](Timespan::stop)): in that case the original start time will be
20    /// preserved.
21    fn start(&self);
22
23    /// Stops tracking time for the provided metric. Sets the metric to the elapsed time.
24    ///
25    /// This will record an error if no [`start`](Timespan::start) was called.
26    fn stop(&self);
27
28    /// Aborts a previous [`start`](Timespan::start) call. No error is recorded
29    /// if no [`start`](Timespan::start) was called.
30    fn cancel(&self);
31
32    /// Explicitly sets the timespan value.
33    ///
34    /// This API should only be used if your library or application requires recording
35    /// spans of time in a way that cannot make use of
36    /// [`start`](Timespan::start)/[`stop`](Timespan::stop)/[`cancel`](Timespan::cancel).
37    ///
38    /// # Arguments
39    ///
40    /// * `elapsed` - The elapsed time to record.
41    fn set_raw(&self, elapsed: Duration);
42
43    /// **Exported for test purposes.**
44    ///
45    /// Gets the number of recorded errors for the given metric and error type.
46    ///
47    /// # Arguments
48    ///
49    /// * `error` - The type of error
50    ///
51    /// # Returns
52    ///
53    /// The number of errors reported.
54    fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32;
55}