| 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 crate::ErrorType; |
| 6 |
use 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/`. |
| 12 |
pub trait Timespan { |
| 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 currently stored value as an integer. |
| 46 |
/// |
| 47 |
/// This doesn't clear the stored value. |
| 48 |
/// |
| 49 |
/// # Arguments |
| 50 |
/// |
| 51 |
/// * `ping_name` - represents the optional name of the ping to retrieve the |
| 52 |
/// metric for. Defaults to the first value in `send_in_pings`. |
| 53 |
fn test_get_value<'a, S: Into<Option<&'a str>>>(&self, ping_name: S) -> Option<u64>; |
| 54 |
|
| 55 |
/// **Exported for test purposes.** |
| 56 |
/// |
| 57 |
/// Gets the number of recorded errors for the given metric and error type. |
| 58 |
/// |
| 59 |
/// # Arguments |
| 60 |
/// |
| 61 |
/// * `error` - The type of error |
| 62 |
/// |
| 63 |
/// # Returns |
| 64 |
/// |
| 65 |
/// The number of errors reported. |
| 66 |
fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32; |
| 67 |
} |