Struct dipstick::Proxy

source ·
pub struct Proxy { /* private fields */ }
Expand description

A dynamic proxy for app and lib metrics. Decouples metrics definition from backend configuration. Allows defining metrics before a concrete type is configured. Allows replacing metrics backend on the fly at runtime.

Implementations§

source§

impl Proxy

source

pub fn new() -> Self

Create a new “private” metric proxy root. This is usually not what you want. Since this proxy will not be part of the standard proxy tree, it will need to be configured independently and since downstream code may not know about its existence this may never happen and metrics will not be proxyed anywhere. If you want to use the standard proxy tree, use #metric_proxy() instead.

source

pub fn set_target<T: InputScope + Send + Sync + 'static>(&self, target: T)

👎Deprecated since 0.7.2: Use target()

Replace target for this proxy and its children.

source

pub fn target<T: InputScope + Send + Sync + 'static>(&self, target: T)

Replace target for this proxy and its children.

Examples found in repository?
examples/bench_bucket_proxy.rs (line 15)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
fn main() {
    let event = Proxy::default().marker("a");

    let bucket = AtomicBucket::new();

    Proxy::default().target(bucket.clone());

    let args = &mut args();
    args.next();
    let tc: u8 = u8::from_str(&args.next().unwrap()).unwrap();
    for _ in 0..tc {
        let event = event.clone();
        thread::spawn(move || {
            loop {
                // report some metric values from our "application" loop
                event.mark();
            }
        });
    }
    sleep(Duration::from_secs(5));
    bucket
        .flush_to(&Stream::write_to_stdout().metrics())
        .unwrap();
}
More examples
Hide additional examples
examples/clopwizard.rs (line 31)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fn main() {
    let one_minute = AtomicBucket::new();
    one_minute.flush_every(Duration::from_secs(60));

    let five_minutes = AtomicBucket::new();
    five_minutes.flush_every(Duration::from_secs(300));

    let fifteen_minutes = AtomicBucket::new();
    fifteen_minutes.flush_every(Duration::from_secs(900));

    let all_buckets = MultiInputScope::new()
        .add_target(one_minute)
        .add_target(five_minutes)
        .add_target(fifteen_minutes)
        .named("machine_name");

    // send application metrics to aggregator
    Proxy::default().target(all_buckets);
    AtomicBucket::default_drain(Stream::write_to_stdout());
    AtomicBucket::default_stats(stats_all);

    loop {
        COUNTER.count(17);
        sleep(Duration::from_secs(3));
    }
}
examples/proxy.rs (line 17)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    let root_proxy = Proxy::default();
    let sub = root_proxy.named("sub");

    let count1 = root_proxy.counter("counter_a");

    let count2 = sub.counter("counter_b");

    loop {
        let stdout = Stream::write_to_stdout().metrics();
        root_proxy.target(stdout.clone());
        count1.count(1);
        count2.count(2);

        // route every metric from the root to stdout with prefix "root"
        root_proxy.target(stdout.named("root"));
        count1.count(3);
        count2.count(4);

        // route metrics from "sub" to stdout with prefix "mutant"
        sub.target(stdout.named("mutant"));
        count1.count(5);
        count2.count(6);

        // clear root metrics route, "sub" still appears
        root_proxy.unset_target();
        count1.count(7);
        count2.count(8);

        // now no metrics appear
        sub.unset_target();
        count1.count(9);
        count2.count(10);

        // go back to initial single un-prefixed route
        root_proxy.target(stdout.clone());
        count1.count(11);
        count2.count(12);

        sleep(Duration::from_secs(1));

        println!()
    }
}
examples/proxy_multi.rs (line 43)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
    // Placeholder to collect output targets
    // This will prefix all metrics with "my_stats"
    // before flushing them.
    let mut targets = MultiInput::new().named("my_stats");

    // Skip the metrics here... we just use this for the output
    // Follow the same pattern for Statsd, Graphite, etc.
    let prometheus = Prometheus::push_to("http://localhost:9091/metrics/job/dipstick_example")
        .expect("Prometheus Socket");
    targets = targets.add_target(prometheus);

    // Add stdout
    targets = targets.add_target(Stream::write_to_stdout());

    // Create the stats and drain targets
    let bucket = AtomicBucket::new();
    bucket.drain(targets);
    // Crucial, set the flush interval, otherwise risk hammering targets
    bucket.flush_every(Duration::from_secs(3));

    // Now wire up the proxy target with the stats and you're all set
    let proxy = Proxy::default();
    proxy.target(bucket.clone());

    // Example using the macro! Proxy sugar
    PROXY.target(bucket.named("global"));

    loop {
        // Using the default proxy
        proxy.counter("beans").count(100);
        proxy.timer("braincells").interval_us(420);
        // global example
        PROXY.counter("my_proxy_counter").count(123);
        PROXY.timer("my_proxy_timer").interval_us(2000000);
        std::thread::sleep(Duration::from_millis(100));
    }
}
source

pub fn unset_target(&self)

Replace target for this proxy and its children.

