-
Notifications
You must be signed in to change notification settings - Fork 124
/
metrics_utils.rs
358 lines (325 loc) · 10.4 KB
/
metrics_utils.rs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::mem::forget;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::thread_local;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use futures::Future;
use nativelink_metric::{
group, publish, MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent,
};
pub use prometheus_client::registry::Registry;
thread_local! {
/// This is a thread local variable that will enable or disable metrics for
/// the current thread. This does not mean that metrics are "disabled"
/// everywhere. It only means that metrics gathering for this specific thread
/// will be disabled. Because tokio uses thread pools, if you change this
/// value you'll need to change it on every thread tokio is using, often using
/// the `tokio::runtime::Builder::on_thread_start` function. This field also
/// does not mean that metrics cannot be pulled from the registry. It only
/// removes the ability for metrics that are collected at runtime (hot path)
/// from being collected.
pub static METRICS_ENABLED: AtomicBool = const { AtomicBool::new(true) };
}
#[inline]
pub fn metrics_enabled() -> bool {
METRICS_ENABLED.with(
#[inline]
|v| v.load(Ordering::Acquire),
)
}
/// This function will enable or disable metrics for the current thread.
/// WARNING: This will only happen for this thread. Tokio uses thread pools
/// so you'd need to run this function on every thread in the thread pool in
/// order to enable it everywhere.
pub fn set_metrics_enabled_for_this_thread(enabled: bool) {
METRICS_ENABLED.with(|v| v.store(enabled, Ordering::Release));
}
#[derive(Default)]
pub struct FuncCounterWrapper {
pub successes: AtomicU64,
pub failures: AtomicU64,
}
impl FuncCounterWrapper {
#[inline]
pub fn wrap<T, E>(&self, func: impl FnOnce() -> Result<T, E>) -> Result<T, E> {
let result = (func)();
if result.is_ok() {
self.successes.fetch_add(1, Ordering::Acquire);
} else {
self.failures.fetch_add(1, Ordering::Acquire);
}
result
}
}
impl MetricsComponent for FuncCounterWrapper {
fn publish(
&self,
_kind: MetricKind,
field_metadata: MetricFieldData,
) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
let _enter = group!(field_metadata.name).entered();
publish!(
"successes",
&self.successes,
MetricKind::Counter,
format!(
"The number of times {} was successful.",
field_metadata.name
)
);
publish!(
"failures",
&self.failures,
MetricKind::Counter,
format!("The number of times {} failed.", field_metadata.name)
);
Ok(MetricPublishKnownKindData::Component)
}
}
/// This is a utility that will only increment the referenced counter when it is dropped.
/// This struct is zero cost and has a runtime cost only when it is dropped.
/// This struct is very useful for tracking when futures are dropped.
struct DropCounter<'a> {
counter: &'a AtomicU64,
}
impl<'a> DropCounter<'a> {
#[inline]
pub fn new(counter: &'a AtomicU64) -> Self {
Self { counter }
}
}
impl<'a> Drop for DropCounter<'a> {
#[inline]
fn drop(&mut self) {
if !metrics_enabled() {
return;
}
self.counter.fetch_add(1, Ordering::Acquire);
}
}
pub struct AsyncTimer<'a> {
start: Instant,
drop_counter: DropCounter<'a>,
counter: &'a AsyncCounterWrapper,
}
impl<'a> AsyncTimer<'a> {
#[inline]
pub fn measure(self) {
if !metrics_enabled() {
return;
}
self.counter
.sum_func_duration_ns
.fetch_add(self.start.elapsed().as_nanos() as u64, Ordering::Acquire);
self.counter.calls.fetch_add(1, Ordering::Acquire);
self.counter.successes.fetch_add(1, Ordering::Acquire);
// This causes DropCounter's drop to never be called.
forget(self.drop_counter);
}
}
/// Tracks the number of calls, successes, failures, and drops of an async function.
/// call `.wrap(future)` to wrap a future and stats about the future are automatically
/// tracked and can be published to a `CollectorState`.
#[derive(Default)]
pub struct AsyncCounterWrapper {
pub calls: AtomicU64,
pub successes: AtomicU64,
pub failures: AtomicU64,
pub drops: AtomicU64,
// Time spent in nano seconds in the future.
// 64 bit address space gives ~584 years of nanoseconds.
pub sum_func_duration_ns: AtomicU64,
}
impl MetricsComponent for AsyncCounterWrapper {
fn publish(
&self,
_kind: MetricKind,
field_metadata: MetricFieldData,
) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
let _enter = group!(field_metadata.name).entered();
publish!(
"calls",
&self.calls,
MetricKind::Counter,
format!("The number of times {} was called.", field_metadata.name)
);
publish!(
"successes",
&self.successes,
MetricKind::Counter,
format!(
"The number of times {} was successful.",
field_metadata.name
)
);
publish!(
"failures",
&self.failures,
MetricKind::Counter,
format!("The number of times {} failed.", field_metadata.name)
);
publish!(
"drops",
&self.drops,
MetricKind::Counter,
format!("The number of times {} was dropped.", field_metadata.name)
);
publish!(
"sum_func_duration_ns",
&self.sum_func_duration_ns,
MetricKind::Counter,
format!(
"The sum of the time spent in nanoseconds in {}.",
field_metadata.name
)
);
Ok(MetricPublishKnownKindData::Component)
}
}
impl AsyncCounterWrapper {
#[inline]
pub fn wrap_fn<'a, T: 'a, E>(
&'a self,
func: impl FnOnce() -> Result<T, E> + 'a,
) -> Result<T, E> {
self.calls.fetch_add(1, Ordering::Acquire);
let result = (func)();
if result.is_ok() {
self.successes.fetch_add(1, Ordering::Acquire);
} else {
self.failures.fetch_add(1, Ordering::Acquire);
}
result
}
#[inline]
pub async fn wrap<'a, T, E, F: Future<Output = Result<T, E>> + 'a>(
&'a self,
future: F,
) -> Result<T, E> {
if !metrics_enabled() {
return future.await;
}
let result = self.wrap_no_capture_result(future).await;
if result.is_ok() {
self.successes.fetch_add(1, Ordering::Acquire);
} else {
self.failures.fetch_add(1, Ordering::Acquire);
}
result
}
#[inline]
pub async fn wrap_no_capture_result<'a, T, F: Future<Output = T> + 'a>(
&'a self,
future: F,
) -> T {
if !metrics_enabled() {
return future.await;
}
self.calls.fetch_add(1, Ordering::Acquire);
let drop_counter = DropCounter::new(&self.drops);
let instant = Instant::now();
let result = future.await;
// By default `drop_counter` will increment the drop counter when it goes out of scope.
// This will ensure we don't increment the counter if we make it here with a zero cost.
forget(drop_counter);
self.sum_func_duration_ns
.fetch_add(instant.elapsed().as_nanos() as u64, Ordering::Acquire);
result
}
#[inline]
pub fn begin_timer(&self) -> AsyncTimer<'_> {
AsyncTimer {
start: Instant::now(),
drop_counter: DropCounter::new(&self.drops),
counter: self,
}
}
}
/// Tracks an number.
#[derive(Default)]
pub struct Counter(AtomicU64);
impl Counter {
#[inline]
pub fn inc(&self) {
self.add(1);
}
#[inline]
pub fn add(&self, value: u64) {
if !metrics_enabled() {
return;
}
self.0.fetch_add(value, Ordering::Acquire);
}
#[inline]
pub fn sub(&self, value: u64) {
if !metrics_enabled() {
return;
}
self.0.fetch_sub(value, Ordering::Acquire);
}
}
impl MetricsComponent for Counter {
fn publish(
&self,
kind: MetricKind,
field_metadata: MetricFieldData,
) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
self.0.publish(kind, field_metadata)
}
}
/// Tracks an counter through time and the last time the counter was changed.
#[derive(Default)]
pub struct CounterWithTime {
pub counter: AtomicU64,
pub last_time: AtomicU64,
}
impl CounterWithTime {
#[inline]
pub fn inc(&self) {
if !metrics_enabled() {
return;
}
self.counter.fetch_add(1, Ordering::Acquire);
self.last_time.store(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as u64,
Ordering::Release,
);
}
}
impl MetricsComponent for CounterWithTime {
fn publish(
&self,
_kind: MetricKind,
field_metadata: MetricFieldData,
) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
let _enter = group!(field_metadata.name).entered();
publish!(
"counter",
&self.counter,
MetricKind::Counter,
format!("Current count of {}.", field_metadata.name)
);
publish!(
"last_time",
&self.last_time,
MetricKind::Counter,
format!("Last timestamp {} was published.", field_metadata.name)
);
Ok(MetricPublishKnownKindData::Component)
}
}