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§

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.

👎Deprecated since 0.7.2: Use target()

Replace target for this proxy and its children.

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));
    }
}

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!()
    }
}
👎Deprecated since 0.7.2: Use default_target()

Install a new default target for all proxies.

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));
    }
}

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

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Return the default root metric proxy.

Flush does nothing by default.
Converts to this type from the input type.

Lookup or create a proxy stub for the requested metric.

Define a Counter.
Define a Marker.
Define a Timer.
Define a Gauge.
Define a Level.
Return attributes of component.
Return attributes of component for mutation.
Clone the component and mutate its attributes at once.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.