glean_core/traits/timing_distribution.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::metrics::DistributionData;
6use crate::metrics::TimerId;
7use crate::{ErrorType, TestGetValue};
8
9use std::time::Duration;
10
11/// A description for the [`TimingDistributionMetric`](crate::metrics::TimingDistributionMetric) type.
12///
13/// When changing this trait, make sure all the operations are
14/// implemented in the related type in `../metrics/`.
15pub trait TimingDistribution: TestGetValue<DistributionData> {
16 /// Start tracking time for the provided metric.
17 /// Multiple timers can run simultaneously.
18 ///
19 /// # Returns
20 ///
21 /// A unique [`TimerId`] for the new timer.
22 fn start(&self) -> TimerId;
23
24 /// Stops tracking time for the provided metric and associated timer id.
25 ///
26 /// Adds a count to the corresponding bucket in the timing distribution.
27 /// This will record an error if no [`start`](TimingDistribution::start) was
28 /// called.
29 ///
30 /// # Arguments
31 ///
32 /// * `id` - The [`TimerId`] to associate with this timing. This allows
33 /// for concurrent timing of events associated with different ids to the
34 /// same timespan metric.
35 fn stop_and_accumulate(&self, id: TimerId);
36
37 /// Aborts a previous [`start`](TimingDistribution::start) call. No
38 /// error is recorded if no [`start`](TimingDistribution::start) was
39 /// called.
40 ///
41 /// # Arguments
42 ///
43 /// * `id` - The [`TimerId`] to associate with this timing. This allows
44 /// for concurrent timing of events associated with different ids to the
45 /// same timing distribution metric.
46 fn cancel(&self, id: TimerId);
47
48 /// Accumulates the provided signed samples in the metric.
49 ///
50 /// This is required so that the platform-specific code can provide us with
51 /// 64 bit signed integers if no `u64` comparable type is available. This
52 /// will take care of filtering and reporting errors for any provided negative
53 /// sample.
54 ///
55 /// Please note that this assumes that the provided samples are already in
56 /// the "unit" declared by the instance of the metric type (e.g. if the
57 /// instance this method was called on is using [`crate::TimeUnit::Second`], then
58 /// `samples` are assumed to be in that unit).
59 ///
60 /// # Arguments
61 ///
62 /// * `samples` - The vector holding the samples to be recorded by the metric.
63 ///
64 /// ## Notes
65 ///
66 /// Discards any negative value in `samples` and report an [`ErrorType::InvalidValue`]
67 /// for each of them. Reports an [`ErrorType::InvalidOverflow`] error for samples that
68 /// are longer than `MAX_SAMPLE_TIME`.
69 fn accumulate_samples(&self, samples: Vec<i64>);
70
71 /// Accumulates precisely one signed sample in the metric.
72 ///
73 /// Precludes the need for a collection in the most common use case.
74 ///
75 /// Sign is required so that the platform-specific code can provide us with
76 /// a 64 bit signed integer if no `u64` comparable type is available. This
77 /// will take care of filtering and reporting errors for any provided negative
78 /// sample.
79 ///
80 /// Please note that this assumes that the provided sample is already in
81 /// the "unit" declared by the instance of the metric type (e.g. if the
82 /// instance this method was called on is using [`crate::TimeUnit::Second`], then
83 /// `sample` is assumed to be in that unit).
84 ///
85 /// # Arguments
86 ///
87 /// * `sample` - The singular sample to be recorded by the metric.
88 ///
89 /// ## Notes
90 ///
91 /// Discards any negative value and reports an [`ErrorType::InvalidValue`].
92 /// Reports an [`ErrorType::InvalidOverflow`] error if the sample is longer than
93 /// `MAX_SAMPLE_TIME`.
94 fn accumulate_single_sample(&self, sample: i64);
95
96 /// Accumulates the provided samples in the metric.
97 ///
98 /// # Arguments
99 ///
100 /// * `samples` - A list of samples recorded by the metric.
101 /// Samples must be in nanoseconds.
102 /// ## Notes
103 ///
104 /// Reports an [`ErrorType::InvalidOverflow`] error for samples that
105 /// are longer than `MAX_SAMPLE_TIME`.
106 fn accumulate_raw_samples_nanos(&self, samples: Vec<u64>);
107
108 /// Accumulates precisely one duration to the metric.
109 ///
110 /// Like `TimingDistribution::accumulate_single_sample`, but for use when the
111 /// duration is:
112 ///
113 /// * measured externally, or
114 /// * is in a unit different from the timing_distribution's internal TimeUnit.
115 ///
116 /// # Arguments
117 ///
118 /// * `duration` - The single duration to be recorded in the metric.
119 ///
120 /// ## Notes
121 ///
122 /// Reports an [`ErrorType::InvalidOverflow`] error if `duration` is longer than
123 /// `MAX_SAMPLE_TIME`.
124 ///
125 /// The API client is responsible for ensuring that `duration` is derived from a
126 /// monotonic clock source that behaves consistently over computer sleep across
127 /// the application's platforms. Otherwise the resulting data may not share the same
128 /// guarantees that other `timing_distribution` metrics' data do.
129 fn accumulate_raw_duration(&self, duration: Duration);
130
131 /// **Exported for test purposes.**
132 ///
133 /// Gets the number of recorded errors for the given error type.
134 ///
135 /// # Arguments
136 ///
137 /// * `error` - The type of error
138 ///
139 /// # Returns
140 ///
141 /// The number of errors recorded.
142 fn test_get_num_recorded_errors(&self, error: ErrorType) -> i32;
143}