Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
some more tinkering with metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Nov 8, 2023
1 parent 61722b3 commit 6fa6373
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 19 deletions.
11 changes: 11 additions & 0 deletions core/src/extent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,14 @@ impl ToExtent for Range<Timestamp> {
Extent::new(self.clone())
}
}

impl ToExtent for Range<Option<Timestamp>> {
fn to_extent(&self) -> Extent {
match (self.start, self.end) {
(Some(start), Some(end)) => (start..end).to_extent(),
(Some(start), None) => start.to_extent(),
(None, Some(end)) => end.to_extent(),
(None, None) => None::<Timestamp>.to_extent(),
}
}
}
32 changes: 25 additions & 7 deletions core/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ impl<'m, T> Metric<'m, T> {
Metric { name, kind, value }
}

pub const fn counter(name: Key<'m>, value: T) -> Self {
Metric::new(name, MetricKind::Counter, value)
}

pub const fn name(&self) -> &Key<'m> {
&self.name
}
Expand All @@ -35,11 +31,33 @@ impl<'m, T> Metric<'m, T> {
&self.value
}

pub fn read<'a, U: ToValue>(&'a self, read: impl FnOnce(&'a T) -> U) -> Metric<'a, U> {
pub fn value_mut(&mut self) -> &mut T {
&mut self.value
}

pub fn sample<'a, U: ToValue>(
&'a self,
sample: impl FnOnce(MetricKind, &'a T) -> (MetricKind, U),
) -> Metric<'a, U> {
let (kind, value) = sample(self.kind, &self.value);

Metric {
name: self.name.by_ref(),
kind,
value,
}
}

pub fn sample_mut<'a, U: ToValue>(
&'a mut self,
sample: impl FnOnce(MetricKind, &'a mut T) -> (MetricKind, U),
) -> Metric<'a, U> {
let (kind, value) = sample(self.kind, &mut self.value);

Metric {
name: self.name.by_ref(),
kind: self.kind,
value: read(&self.value),
kind,
value,
}
}
}
Expand Down
55 changes: 43 additions & 12 deletions tests/smoke-test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,48 @@ use emit::Props;
#[macro_use]
extern crate serde_derive;

static COUNT: emit::Metric<'static, AtomicUsize> =
emit::Metric::counter(emit::Key::new("smoke_test::count"), AtomicUsize::new(0));
struct DeltaCount {
last: std::sync::Mutex<(Option<emit::timestamp::Timestamp>, usize)>,
value: AtomicUsize,
}

fn increment(metric: &emit::Metric<AtomicUsize>) {
metric.value().fetch_add(1, Ordering::Relaxed);
static COUNT: emit::Metric<'static, DeltaCount> = emit::Metric::new(
emit::Key::new("smoke_test::count"),
emit::metrics::MetricKind::Counter,
DeltaCount {
last: std::sync::Mutex::new((None, 0)),
value: AtomicUsize::new(0),
},
);

fn increment(metric: &emit::Metric<DeltaCount>) {
metric.value().value.fetch_add(1, Ordering::Relaxed);
}

fn flush_metrics<'a>(metrics: impl IntoIterator<Item = &'a emit::Metric<'a, AtomicUsize>> + 'a) {
fn flush_metrics<'a>(metrics: impl IntoIterator<Item = &'a emit::Metric<'a, DeltaCount>> + 'a) {
let now = emit::now();

for metric in metrics {
let mut start = None;
let delta = metric.sample(|_, value| {
let mut previous = value.last.lock().unwrap();
let current = value.value.load(Ordering::Relaxed);

start = previous.0;
let delta = current.saturating_sub(previous.1);

previous.0 = now;
previous.1 = current;

(emit::metrics::MetricKind::Counter, delta)
});

emit::emit(&emit::Event::new(
emit::now(),
start..now,
emit::tpl!("{metric_name} read {metric_value} with {ordering}"),
metric
.read(|value| value.load(Ordering::Relaxed))
.chain(emit::props! {
ordering: "relaxed"
}),
delta.chain(emit::props! {
ordering: "relaxed"
}),
));
}
}
Expand All @@ -53,7 +78,13 @@ async fn main() {
.and_to(emit_term::stdout())
.init();

for i in 0..10 {
for i in 0..9 {
let _ = in_ctxt(i).await;
}

flush_metrics([&COUNT]);

for i in 0..7 {
let _ = in_ctxt(i).await;
}

Expand Down

0 comments on commit 6fa6373

Please sign in to comment.