Examples found in repository?
examples/proxy.rs (line 32)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() {
    let root_proxy = Proxy::default();
    let sub = root_proxy.named("sub");

    let count1 = root_proxy.counter("counter_a");

    let count2 = sub.counter("counter_b");

    loop {
        let stdout = Stream::write_to_stdout().metrics();
        root_proxy.target(stdout.clone());
        count1.count(1);
        count2.count(2);

        // route every metric from the root to stdout with prefix "root"
        root_proxy.target(stdout.named("root"));
        count1.count(3);
        count2.count(4);

        // route metrics from "sub" to stdout with prefix "mutant"
        sub.target(stdout.named("mutant"));
        count1.count(5);
        count2.count(6);

        // clear root metrics route, "sub" still appears
        root_proxy.unset_target();
        count1.count(7);
        count2.count(8);

        // now no metrics appear
        sub.unset_target();
        count1.count(9);
        count2.count(10);

        // go back to initial single un-prefixed route
        root_proxy.target(stdout.clone());
        count1.count(11);
        count2.count(12);

        sleep(Duration::from_secs(1));

        println!()
    }
}
source

pub fn set_default_target<T: InputScope + Send + Sync + 'static>(target: T)

👎Deprecated since 0.7.2: Use default_target()

Install a new default target for all proxies.

source

pub fn default_target<T: InputScope + Send + Sync + 'static>(target: T)

Install a new default target for all proxies.

Examples found in repository?
examples/macro_proxy.rs (line 36)
35
36
37
38
39
40
41
42
43
44
45
fn main() {
    dipstick::Proxy::default_target(Stream::write_to_stdout().metrics());

    loop {
        ROOT_COUNTER.count(123);
        ANOTHER_COUNTER.count(456);
        ROOT_TIMER.interval_us(2000000);
        ROOT_GAUGE.value(34534);
        std::thread::sleep(Duration::from_millis(40));
    }
}
source

pub fn unset_default_target(&self)

Revert to initial state any installed default target for all proxies.

Trait Implementations§

source§

impl Clone for Proxy

source§

fn clone(&self) -> Proxy

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Proxy

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Proxy

source§

fn default() -> Self

Return the default root metric proxy.

source§

impl Flush for Proxy

source§

fn flush(&self) -> Result<()>

Flush does nothing by default.
source§

impl<S: AsRef<str>> From<S> for Proxy

source§

fn from(name: S) -> Proxy

Converts to this type from the input type.
source§

impl InputScope for Proxy

source§

fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric

Lookup or create a proxy stub for the requested metric.

source§

fn counter(&self, name: &str) -> Counter

Define a Counter.
source§

fn marker(&self, name: &str) -> Marker

Define a Marker.
source§

fn timer(&self, name: &str) -> Timer

Define a Timer.
source§

fn gauge(&self, name: &str) -> Gauge

Define a Gauge.
source§

fn level(&self, name: &str) -> Level

Define a Level.
source§

impl WithAttributes for Proxy

source§

fn get_attributes(&self) -> &Attributes

Return attributes of component.
source§

fn mut_attributes(&mut self) -> &mut Attributes

Return attributes of component for mutation.
source§

fn with_attributes<F: Fn(&mut Attributes)>(&self, edit: F) -> Self

Clone the component and mutate its attributes at once.

Auto Trait Implementations§

§

impl Freeze for Proxy

§

impl !RefUnwindSafe for Proxy

§

impl Send for Proxy

§

impl Sync for Proxy

§

impl Unpin for Proxy

§

impl !UnwindSafe for Proxy

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Observe for T

§

type Inner = T

The inner type for the ObserveWhen. Read more
source§

fn observe<F>( &self, metric: impl Deref<Target = InputMetric>, operation: F, ) -> ObserveWhen<'_, T, F>
where F: Fn(Instant) -> isize + Send + Sync + 'static,

Provide a source for a metric’s values.
source§

impl<T> OnFlush for T
where T: Flush + WithAttributes,

source§

fn notify_flush_listeners(&self)

Notify registered listeners of an impending flush.
source§

impl<T> Prefixed for T
where T: WithAttributes,

source§

fn get_prefixes(&self) -> &NameParts

Returns namespace of component.

source§

fn add_prefix<S>(&self, name: S) -> T
where S: Into<String>,

👎Deprecated since 0.7.2: Use named() or add_name()

Append a name to the existing names. Return a clone of the component with the updated names.

source§

fn add_name<S>(&self, name: S) -> T
where S: Into<String>,

Append a name to the existing names. Return a clone of the component with the updated names.

source§

fn named<S>(&self, name: S) -> T
where S: Into<String>,

Replace any existing names with a single name. Return a clone of the component with the new name. If multiple names are required, add_name may also be used.

source§

fn prefix_append<S: Into<MetricName>>(&self, name: S) -> MetricName

Append any name parts to the name’s namespace.
source§

fn prefix_prepend<S: Into<MetricName>>(&self, name: S) -> MetricName

Prepend any name parts to the name’s namespace.
source§

impl<T> ScheduleFlush for T
where T: InputScope + Send + Sync + Clone + 'static,

source§

fn flush_every(&self, period: Duration) -> CancelHandle

Flush this scope at regular intervals.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.