Skip to content

Commit

Permalink
fralalonde#42 Improve API, remove Arc that wraps the closure.
Browse files Browse the repository at this point in the history
  • Loading branch information
mixalturek committed Jan 25, 2019
1 parent c0d0765 commit 042139a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
5 changes: 2 additions & 3 deletions examples/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ extern crate dipstick;

use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::sync::Arc;
use std::time::{Duration, Instant};

use dipstick::{AtomicBucket, InputScope, MetricValue, Prefixed, ScheduleFlush, Stream};
Expand All @@ -34,8 +33,8 @@ fn main() {
metrics.set_drain(Stream::to_stderr());
let flush_handle = metrics.flush_every(Duration::from_secs(1));

metrics.observe("uptime", Arc::new(move || dur2ms(start_time.elapsed())));
metrics.observe("threads", Arc::new(threads));
metrics.observe("uptime", move || dur2ms(start_time.elapsed()));
metrics.observe("threads", threads);

println!("Press Enter key to exit");
io::stdin().read_line(&mut String::new()).expect("Example, ignored");
Expand Down
2 changes: 1 addition & 1 deletion src/bucket/atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ impl InputScope for AtomicBucket {
InputMetric::new(move |value, _labels| scores.update(value))
}

fn observe(&self, name: &str, callback: GaugeCallback) {
fn observe_helper(&self, name: &str, callback: GaugeCallback) {
let gauge = self.gauge(name);

self.inner
Expand Down
10 changes: 8 additions & 2 deletions src/core/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ pub trait InputScope: Flush {

/// Observe a gauge value using a callback function. If multiple callbacks are registered under
/// the same conflicting key, only the last one will survive.
fn observe(&self, _name: &str, _callback: GaugeCallback) {
fn observe<F: Fn() -> MetricValue + Send + Sync + 'static>(&self, name: &str, callback: F) where Self: Sized {
self.observe_helper(name, Arc::new(callback));
}

/// Helper method to make use of `observe()` more pleasant. The Arc wrapper is not necessary
/// in the client code now. Consider this as an internal method.
fn observe_helper(&self, _name: &str, _callback: GaugeCallback) {
// TODO: Not yet finished, remove default impl.
}
}
Expand Down Expand Up @@ -190,7 +196,7 @@ impl Gauge {
}

/// Callback function for gauge observer.
pub type GaugeCallback = Arc<Fn() -> MetricValue + Send + Sync>;
pub(crate) type GaugeCallback = Arc<Fn() -> MetricValue + Send + Sync + 'static>;

/// Gauge and it's observer callback.
#[derive(Clone)]
Expand Down

0 comments on commit 042139a

Please sign in to comment.