Skip to content

Commit 8794cc6

Browse files
authored
metrics: Add support for microsecond counters (#772)
Also fix broken summary support; and ensure that all features are checked in CI.
1 parent d2a25ce commit 8794cc6

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

Cargo.lock

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ dependencies = [
12451245
"indexmap",
12461246
"parking_lot",
12471247
"quickcheck",
1248-
"tokio 0.2.23",
1248+
"tokio 0.3.5",
12491249
"tracing",
12501250
]
12511251

@@ -2581,7 +2581,6 @@ dependencies = [
25812581
"memchr 2.3.3",
25822582
"pin-project-lite 0.1.4",
25832583
"slab",
2584-
"tokio-macros 0.2.6",
25852584
"tracing",
25862585
]
25872586

@@ -2603,7 +2602,7 @@ dependencies = [
26032602
"pin-project-lite 0.2.0",
26042603
"signal-hook-registry",
26052604
"slab",
2606-
"tokio-macros 0.3.1",
2605+
"tokio-macros",
26072606
"winapi 0.3.8",
26082607
]
26092608

@@ -2637,17 +2636,6 @@ dependencies = [
26372636
"log",
26382637
]
26392638

2640-
[[package]]
2641-
name = "tokio-macros"
2642-
version = "0.2.6"
2643-
source = "registry+https://github.com/rust-lang/crates.io-index"
2644-
checksum = "e44da00bfc73a25f814cd8d7e57a68a5c31b74b3152a0a1d1f590c97ed06265a"
2645-
dependencies = [
2646-
"proc-macro2",
2647-
"quote",
2648-
"syn",
2649-
]
2650-
26512639
[[package]]
26522640
name = "tokio-macros"
26532641
version = "0.3.1"

linkerd/metrics/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ http = "0.2"
1818
hyper = "0.14.0-dev"
1919
indexmap = "1.0"
2020
parking_lot = { version = "0.11", optional = true }
21-
tokio = { version = "0.2", features = ["time"], optional = true }
21+
tokio = { version = "0.3", features = ["time"], optional = true }
2222
tracing = "0.1.2"
2323

2424
[dev-dependencies]
2525
quickcheck = { version = "0.9", default-features = false }
26-
tokio = { version = "0.2", features = ["macros", "test-util", "time"] }
26+
tokio = { version = "0.3", features = ["macros", "rt-multi-thread", "test-util", "time"] }

linkerd/metrics/src/counter.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<F: Factor> FmtMetric for Counter<F> {
9494
#[cfg(test)]
9595
mod tests {
9696
use super::*;
97-
use crate::{MillisAsSeconds, MAX_PRECISE_UINT64};
97+
use crate::{MicrosAsSeconds, MillisAsSeconds, MAX_PRECISE_UINT64};
9898

9999
#[test]
100100
fn count_simple() {
@@ -140,4 +140,24 @@ mod tests {
140140
let max = Counter::<MillisAsSeconds>::from(MAX_PRECISE_UINT64 * 1000);
141141
assert_eq!(max.value(), MAX_PRECISE_UINT64 as f64);
142142
}
143+
144+
#[test]
145+
fn micros_as_seconds() {
146+
let c = Counter::<MicrosAsSeconds>::from(1);
147+
assert_eq!(c.value(), 0.000_001);
148+
c.add(110);
149+
assert_eq!(c.value(), 0.000_111);
150+
151+
let c = Counter::<MicrosAsSeconds>::from((MAX_PRECISE_UINT64 - 1) * 1000);
152+
assert_eq!(c.value(), (MAX_PRECISE_UINT64 - 1) as f64 * 0.001);
153+
c.add(1_000);
154+
assert_eq!(c.value(), MAX_PRECISE_UINT64 as f64 * 0.001);
155+
c.add(1_000);
156+
assert_eq!(c.value(), 0.0);
157+
c.add(1);
158+
assert_eq!(c.value(), 0.000_001);
159+
160+
let max = Counter::<MicrosAsSeconds>::from(MAX_PRECISE_UINT64 * 1000);
161+
assert_eq!(max.value(), MAX_PRECISE_UINT64 as f64 * 0.001);
162+
}
143163
}

linkerd/metrics/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ pub trait Factor {
4040
fn factor(n: u64) -> f64;
4141
}
4242

43+
pub struct MicrosAsSeconds;
44+
4345
pub struct MillisAsSeconds;
4446

4547
/// Largest `u64` that can fit without loss of precision in `f64` (2^53).
@@ -60,3 +62,9 @@ impl Factor for MillisAsSeconds {
6062
n.wrapping_rem((MAX_PRECISE_UINT64 + 1) * 1000) as f64 * 0.001
6163
}
6264
}
65+
66+
impl Factor for MicrosAsSeconds {
67+
fn factor(n: u64) -> f64 {
68+
n.wrapping_rem((MAX_PRECISE_UINT64 + 1) * 1_000) as f64 * 0.000_001
69+
}
70+
}

linkerd/metrics/src/summary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<F> Summary<F> {
154154
/// necessary.
155155
#[inline]
156156
fn rotated_window_mut(&self) -> MappedMutexGuard<'_, Histogram<u64>> {
157-
let mut w = self.windows.write();
157+
let mut w = self.windows.lock();
158158
let now = time::Instant::now();
159159
if now >= w.next_rotate {
160160
// Advance windows per elapsed time. If the more than one interval
@@ -178,7 +178,7 @@ impl<F> Summary<F> {
178178
let mut report = self.report.lock();
179179
// Remove all values from the merged
180180
report.reset();
181-
let windows = self.windows.read();
181+
let windows = self.windows.lock();
182182
for w in windows.active.iter() {
183183
// The report histogram must have been created with the same
184184
// configuration as the other histograms, so they all either share a

0 commit comments

Comments
 (0)