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

Add blanket implementations of Recorder for some pointer-like types #507

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions metrics-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ quanta = { version = "0.12", default-features = false, optional = true }
sketches-ddsketch = { version = "0.2", default-features = false, optional = true }
radix_trie = { version = "0.2", default-features = false, optional = true }
ordered-float = { version = "4.2", default-features = false, optional = true }
num_cpus = { version = "1", default-features = false, optional = true }
ahash = { version = "0.8.8", default-features = false, optional = true }
hashbrown = { version = "0.14", default-features = false, optional = true, features = ["ahash"] }

Expand Down Expand Up @@ -90,4 +89,4 @@ layer-filter = ["aho-corasick"]
layer-router = ["radix_trie"]
summary = ["sketches-ddsketch"]
recency = ["registry", "quanta"]
registry = ["crossbeam-epoch", "crossbeam-utils", "handles", "hashbrown", "num_cpus"]
registry = ["crossbeam-epoch", "crossbeam-utils", "handles", "hashbrown"]
47 changes: 46 additions & 1 deletion metrics-util/src/layers/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ mod tests {
predicate::{always, eq},
Sequence,
};
use std::borrow::Cow;
use std::{borrow::Cow, rc::Rc};

use super::RouterBuilder;
use crate::MetricKindMask;
Expand Down Expand Up @@ -265,4 +265,49 @@ mod tests {
let _ = recorder.register_counter(&all_override, &METADATA);
let _ = recorder.register_histogram(&all_override, &METADATA);
}

#[test]
fn test_same_recorder_multiple_routes() {
let default_counter: Key = "default".into();
let foo_counter: Key = "foo.counter".into();
let bar_counter: Key = "bar.counter".into();

let mut default_mock = MockTestRecorder::new();
let mut foo_bar_mock = MockTestRecorder::new();

let mut seq = Sequence::new();

static METADATA: metrics::Metadata =
metrics::Metadata::new(module_path!(), metrics::Level::INFO, Some(module_path!()));

foo_bar_mock
.expect_register_counter()
.times(1)
.in_sequence(&mut seq)
.with(eq(foo_counter.clone()), always())
.returning(|_, _| Counter::noop());
foo_bar_mock
.expect_register_counter()
.times(1)
.in_sequence(&mut seq)
.with(eq(bar_counter.clone()), always())
.returning(|_, _| Counter::noop());
default_mock
.expect_register_counter()
.times(1)
.in_sequence(&mut seq)
.with(eq(default_counter.clone()), always())
.returning(|_, _| Counter::noop());

let foo_bar_mock = Rc::new(foo_bar_mock);

let mut builder = RouterBuilder::from_recorder(default_mock);
builder.add_route(MetricKindMask::COUNTER, "foo", foo_bar_mock.clone());
builder.add_route(MetricKindMask::COUNTER, "bar", foo_bar_mock);
let recorder = builder.build();

let _ = recorder.register_counter(&foo_counter, &METADATA);
let _ = recorder.register_counter(&bar_counter, &METADATA);
let _ = recorder.register_counter(&default_counter, &METADATA);
}
}
8 changes: 6 additions & 2 deletions metrics-util/src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ where
storage: S,
}

fn shard_count() -> usize {
std::thread::available_parallelism().map(|x| x.get()).unwrap_or(1).next_power_of_two()
}

