Skip to content

Commit

Permalink
simplify macros (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlbarber authored Oct 9, 2023
1 parent 67d5100 commit 3fca508
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 633 deletions.
16 changes: 8 additions & 8 deletions metrics-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use getopts::Options;
use hdrhistogram::Histogram as HdrHistogram;
use log::{error, info};
use metrics::{
gauge, histogram, increment_counter, register_counter, register_gauge, register_histogram,
Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder, SharedString, Unit,
counter, gauge, histogram, Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder,
SharedString, Unit,
};
use metrics_util::registry::{AtomicStorage, Registry};
use portable_atomic::AtomicU64;
Expand Down Expand Up @@ -120,9 +120,9 @@ impl Generator {
if let Some(t0) = self.t0 {
let start = if loop_counter % LOOP_SAMPLE == 0 { Some(clock.now()) } else { None };

increment_counter!("ok");
gauge!("total", self.gauge as f64);
histogram!("ok", t1.sub(t0));
counter!("ok").increment(1);
gauge!("total").set(self.gauge as f64);
histogram!("ok").record(t1.sub(t0));

if let Some(val) = start {
let delta = clock.now() - val;
Expand All @@ -145,9 +145,9 @@ impl Generator {
let clock = Clock::new();
let mut loop_counter = 0;

let counter = register_counter!("ok");
let gauge = register_gauge!("total");
let histogram = register_histogram!("ok");
let counter = counter!("ok");
let gauge = gauge!("total");
let histogram = histogram!("ok");

loop {
loop_counter += 1;
Expand Down
18 changes: 8 additions & 10 deletions metrics-exporter-prometheus/examples/prometheus_push_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use std::thread;
use std::time::Duration;

#[allow(unused_imports)]
use metrics::{
decrement_gauge, gauge, histogram, increment_counter, increment_gauge, register_counter,
register_histogram,
};
use metrics::{counter, gauge, histogram};
use metrics::{describe_counter, describe_histogram};
#[allow(unused_imports)]
use metrics_exporter_prometheus::PrometheusBuilder;
Expand Down Expand Up @@ -50,23 +47,24 @@ fn main() {
let clock = Clock::new();
let mut last = None;

increment_counter!("idle_metric");
gauge!("testing", 42.0);
counter!("idle_metric").increment(1);
gauge!("testing").set(42.0);

// Loop over and over, pretending to do some work.
loop {
increment_counter!("tcp_server_loops", "system" => "foo");
counter!("tcp_server_loops", "system" => "foo").increment(1);

if let Some(t) = last {
let delta: Duration = clock.now() - t;
histogram!("tcp_server_loop_delta_secs", delta, "system" => "foo");
histogram!("tcp_server_loop_delta_secs", "system" => "foo").record(delta);
}

let increment_gauge = thread_rng().gen_bool(0.75);
let gauge = gauge!("lucky_iterations");
if increment_gauge {
increment_gauge!("lucky_iterations", 1.0);
gauge.increment(1.0);
} else {
decrement_gauge!("lucky_iterations", 1.0);
gauge.decrement(1.0);
}

last = Some(clock.now());
Expand Down
18 changes: 8 additions & 10 deletions metrics-exporter-prometheus/examples/prometheus_server.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::thread;
use std::time::Duration;

use metrics::{
decrement_gauge, describe_counter, describe_histogram, gauge, histogram, increment_counter,
increment_gauge,
};
use metrics::{counter, describe_counter, describe_histogram, gauge, histogram};
use metrics_exporter_prometheus::PrometheusBuilder;
use metrics_util::MetricKindMask;

Expand Down Expand Up @@ -38,23 +35,24 @@ fn main() {
let clock = Clock::new();
let mut last = None;

increment_counter!("idle_metric");
gauge!("testing", 42.0);
counter!("idle_metric").increment(1);
gauge!("testing").set(42.0);

// Loop over and over, pretending to do some work.
loop {
increment_counter!("tcp_server_loops", "system" => "foo");
counter!("tcp_server_loops", "system" => "foo").increment(1);

if let Some(t) = last {
let delta: Duration = clock.now() - t;
histogram!("tcp_server_loop_delta_secs", delta, "system" => "foo");
histogram!("tcp_server_loop_delta_secs", "system" => "foo").record(delta);
}

let increment_gauge = thread_rng().gen_bool(0.75);
let gauge = gauge!("lucky_iterations");
if increment_gauge {
increment_gauge!("lucky_iterations", 1.0);
gauge.increment(1.0);
} else {
decrement_gauge!("lucky_iterations", 1.0);
gauge.decrement(1.0);
}

last = Some(clock.now());
Expand Down
13 changes: 6 additions & 7 deletions metrics-exporter-tcp/examples/tcp_server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::thread;
use std::time::Duration;

use metrics::{
decrement_gauge, describe_histogram, histogram, increment_counter, increment_gauge, Unit,
};
use metrics::{counter, describe_histogram, gauge, histogram, Unit};
use metrics_exporter_tcp::TcpBuilder;

use quanta::Clock;
Expand All @@ -25,18 +23,19 @@ fn main() {
);

loop {
increment_counter!("tcp_server_loops", "system" => "foo");
counter!("tcp_server_loops", "system" => "foo").increment(1);

if let Some(t) = last {
let delta: Duration = clock.now() - t;
histogram!("tcp_server_loop_delta_secs", delta, "system" => "foo");
histogram!("tcp_server_loop_delta_secs", "system" => "foo").record(delta);
}

let increment_gauge = thread_rng().gen_bool(0.75);
let gauge = gauge!("lucky_iterations");
if increment_gauge {
increment_gauge!("lucky_iterations", 1.0);
gauge.increment(1.0);
} else {
decrement_gauge!("lucky_iterations", 1.0);
gauge.decrement(1.0);
}

last = Some(clock.now());
Expand Down
2 changes: 1 addition & 1 deletion metrics-tracing-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
//! let span = span!(Level::TRACE, "login", user);
//! let _guard = span.enter();
//!
//! counter!("login_attempts", 1, "service" => "login_service");
//! counter!("login_attempts", "service" => "login_service").increment(1);
//! ```
//!
//! The code above will emit a increment for a `login_attempts` counter with
Expand Down
23 changes: 12 additions & 11 deletions metrics-tracing-context/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn test_basic_functionality() {
let span = span!(Level::TRACE, "login", user, user.email = email);
let _guard = span.enter();

counter!("login_attempts", 1, "service" => "login_service");
counter!("login_attempts", "service" => "login_service").increment(1);

let snapshot = snapshotter.snapshot().into_vec();

Expand Down Expand Up @@ -123,15 +123,16 @@ fn test_macro_forms() {
let _guard = span.enter();

// No labels.
counter!("login_attempts_no_labels", 1);
counter!("login_attempts_no_labels").increment(1);
// Static labels only.
counter!("login_attempts_static_labels", 1, "service" => "login_service");
counter!("login_attempts_static_labels", "service" => "login_service").increment(1);
// Dynamic labels only.
let node_name = "localhost".to_string();
counter!("login_attempts_dynamic_labels", 1, "node_name" => node_name.clone());
counter!("login_attempts_dynamic_labels", "node_name" => node_name.clone()).increment(1);
// Static and dynamic.
counter!("login_attempts_static_and_dynamic_labels", 1,
"service" => "login_service", "node_name" => node_name.clone());
counter!("login_attempts_static_and_dynamic_labels",
"service" => "login_service", "node_name" => node_name.clone())
.increment(1);

let snapshot = snapshotter.snapshot().into_vec();

Expand Down Expand Up @@ -185,7 +186,7 @@ fn test_no_labels() {
let span = span!(Level::TRACE, "login");
let _guard = span.enter();

counter!("login_attempts", 1);
counter!("login_attempts").increment(1);

let snapshot = snapshotter.snapshot().into_vec();

Expand All @@ -205,7 +206,7 @@ fn test_multiple_paths_to_the_same_callsite() {
let (_guard, snapshotter) = setup(TracingContextLayer::all());

let shared_fn = || {
counter!("my_counter", 1);
counter!("my_counter").increment(1);
};

let path1 = || {
Expand Down Expand Up @@ -279,7 +280,7 @@ fn test_nested_spans() {
);
let _guard = span.enter();

counter!("my_counter", 1);
counter!("my_counter").increment(1);
};

let outer = || {
Expand Down Expand Up @@ -331,7 +332,7 @@ fn test_label_filtering() {
let span = span!(Level::TRACE, "login", user, user.email_span = email);
let _guard = span.enter();

counter!("login_attempts", 1, "user.email" => "[email protected]");
counter!("login_attempts", "user.email" => "[email protected]").increment(1);

let snapshot = snapshotter.snapshot().into_vec();

Expand Down Expand Up @@ -365,7 +366,7 @@ fn test_label_allowlist() {
);
let _guard = span.enter();

counter!("login_attempts", 1);
counter!("login_attempts").increment(1);

let snapshot = snapshotter.snapshot().into_vec();

Expand Down
4 changes: 2 additions & 2 deletions metrics-util/src/debugging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,13 @@ mod tests {
recorder.install().expect("installing debugging recorder should not fail");

let t1 = std::thread::spawn(|| {
counter!("test_counter", 43);
counter!("test_counter").increment(43);

Snapshotter::current_thread_snapshot()
});

let t2 = std::thread::spawn(|| {
counter!("test_counter", 47);
counter!("test_counter").increment(47);

Snapshotter::current_thread_snapshot()
});
Expand Down
36 changes: 18 additions & 18 deletions metrics-util/src/recoverable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,12 @@ mod tests {

// Record some metrics, and make sure the atomics for each metric type are
// incremented as we would expect them to be.
metrics::counter!("counter", 5);
metrics::increment_gauge!("gauge", 5.0);
metrics::increment_gauge!("gauge", 5.0);
metrics::histogram!("histogram", 5.0);
metrics::histogram!("histogram", 5.0);
metrics::histogram!("histogram", 5.0);
metrics::counter!("counter").increment(5);
metrics::gauge!("gauge").increment(5.0);
metrics::gauge!("gauge").increment(5.0);
metrics::histogram!("histogram").record(5.0);
metrics::histogram!("histogram").record(5.0);
metrics::histogram!("histogram").record(5.0);

let _recorder = recoverable.into_inner();
assert_eq!(counter.get(), 5);
Expand All @@ -272,9 +272,9 @@ mod tests {

// Now that we've recovered the recorder, incrementing the same metrics should
// not actually increment the value of the atomics for each metric type.
metrics::counter!("counter", 7);
metrics::increment_gauge!("gauge", 7.0);
metrics::histogram!("histogram", 7.0);
metrics::counter!("counter").increment(7);
metrics::gauge!("gauge").increment(7.0);
metrics::histogram!("histogram").record(7.0);

assert_eq!(counter.get(), 5);
assert_eq!(gauge.get(), 10);
Expand All @@ -293,12 +293,12 @@ mod tests {

// Record some metrics, and make sure the atomics for each metric type are
// incremented as we would expect them to be.
metrics::counter!("counter", 5);
metrics::increment_gauge!("gauge", 5.0);
metrics::increment_gauge!("gauge", 5.0);
metrics::histogram!("histogram", 5.0);
metrics::histogram!("histogram", 5.0);
metrics::histogram!("histogram", 5.0);
metrics::counter!("counter").increment(5);
metrics::gauge!("gauge").increment(5.0);
metrics::gauge!("gauge").increment(5.0);
metrics::histogram!("histogram").record(5.0);
metrics::histogram!("histogram").record(5.0);
metrics::histogram!("histogram").record(5.0);

drop(recoverable.into_inner());
assert_eq!(counter.get(), 5);
Expand All @@ -307,9 +307,9 @@ mod tests {

// Now that we've recovered the recorder, incrementing the same metrics should
// not actually increment the value of the atomics for each metric type.
metrics::counter!("counter", 7);
metrics::increment_gauge!("gauge", 7.0);
metrics::histogram!("histogram", 7.0);
metrics::counter!("counter").increment(7);
metrics::gauge!("gauge").increment(7.0);
metrics::histogram!("histogram").record(7.0);

assert_eq!(counter.get(), 5);
assert_eq!(gauge.get(), 10);
Expand Down
11 changes: 6 additions & 5 deletions metrics/benches/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ fn macro_benchmark(c: &mut Criterion) {
metrics::clear_recorder();
}
b.iter(|| {
counter!("counter_bench", 42);
counter!("counter_bench").increment(42);
})
});
group.bench_function("uninitialized/with_static_labels", |b| {
unsafe {
metrics::clear_recorder();
}
b.iter(|| {
counter!("counter_bench", 42, "request" => "http", "svc" => "admin");
counter!("counter_bench", "request" => "http", "svc" => "admin").increment(42);
})
});
group.bench_function("initialized/no_labels", |b| {
reset_recorder();
b.iter(|| {
counter!("counter_bench", 42);
counter!("counter_bench").increment(42);
});
unsafe {
metrics::clear_recorder();
Expand All @@ -62,7 +62,7 @@ fn macro_benchmark(c: &mut Criterion) {
group.bench_function("initialized/with_static_labels", |b| {
reset_recorder();
b.iter(|| {
counter!("counter_bench", 42, "request" => "http", "svc" => "admin");
counter!("counter_bench", "request" => "http", "svc" => "admin").increment(42);
});
unsafe {
metrics::clear_recorder();
Expand All @@ -73,7 +73,8 @@ fn macro_benchmark(c: &mut Criterion) {

reset_recorder();
b.iter(move || {
counter!("counter_bench", 42, "request" => "http", "uid" => label_val.clone());
counter!("counter_bench", "request" => "http", "uid" => label_val.clone())
.increment(42);
});
unsafe {
metrics::clear_recorder();
Expand Down
Loading

0 comments on commit 3fca508

Please sign in to comment.