Skip to content

Commit 2cb37cb

Browse files
committed
experiment: try to reduce overhead of performing counter increments/gauge sets
1 parent ffc57f0 commit 2cb37cb

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

metrics-exporter-dogstatsd/examples/dogstatsd_synchronous.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ fn main() {
99
DogStatsDBuilder::default()
1010
.with_remote_address("localhost:9125")
1111
.expect("failed to parse remote address")
12+
.with_histogram_sampling(true)
13+
.with_histogram_reservoir_size(128)
1214
.install()
1315
.expect("failed to install DogStatsD recorder");
1416

metrics-exporter-dogstatsd/src/storage.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,79 @@ use metrics_util::{
1919
};
2020

2121
pub(crate) struct AtomicCounter {
22-
is_absolute: AtomicBool,
23-
last: AtomicU64,
22+
//is_absolute: AtomicBool,
23+
//last: AtomicU64,
2424
current: AtomicU64,
25-
updates: AtomicU64,
25+
//updates: AtomicU64,
2626
}
2727

2828
impl AtomicCounter {
2929
/// Creates a new `AtomicCounter`.
3030
fn new() -> Self {
3131
Self {
32-
is_absolute: AtomicBool::new(false),
33-
last: AtomicU64::new(0),
32+
//is_absolute: AtomicBool::new(false),
33+
//last: AtomicU64::new(0),
3434
current: AtomicU64::new(0),
35-
updates: AtomicU64::new(0),
35+
//updates: AtomicU64::new(0),
3636
}
3737
}
3838

3939
/// Flushes the current counter value, returning the delta of the counter value, and the number of updates, since
4040
/// the last flush.
4141
pub fn flush(&self) -> (u64, u64) {
42-
let current = self.current.load(Acquire);
43-
let last = self.last.swap(current, AcqRel);
44-
let delta = current.wrapping_sub(last);
45-
let updates = self.updates.swap(0, AcqRel);
46-
47-
(delta, updates)
42+
//let current = self.current.load(Acquire);
43+
//let last = self.last.swap(current, AcqRel);
44+
//let delta = current.wrapping_sub(last);
45+
//let updates = self.updates.swap(0, AcqRel);
46+
47+
//(delta, updates)
48+
let delta = self.current.swap(0, Relaxed);
49+
(delta, if delta > 0 { 1 } else { 0 })
4850
}
4951
}
5052

5153
impl CounterFn for AtomicCounter {
5254
fn increment(&self, value: u64) {
53-
self.is_absolute.store(false, Release);
55+
//self.is_absolute.store(false, Release);
5456
self.current.fetch_add(value, Relaxed);
55-
self.updates.fetch_add(1, Relaxed);
57+
//self.updates.fetch_add(1, Relaxed);
5658
}
5759

5860
fn absolute(&self, value: u64) {
5961
// Ensure the counter is in absolute mode, and if it wasn't already, reset `last` to `value` to give ourselves a
6062
// consistent starting point when flushing. This ensures that we only start flushing deltas once we've gotten
6163
// two consecutive absolute values, since otherwise we might be calculating a delta between a `last` of 0 and a
6264
// very large `current` value.
63-
if !self.is_absolute.swap(true, Release) {
64-
self.last.store(value, Release);
65-
}
65+
//if !self.is_absolute.swap(true, Release) {
66+
// self.last.store(value, Release);
67+
//}
6668

67-
self.current.store(value, Release);
68-
self.updates.fetch_add(1, Relaxed);
69+
//self.current.store(value, Release);
70+
//self.updates.fetch_add(1, Relaxed);
6971
}
7072
}
7173

7274
pub(crate) struct AtomicGauge {
7375
inner: AtomicU64,
74-
updates: AtomicU64,
76+
//updates: AtomicU64,
7577
}
7678

7779
impl AtomicGauge {
7880
/// Creates a new `AtomicGauge`.
7981
fn new() -> Self {
80-
Self { inner: AtomicU64::new(0.0f64.to_bits()), updates: AtomicU64::new(0) }
82+
Self {
83+
inner: AtomicU64::new(0.0f64.to_bits()),
84+
//updates: AtomicU64::new(0),
85+
}
8186
}
8287

8388
/// Flushes the current gauge value and the number of updates since the last flush.
8489
pub fn flush(&self) -> (f64, u64) {
8590
let current = f64::from_bits(self.inner.load(Acquire));
86-
let updates = self.updates.swap(0, AcqRel);
91+
//let updates = self.updates.swap(0, AcqRel);
8792

88-
(current, updates)
93+
//(current, updates)
94+
(current, 0)
8995
}
9096
}
9197

@@ -97,7 +103,7 @@ impl GaugeFn for AtomicGauge {
97103
Some(f64::to_bits(new))
98104
})
99105
.expect("should never fail to update gauge");
100-
self.updates.fetch_add(1, Relaxed);
106+
//self.updates.fetch_add(1, Relaxed);
101107
}
102108

103109
fn decrement(&self, value: f64) {
@@ -107,12 +113,12 @@ impl GaugeFn for AtomicGauge {
107113
Some(f64::to_bits(new))
108114
})
109115
.expect("should never fail to update gauge");
110-
self.updates.fetch_add(1, Relaxed);
116+
//self.updates.fetch_add(1, Relaxed);
111117
}
112118

113119
fn set(&self, value: f64) {
114120
self.inner.store(value.to_bits(), Release);
115-
self.updates.fetch_add(1, Relaxed);
121+
//self.updates.fetch_add(1, Relaxed);
116122
}
117123
}
118124

0 commit comments

Comments
 (0)