Skip to content

Commit

Permalink
rough pass at parameterizing generation tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz committed May 18, 2021
1 parent 2db9699 commit 5dbe21f
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 52 deletions.
8 changes: 4 additions & 4 deletions metrics-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use getopts::Options;
use hdrhistogram::Histogram;
use log::{error, info};
use metrics::{gauge, histogram, increment_counter, GaugeValue, Key, Recorder, Unit};
use metrics_util::{Handle, MetricKind, Registry};
use metrics_util::{Handle, MetricKind, Registry, Tracked};
use quanta::{Clock, Instant as QuantaInstant};
use std::{
env,
Expand All @@ -19,7 +19,7 @@ use std::{
const LOOP_SAMPLE: u64 = 1000;

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

impl Controller {
Expand All @@ -40,14 +40,14 @@ impl Controller {
///
/// Simulates typical recorder implementations by utilizing `Registry`, clearing histogram buckets, etc.
pub struct BenchmarkingRecorder {
registry: Arc<Registry<Key, Handle>>,
registry: Arc<Registry<Key, Handle, Tracked<Handle>>>,
}

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

Expand Down
5 changes: 3 additions & 2 deletions metrics-exporter-prometheus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use hyper::{
service::{make_service_fn, service_fn},
StatusCode, {Body, Error as HyperError, Response},
};
use metrics::Key;
use parking_lot::RwLock;
use quanta::Clock;
#[cfg(feature = "tokio-exporter")]
use tokio::{pin, runtime, select};

use metrics_util::{parse_quantiles, MetricKindMask, Quantile, Recency, Registry};
use metrics_util::{parse_quantiles, Handle, MetricKindMask, Quantile, Recency, Registry, Tracked};

#[cfg(feature = "tokio-exporter")]
use crate::common::InstallError;
Expand Down Expand Up @@ -200,7 +201,7 @@ impl PrometheusBuilder {

pub(crate) fn build_with_clock(self, clock: Clock) -> PrometheusRecorder {
let inner = Inner {
registry: Registry::new(),
registry: Registry::<Key, Handle, Tracked<Handle>>::tracked(),
recency: Recency::new(clock, self.recency_mask, self.idle_timeout),
distributions: RwLock::new(HashMap::new()),
distribution_builder: DistributionBuilder::new(
Expand Down
6 changes: 3 additions & 3 deletions metrics-exporter-prometheus/src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::sync::Arc;
use parking_lot::RwLock;

use metrics::{GaugeValue, Key, Recorder, Unit};
use metrics_util::{Handle, MetricKind, Recency, Registry};
use metrics_util::{Handle, MetricKind, Recency, Registry, Tracked};

use crate::common::{sanitize_key_name, Snapshot};
use crate::distribution::{Distribution, DistributionBuilder};

pub(crate) struct Inner {
pub registry: Registry<Key, Handle>,
pub registry: Registry<Key, Handle, Tracked<Handle>>,
pub recency: Recency<Key>,
pub distributions: RwLock<HashMap<String, HashMap<Vec<String>, Distribution>>>,
pub distribution_builder: DistributionBuilder,
Expand All @@ -19,7 +19,7 @@ pub(crate) struct Inner {
}

impl Inner {
pub fn registry(&self) -> &Registry<Key, Handle> {
pub fn registry(&self) -> &Registry<Key, Handle, Tracked<Handle>> {
&self.registry
}

Expand Down
14 changes: 7 additions & 7 deletions metrics-util/benches/registry.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use metrics::{Key, Label};
use metrics_util::{MetricKind, Registry};
use metrics_util::{MetricKind, NotTracked, Registry};

fn registry_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("registry");
let mut group = c.benchmark_group("registry (not tracked)");
group.bench_function("cached op (basic)", |b| {
let registry: Registry<Key, ()> = Registry::new();
let registry = Registry::<Key, (), NotTracked<()>>::untracked();
static KEY_NAME: &'static str = "simple_key";
static KEY_DATA: Key = Key::from_static_name(&KEY_NAME);

b.iter(|| registry.op(MetricKind::Counter, &KEY_DATA, |_| (), || ()))
});
group.bench_function("cached op (labels)", |b| {
let registry: Registry<Key, ()> = Registry::new();
let registry = Registry::<Key, (), NotTracked<()>>::untracked();
static KEY_NAME: &'static str = "simple_key";
static KEY_LABELS: [Label; 1] = [Label::from_static_parts("type", "http")];
static KEY_DATA: Key = Key::from_static_parts(&KEY_NAME, &KEY_LABELS);
Expand All @@ -21,7 +21,7 @@ fn registry_benchmark(c: &mut Criterion) {
});
group.bench_function("uncached op (basic)", |b| {
b.iter_batched_ref(
|| Registry::<Key, ()>::new(),
|| Registry::<Key, (), NotTracked<()>>::untracked(),
|registry| {
let key = "simple_key".into();
registry.op(MetricKind::Counter, &key, |_| (), || ())
Expand All @@ -31,7 +31,7 @@ fn registry_benchmark(c: &mut Criterion) {
});
group.bench_function("uncached op (labels)", |b| {
b.iter_batched_ref(
|| Registry::<Key, ()>::new(),
|| Registry::<Key, (), NotTracked<()>>::untracked(),
|registry| {
let labels = vec![Label::new("type", "http")];
let key = ("simple_key", labels).into();
Expand All @@ -43,7 +43,7 @@ fn registry_benchmark(c: &mut Criterion) {
group.bench_function("registry overhead", |b| {
b.iter_batched(
|| (),
|_| Registry::<Key, ()>::new(),
|_| Registry::<Key, (), NotTracked<()>>::untracked(),
BatchSize::NumIterations(1),
)
});
Expand Down
8 changes: 4 additions & 4 deletions metrics-util/src/debugging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::hash::Hash;
use std::sync::{Arc, Mutex};
use std::{collections::HashMap, fmt::Debug};

use crate::{handle::Handle, kind::MetricKind, registry::Registry, CompositeKey};
use crate::{handle::Handle, kind::MetricKind, registry::Registry, CompositeKey, NotTracked};

use indexmap::IndexMap;
use metrics::{GaugeValue, Key, Recorder, Unit};
Expand All @@ -25,7 +25,7 @@ pub enum DebugValue {

/// Captures point-in-time snapshots of `DebuggingRecorder`.
pub struct Snapshotter {
registry: Arc<Registry<Key, Handle>>,
registry: Arc<Registry<Key, Handle, NotTracked<Handle>>>,
metrics: Option<Arc<Mutex<IndexMap<CompositeKey, ()>>>>,
units: UnitMap,
descs: DescriptionMap,
Expand Down Expand Up @@ -99,7 +99,7 @@ impl Snapshotter {
/// Callers can easily take snapshots of the metrics at any given time and get access
/// to the raw values.
pub struct DebuggingRecorder {
registry: Arc<Registry<Key, Handle>>,
registry: Arc<Registry<Key, Handle, NotTracked<Handle>>>,
metrics: Option<Arc<Mutex<IndexMap<CompositeKey, ()>>>>,
units: Arc<Mutex<HashMap<CompositeKey, Unit>>>,
descs: Arc<Mutex<HashMap<CompositeKey, &'static str>>>,
Expand All @@ -124,7 +124,7 @@ impl DebuggingRecorder {
};

DebuggingRecorder {
registry: Arc::new(Registry::new()),
registry: Arc::new(Registry::<Key, Handle, NotTracked<Handle>>::untracked()),
metrics,
units: Arc::new(Mutex::new(HashMap::new())),
descs: Arc::new(Mutex::new(HashMap::new())),
Expand Down
2 changes: 1 addition & 1 deletion metrics-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use quantile::{parse_quantiles, Quantile};
#[cfg(feature = "std")]
mod registry;
#[cfg(feature = "std")]
pub use registry::{Generation, Registry};
pub use registry::{Generation, Generational, NotTracked, Registry, Tracked};

mod common;
pub use common::*;
Expand Down
7 changes: 4 additions & 3 deletions metrics-util/src/recency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Debug;
use std::time::Duration;
use std::{collections::HashMap, ops::DerefMut};

use crate::{kind::MetricKindMask, Generation, Hashable, MetricKind, Registry};
use crate::{kind::MetricKindMask, Generation, Generational, Hashable, MetricKind, Registry};

use parking_lot::Mutex;
use quanta::{Clock, Instant};
Expand Down Expand Up @@ -64,16 +64,17 @@ impl<K> Recency<K> {
/// If the generation does not match, this indicates that the key was updated between querying
/// it from the registry and calling this method, and this method will return `true` in those
/// cases, and `false` for all remaining cases.
pub fn should_store<H>(
pub fn should_store<H, G>(
&self,
kind: MetricKind,
key: &K,
gen: Generation,
registry: &Registry<K, H>,
registry: &Registry<K, H, G>,
) -> bool
where
K: Debug + Eq + Hashable + Clone + 'static,
H: Debug + Clone + 'static,
G: Generational<H>,
{
if let Some(idle_timeout) = self.idle_timeout {
if self.mask.matches(kind) {
Expand Down
Loading

0 comments on commit 5dbe21f

Please sign in to comment.