|
| 1 | +// Copyright © Aptos Foundation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +pub mod __private { |
| 5 | + pub use once_cell::sync::Lazy; |
| 6 | + pub use paste::paste; |
| 7 | +} |
| 8 | + |
| 9 | +use crate::{IntCounterHelper, IntCounterVecHelper, TimerHelper}; |
| 10 | +use std::{ |
| 11 | + cell::RefCell, |
| 12 | + thread::LocalKey, |
| 13 | + time::{Duration, Instant}, |
| 14 | +}; |
| 15 | + |
| 16 | +const FLUSH_INTERVAL: Duration = Duration::from_secs(1); |
| 17 | + |
| 18 | +pub struct ThreadLocalIntCounter { |
| 19 | + inner: prometheus::local::LocalIntCounter, |
| 20 | + last_flush: Instant, |
| 21 | +} |
| 22 | + |
| 23 | +impl ThreadLocalIntCounter { |
| 24 | + pub fn new(shared: &prometheus::IntCounter) -> Self { |
| 25 | + Self { |
| 26 | + inner: shared.local(), |
| 27 | + last_flush: Instant::now(), |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + fn maybe_flush(&mut self) { |
| 32 | + let now = Instant::now(); |
| 33 | + if now.duration_since(self.last_flush) > FLUSH_INTERVAL { |
| 34 | + self.inner.flush(); |
| 35 | + } |
| 36 | + self.last_flush = now; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl IntCounterHelper for LocalKey<RefCell<ThreadLocalIntCounter>> { |
| 41 | + type IntType = u64; |
| 42 | + |
| 43 | + fn inc(&'static self) { |
| 44 | + self.inc_by(1); |
| 45 | + } |
| 46 | + |
| 47 | + fn inc_by(&'static self, v: Self::IntType) { |
| 48 | + self.with_borrow_mut(|x| { |
| 49 | + x.inner.inc_by(v); |
| 50 | + x.maybe_flush(); |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +pub struct ThreadLocalIntCounterVec { |
| 56 | + inner: prometheus::local::LocalIntCounterVec, |
| 57 | + last_flush: Instant, |
| 58 | +} |
| 59 | + |
| 60 | +impl ThreadLocalIntCounterVec { |
| 61 | + pub fn new(shared: &prometheus::IntCounterVec) -> Self { |
| 62 | + Self { |
| 63 | + inner: shared.local(), |
| 64 | + last_flush: Instant::now(), |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fn maybe_flush(&mut self) { |
| 69 | + let now = Instant::now(); |
| 70 | + if now.duration_since(self.last_flush) > FLUSH_INTERVAL { |
| 71 | + self.inner.flush(); |
| 72 | + } |
| 73 | + self.last_flush = now; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl IntCounterVecHelper for LocalKey<RefCell<ThreadLocalIntCounterVec>> { |
| 78 | + type IntType = u64; |
| 79 | + |
| 80 | + fn inc_with(&'static self, labels: &[&str]) { |
| 81 | + self.inc_with_by(labels, 1); |
| 82 | + } |
| 83 | + |
| 84 | + fn inc_with_by(&'static self, labels: &[&str], v: Self::IntType) { |
| 85 | + self.with_borrow_mut(|x| { |
| 86 | + x.inner.with_label_values(labels).inc_by(v); |
| 87 | + x.maybe_flush(); |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +pub struct ThreadLocalHistogramTimer<'a> { |
| 93 | + start: Instant, |
| 94 | + labels: &'a [&'a str], |
| 95 | + parent: &'static LocalKey<RefCell<ThreadLocalHistogramVec>>, |
| 96 | +} |
| 97 | + |
| 98 | +impl<'a> ThreadLocalHistogramTimer<'a> { |
| 99 | + fn new( |
| 100 | + labels: &'a [&'a str], |
| 101 | + parent: &'static LocalKey<RefCell<ThreadLocalHistogramVec>>, |
| 102 | + ) -> Self { |
| 103 | + Self { |
| 104 | + start: Instant::now(), |
| 105 | + labels, |
| 106 | + parent, |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl<'a> Drop for ThreadLocalHistogramTimer<'a> { |
| 112 | + fn drop(&mut self) { |
| 113 | + self.parent |
| 114 | + .observe_with(self.labels, self.start.elapsed().as_secs_f64()); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +pub struct ThreadLocalHistogramVec { |
| 119 | + inner: prometheus::local::LocalHistogramVec, |
| 120 | + last_flush: Instant, |
| 121 | +} |
| 122 | + |
| 123 | +impl ThreadLocalHistogramVec { |
| 124 | + pub fn new(shared: &prometheus::HistogramVec) -> Self { |
| 125 | + Self { |
| 126 | + inner: shared.local(), |
| 127 | + last_flush: Instant::now(), |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fn maybe_flush(&mut self) { |
| 132 | + let now = Instant::now(); |
| 133 | + if now.duration_since(self.last_flush) > FLUSH_INTERVAL { |
| 134 | + self.inner.flush(); |
| 135 | + } |
| 136 | + self.last_flush = now; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl TimerHelper for LocalKey<RefCell<ThreadLocalHistogramVec>> { |
| 141 | + type TimerType<'a> = ThreadLocalHistogramTimer<'a>; |
| 142 | + |
| 143 | + fn timer_with<'a>(&'static self, labels: &'a [&str]) -> Self::TimerType<'a> { |
| 144 | + // We could use `self.with_borrow_mut(|x| x.inner.with_label_values(labels).start_timer())`. |
| 145 | + // However, this creates a `LocalHistogramTimer`, which internally stores a copy of |
| 146 | + // `LocalHistogram`: |
| 147 | + // https://github.com/tikv/rust-prometheus/blob/1d3174bf5ddf056dcb0fe59e06cad4ef42ebec68/src/histogram.rs#L1077-L1080. |
| 148 | + // When the timer is dropped, the copied `LocalHistogram` is also dropped, and this always |
| 149 | + // causes a flush: |
| 150 | + // https://github.com/tikv/rust-prometheus/blob/1d3174bf5ddf056dcb0fe59e06cad4ef42ebec68/src/histogram.rs#L1142-L1146 |
| 151 | + ThreadLocalHistogramTimer::new(labels, self) |
| 152 | + } |
| 153 | + |
| 154 | + fn observe_with(&'static self, labels: &[&str], val: f64) { |
| 155 | + self.with_borrow_mut(|x| { |
| 156 | + x.inner.with_label_values(labels).observe(val); |
| 157 | + x.maybe_flush(); |
| 158 | + }); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +#[macro_export] |
| 163 | +macro_rules! make_thread_local_int_counter { |
| 164 | + ( |
| 165 | + $(#[$attr:meta])* |
| 166 | + $vis:vis, |
| 167 | + $var_name:ident, |
| 168 | + $name:expr, |
| 169 | + $help:expr $(,)? |
| 170 | + ) => { |
| 171 | + $crate::thread_local::__private::paste! { |
| 172 | + static [<__ $var_name>]: $crate::thread_local::__private::Lazy<$crate::IntCounter> = |
| 173 | + $crate::thread_local::__private::Lazy::new(|| { |
| 174 | + $crate::register_int_counter!($name, $help) |
| 175 | + .expect("register_int_counter should succeed") |
| 176 | + }); |
| 177 | + ::std::thread_local! { |
| 178 | + $(#[$attr])* |
| 179 | + $vis static $var_name: ::std::cell::RefCell<$crate::thread_local::ThreadLocalIntCounter> = |
| 180 | + ::std::cell::RefCell::new( |
| 181 | + $crate::thread_local::ThreadLocalIntCounter::new(&[<__ $var_name>]), |
| 182 | + ); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +#[macro_export] |
| 189 | +macro_rules! make_thread_local_int_counter_vec { |
| 190 | + ( |
| 191 | + $(#[$attr:meta])* |
| 192 | + $vis:vis, |
| 193 | + $var_name:ident, |
| 194 | + $name:expr, |
| 195 | + $help:expr, |
| 196 | + $labels_names:expr $(,)? |
| 197 | + ) => { |
| 198 | + $crate::thread_local::__private::paste! { |
| 199 | + static [<__ $var_name>]: $crate::thread_local::__private::Lazy<$crate::IntCounterVec> = |
| 200 | + $crate::thread_local::__private::Lazy::new(|| { |
| 201 | + $crate::register_int_counter_vec!($name, $help, $labels_names) |
| 202 | + .expect("register_int_counter_vec should succeed") |
| 203 | + }); |
| 204 | + ::std::thread_local! { |
| 205 | + $(#[$attr])* |
| 206 | + $vis static $var_name: ::std::cell::RefCell<$crate::thread_local::ThreadLocalIntCounterVec> = |
| 207 | + ::std::cell::RefCell::new( |
| 208 | + $crate::thread_local::ThreadLocalIntCounterVec::new(&[<__ $var_name>]), |
| 209 | + ); |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +#[macro_export] |
| 216 | +macro_rules! make_thread_local_histogram_vec { |
| 217 | + ( |
| 218 | + $(#[$attr:meta])* |
| 219 | + $vis:vis, |
| 220 | + $var_name:ident, |
| 221 | + $name:expr, |
| 222 | + $help:expr, |
| 223 | + $labels_names:expr |
| 224 | + $(, $buckets:expr)? $(,)? |
| 225 | + ) => { |
| 226 | + $crate::thread_local::__private::paste! { |
| 227 | + static [<__ $var_name>]: $crate::thread_local::__private::Lazy<$crate::HistogramVec> = |
| 228 | + $crate::thread_local::__private::Lazy::new(|| { |
| 229 | + $crate::register_histogram_vec!($name, $help, $labels_names $(, $buckets)?) |
| 230 | + .expect("register_histogram_vec should succeed") |
| 231 | + }); |
| 232 | + ::std::thread_local! { |
| 233 | + $(#[$attr])* |
| 234 | + $vis static $var_name: ::std::cell::RefCell<$crate::thread_local::ThreadLocalHistogramVec> = |
| 235 | + ::std::cell::RefCell::new( |
| 236 | + $crate::thread_local::ThreadLocalHistogramVec::new(&[<__ $var_name>]), |
| 237 | + ); |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | +} |
| 242 | + |
| 243 | +#[cfg(test)] |
| 244 | +mod tests { |
| 245 | + use crate::{IntCounterHelper, IntCounterVecHelper, TimerHelper}; |
| 246 | + |
| 247 | + make_thread_local_int_counter!( |
| 248 | + pub(self), |
| 249 | + TEST_INT_COUNTER, |
| 250 | + "aptos_test_int_counter", |
| 251 | + "this is a help message", |
| 252 | + ); |
| 253 | + make_thread_local_int_counter_vec!( |
| 254 | + pub(self), |
| 255 | + TEST_INT_COUNTER_VEC, |
| 256 | + "aptos_test_int_counter_vec", |
| 257 | + "this is a help message", |
| 258 | + &["label"], |
| 259 | + ); |
| 260 | + make_thread_local_histogram_vec!( |
| 261 | + pub(self), |
| 262 | + TEST_HISTOGRAM_VEC, |
| 263 | + "aptos_test_histogram_vec", |
| 264 | + "this is a help message", |
| 265 | + &["label"], |
| 266 | + ); |
| 267 | + |
| 268 | + #[test] |
| 269 | + fn test_thread_local_int_counter() { |
| 270 | + TEST_INT_COUNTER.inc(); |
| 271 | + TEST_INT_COUNTER.inc_by(2); |
| 272 | + } |
| 273 | + |
| 274 | + #[test] |
| 275 | + fn test_thread_local_int_counter_vec() { |
| 276 | + TEST_INT_COUNTER_VEC.inc_with(&["foo"]); |
| 277 | + TEST_INT_COUNTER_VEC.inc_with_by(&["foo"], 2); |
| 278 | + } |
| 279 | + |
| 280 | + #[test] |
| 281 | + fn test_thread_local_histogram_vec() { |
| 282 | + let _timer = TEST_HISTOGRAM_VEC.timer_with(&["foo"]); |
| 283 | + TEST_HISTOGRAM_VEC.observe_with(&["bar"], 1.0); |
| 284 | + } |
| 285 | +} |
0 commit comments