impl Registry<Key, AtomicStorage> {
/// Creates a new `Registry` using a regular [`Key`] and atomic storage.
pub fn atomic() -> Self {
let shard_count = std::cmp::max(1, num_cpus::get()).next_power_of_two();
let shard_count = shard_count();
let shard_mask = shard_count - 1;
let counters =
repeat(()).take(shard_count).map(|_| RwLock::new(RegistryHashMap::default())).collect();
Expand All @@ -78,7 +82,7 @@ where
{
/// Creates a new `Registry`.
pub fn new(storage: S) -> Self {
let shard_count = std::cmp::max(1, num_cpus::get()).next_power_of_two();
let shard_count = shard_count();
let shard_mask = shard_count - 1;
let counters =
repeat(()).take(shard_count).map(|_| RwLock::new(RegistryHashMap::default())).collect();
Expand Down
112 changes: 108 additions & 4 deletions metrics/src/recorder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{cell::Cell, ptr::NonNull};
use std::{cell::Cell, ops::Deref, ptr::NonNull, rc::Rc, sync::Arc};

mod cell;
use self::cell::RecorderOnceCell;
Expand Down Expand Up @@ -57,6 +57,95 @@ pub trait Recorder {
fn register_histogram(&self, key: &Key, metadata: &Metadata<'_>) -> Histogram;
}

// Blanket implementations.

impl<T> Recorder for Rc<T>
where
T: Recorder,
{
fn describe_counter(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_counter(key, unit, description)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to just do (**self), or even self.as_ref(), which I think are both a bit more idiomatic than Deref::deref(self). Dealer's choice on which one you choose... but just not Deref::deref(self). 😂


fn describe_gauge(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_gauge(key, unit, description)
}

fn describe_histogram(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_histogram(key, unit, description)
}

fn register_counter(&self, key: &Key, metadata: &Metadata<'_>) -> Counter {
Deref::deref(self).register_counter(key, metadata)
}

fn register_gauge(&self, key: &Key, metadata: &Metadata<'_>) -> Gauge {
Deref::deref(self).register_gauge(key, metadata)
}

fn register_histogram(&self, key: &Key, metadata: &Metadata<'_>) -> Histogram {
Deref::deref(self).register_histogram(key, metadata)
}
}

impl<T> Recorder for Arc<T>
where
T: Recorder,
{
fn describe_counter(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_counter(key, unit, description)
}

fn describe_gauge(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_gauge(key, unit, description)
}

fn describe_histogram(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_histogram(key, unit, description)
}

fn register_counter(&self, key: &Key, metadata: &Metadata<'_>) -> Counter {
Deref::deref(self).register_counter(key, metadata)
}

fn register_gauge(&self, key: &Key, metadata: &Metadata<'_>) -> Gauge {
Deref::deref(self).register_gauge(key, metadata)
}

fn register_histogram(&self, key: &Key, metadata: &Metadata<'_>) -> Histogram {
Deref::deref(self).register_histogram(key, metadata)
}
}

impl<T> Recorder for Box<T>
where
T: Recorder,
{
fn describe_counter(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_counter(key, unit, description)
}

fn describe_gauge(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_gauge(key, unit, description)
}

fn describe_histogram(&self, key: KeyName, unit: Option<Unit>, description: SharedString) {
Deref::deref(self).describe_histogram(key, unit, description)
}

fn register_counter(&self, key: &Key, metadata: &Metadata<'_>) -> Counter {
Deref::deref(self).register_counter(key, metadata)
}

fn register_gauge(&self, key: &Key, metadata: &Metadata<'_>) -> Gauge {
Deref::deref(self).register_gauge(key, metadata)
}

fn register_histogram(&self, key: &Key, metadata: &Metadata<'_>) -> Histogram {
Deref::deref(self).register_histogram(key, metadata)
}
}

/// Guard for setting a local recorder.
///
/// When using a local recorder, we take a reference to the recorder and only hold it for as long as
Expand Down Expand Up @@ -142,11 +231,16 @@ pub fn with_recorder<T>(f: impl FnOnce(&dyn Recorder) -> T) -> T {

#[cfg(test)]
mod tests {
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
use std::{
rc::Rc,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};

use crate::NoopRecorder;

use super::{Recorder, RecorderOnceCell};

#[test]
Expand Down Expand Up @@ -227,4 +321,14 @@ mod tests {
drop(second_set_result);
assert!(was_dropped.load(Ordering::SeqCst));
}

#[test]
fn blanket_implementations() {
fn is_recorder<T: Recorder>(_recorder: T) {}

is_recorder(NoopRecorder);
is_recorder(Rc::new(NoopRecorder));
is_recorder(Arc::new(NoopRecorder));
is_recorder(Box::new(NoopRecorder));
}
}
Loading