Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental: metric handles #240

Merged
merged 16 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,47 @@ jobs:
- name: Install Rust Nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rustfmt, clippy
toolchain: nightly
override: true
components: rustfmt, clippy
- name: Check Formatting
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
feature-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install cargo-hack
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-hack
- name: Check Feature Matrix
uses: actions-rs/cargo@v1
with:
command: hack
args: check --each-feature --no-dev-deps
test:
name: Test ${{ matrix.rust_version }}/${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
rust_version: ['1.49.0', 'stable', 'nightly']
rust_version: ['1.55.0', 'stable', 'nightly']
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v1
- name: Install Rust ${{ matrix.rust_version }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust_version }}
override: true
toolchain: ${{ matrix.rust_version }}
override: true
- name: Run Tests
uses: actions-rs/cargo@v1
with:
Expand All @@ -50,9 +69,9 @@ jobs:
- name: Install Rust Nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rust-docs
toolchain: nightly
override: true
components: rust-docs
- name: Check Docs
uses: actions-rs/cargo@v1
with:
Expand All @@ -69,8 +88,8 @@ jobs:
- name: Install Rust Stable
uses: actions-rs/toolchain@v1
with:
toolchain: 'stable'
override: true
toolchain: stable
override: true
- name: Run Benchmarks
uses: actions-rs/cargo@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion metrics-benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
log = "0.4"
env_logger = "0.9"
pretty_env_logger = "0.4"
getopts = "0.2"
hdrhistogram = { version = "7.2", default-features = false }
quanta = "0.9.3"
Expand Down
160 changes: 104 additions & 56 deletions metrics-benchmark/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use atomic_shim::AtomicU64;
use getopts::Options;
use hdrhistogram::Histogram;
use hdrhistogram::Histogram as HdrHistogram;
use log::{error, info};
use metrics::{gauge, histogram, increment_counter, GaugeValue, Key, Recorder, Unit};
use metrics_util::{Handle, MetricKind, Registry, Tracked};
use metrics::{
gauge, histogram, increment_counter, register_counter, register_gauge, register_histogram,
Counter, Gauge, Histogram, Key, Recorder, Unit,
};
use metrics_util::{Registry, StandardPrimitives};
use quanta::{Clock, Instant as QuantaInstant};
use std::{
env,
Expand All @@ -19,19 +22,17 @@ use std::{
const LOOP_SAMPLE: u64 = 1000;

pub struct Controller {
registry: Arc<Registry<Key, Handle, Tracked<Handle>>>,
registry: Arc<Registry<StandardPrimitives>>,
}

impl Controller {
/// Takes a snapshot of the recorder.
// Performs the traditional "upkeep" of a recorder i.e. clearing histogram buckets, etc.
/// Performs the traditional "upkeep" of a recorder i.e. clearing histogram buckets, etc.
pub fn upkeep(&self) {
let handles = self.registry.get_handles();
let handles = self.registry.get_histogram_handles();

for ((kind, _), (_, handle)) in handles {
if matches!(kind, MetricKind::Histogram) {
handle.read_histogram_with_clear(|_| {});
}
for (_, histo) in handles {
histo.clear();
}
}
}
Expand All @@ -40,14 +41,14 @@ impl Controller {
///
/// Simulates typical recorder implementations by utilizing `Registry`, clearing histogram buckets, etc.
pub struct BenchmarkingRecorder {
registry: Arc<Registry<Key, Handle, Tracked<Handle>>>,
registry: Arc<Registry<StandardPrimitives>>,
}

impl BenchmarkingRecorder {
/// Creates a new `BenchmarkingRecorder`.
pub fn new() -> BenchmarkingRecorder {
BenchmarkingRecorder {
registry: Arc::new(Registry::<Key, Handle, Tracked<Handle>>::tracked()),
registry: Arc::new(Registry::new()),
}
}

Expand All @@ -65,51 +66,31 @@ impl BenchmarkingRecorder {
}

impl Recorder for BenchmarkingRecorder {
fn register_counter(&self, key: &Key, _unit: Option<Unit>, _description: Option<&'static str>) {
self.registry
.op(MetricKind::Counter, key, |_| {}, Handle::counter)
fn describe_counter(&self, key: &Key, _: Option<Unit>, _: Option<&'static str>) {
self.registry.get_or_create_counter(key, |_| {})
}

fn register_gauge(&self, key: &Key, _unit: Option<Unit>, _description: Option<&'static str>) {
self.registry
.op(MetricKind::Gauge, key, |_| {}, Handle::gauge)
fn describe_gauge(&self, key: &Key, _: Option<Unit>, _: Option<&'static str>) {
self.registry.get_or_create_gauge(key, |_| {})
}

fn register_histogram(
&self,
key: &Key,
_unit: Option<Unit>,
_description: Option<&'static str>,
) {
self.registry
.op(MetricKind::Histogram, key, |_| {}, Handle::histogram)
fn describe_histogram(&self, key: &Key, _: Option<Unit>, _: Option<&'static str>) {
self.registry.get_or_create_histogram(key, |_| {})
}

fn increment_counter(&self, key: &Key, value: u64) {
self.registry.op(
MetricKind::Counter,
key,
|handle| handle.increment_counter(value),
Handle::counter,
)
fn register_counter(&self, key: &Key) -> Counter {
self.registry
.get_or_create_counter(key, |c| Counter::from_arc(c.clone()))
}

fn update_gauge(&self, key: &Key, value: GaugeValue) {
self.registry.op(
MetricKind::Gauge,
key,
|handle| handle.update_gauge(value),
Handle::gauge,
)
fn register_gauge(&self, key: &Key) -> Gauge {
self.registry
.get_or_create_gauge(key, |g| Gauge::from_arc(g.clone()))
}

fn record_histogram(&self, key: &Key, value: f64) {
self.registry.op(
MetricKind::Histogram,
key,
|handle| handle.record_histogram(value),
Handle::histogram,
)
fn register_histogram(&self, key: &Key) -> Histogram {
self.registry
.get_or_create_histogram(key, |h| Histogram::from_arc(h.clone()))
}
}

Expand All @@ -122,7 +103,7 @@ impl Default for BenchmarkingRecorder {
struct Generator {
t0: Option<QuantaInstant>,
gauge: i64,
hist: Histogram<u64>,
hist: HdrHistogram<u64>,
done: Arc<AtomicBool>,
rate_counter: Arc<AtomicU64>,
}
Expand All @@ -132,24 +113,25 @@ impl Generator {
Generator {
t0: None,
gauge: 0,
hist: Histogram::<u64>::new_with_bounds(1, u64::max_value(), 3).unwrap(),
hist: HdrHistogram::<u64>::new_with_bounds(1, u64::max_value(), 3).unwrap(),
done,
rate_counter,
}
}

fn run(&mut self) {
fn run_slow(&mut self) {
let clock = Clock::new();
let mut counter = 0;
let mut loop_counter = 0;

loop {
counter += 1;
loop_counter += 1;

self.gauge += 1;

let t1 = clock.recent();

if let Some(t0) = self.t0 {
let start = if counter % LOOP_SAMPLE == 0 {
let start = if loop_counter % LOOP_SAMPLE == 0 {
Some(clock.now())
} else {
None
Expand All @@ -176,6 +158,50 @@ impl Generator {
self.t0 = Some(t1);
}
}

fn run_fast(&mut self) {
let clock = Clock::new();
let mut loop_counter = 0;

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

loop {
loop_counter += 1;

self.gauge += 1;

let t1 = clock.recent();

if let Some(t0) = self.t0 {
let start = if loop_counter % LOOP_SAMPLE == 0 {
Some(clock.now())
} else {
None
};

counter.increment(1);
gauge.set(self.gauge as f64);
histogram.record(t1.sub(t0));

if let Some(val) = start {
let delta = clock.now() - val;
self.hist.saturating_record(delta.as_nanos() as u64);

// We also increment our global counter for the sample rate here.
self.rate_counter
.fetch_add(LOOP_SAMPLE * 3, Ordering::AcqRel);

if self.done.load(Ordering::Relaxed) {
break;
}
}
}

self.t0 = Some(t1);
}
}
}

impl Drop for Generator {
Expand Down Expand Up @@ -206,14 +232,20 @@ pub fn opts() -> Options {
"number of seconds to run the benchmark",
"INTEGER",
);
opts.optopt(
"m",
"mode",
"whether or run the benchmark in slow or fast mode (static vs dynamic handles)",
"STRING",
);
opts.optopt("p", "producers", "number of producers", "INTEGER");
opts.optflag("h", "help", "print this help menu");

opts
}

fn main() {
env_logger::init();
pretty_env_logger::init();

let args: Vec<String> = env::args().collect();
let program = &args[0];
Expand Down Expand Up @@ -245,6 +277,17 @@ fn main() {
.unwrap_or_else(|| "1".to_owned())
.parse()
.unwrap();
let mode = matches
.opt_str("mode")
.map(|s| {
if s.to_ascii_lowercase() == "fast" {
"fast"
} else {
"slow"
}
})
.unwrap_or_else(|| "slow")
.to_owned();

info!("duration: {}s", seconds);
info!("producers: {}", producers);
Expand All @@ -263,9 +306,14 @@ fn main() {
for _ in 0..producers {
let d = done.clone();
let r = rate_counter.clone();
let mode = mode.clone();
let handle = thread::spawn(move || {
let mut gen = Generator::new(d, r);
gen.run();
if mode == "fast" {
gen.run_fast();
} else {
gen.run_slow();
}
});

handles.push(handle);
Expand All @@ -280,7 +328,7 @@ fn main() {
let mut total = 0;
let mut t0 = Instant::now();

let mut upkeep_hist = Histogram::<u64>::new_with_bounds(1, u64::max_value(), 3).unwrap();
let mut upkeep_hist = HdrHistogram::<u64>::new_with_bounds(1, u64::max_value(), 3).unwrap();
for _ in 0..seconds {
let t1 = Instant::now();

Expand Down
2 changes: 1 addition & 1 deletion metrics-exporter-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ keywords = ["metrics", "telemetry", "prometheus"]
[features]
default = ["tokio-exporter"]
tokio-exporter = ["hyper", "ipnet", "tokio"]
push-gateway = ["reqwest", "tracing"]
push-gateway = ["reqwest", "tracing", "tokio-exporter"]

[dependencies]
metrics = { version = "^0.17", path = "../metrics" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use metrics::{
decrement_gauge, gauge, histogram, increment_counter, increment_gauge, register_counter,
register_histogram,
};
use metrics::{describe_counter, describe_histogram};
#[allow(unused_imports)]
use metrics_exporter_prometheus::PrometheusBuilder;
#[allow(unused_imports)]
Expand Down Expand Up @@ -45,11 +46,11 @@ fn main() {
//
// Registering metrics ahead of using them is not required, but is the only way to specify the
// description of a metric.
register_counter!(
describe_counter!(
"tcp_server_loops",
"The iterations of the TCP server event loop so far."
);
register_histogram!(
describe_histogram!(
"tcp_server_loop_delta_ns",
"The time taken for iterations of the TCP server event loop."
);
Expand Down
Loading