Struct dipstick::Stream

source ·
pub struct Stream<W: Write + Send + Sync + 'static> { /* private fields */ }
Expand description

Buffered metrics text Input.

Implementations§

source§

impl<W: Write + Send + Sync + 'static> Stream<W>

source

pub fn write_to(write: W) -> Stream<W>

Write metric values to provided Write target.

Examples found in repository?
examples/cache.rs (line 9)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
    let metrics = Stream::write_to(io::stdout())
        .cached(5)
        .metrics()
        .named("cache");

    loop {
        // report some ad-hoc metric values from our "application" loop
        metrics.counter("blorf").count(1134);
        metrics.marker("burg").mark();

        sleep(Duration::from_millis(500));
    }
}
More examples
Hide additional examples
examples/basics.rs (line 11)
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() {
    // for this demo, print metric values to the console
    let app_metrics = Stream::write_to(io::stdout()).metrics();

    // metrics can be predefined by type and name
    let counter = app_metrics.counter("counter_a");
    let level = app_metrics.level("level_a");
    let timer = app_metrics.timer("timer_b");

    // metrics can also be declared and used ad-hoc (use output.cache() if this happens often)
    app_metrics.counter("just_once").count(4);

    // metric names can be prepended with a common prefix
    let prefixed_metrics = app_metrics.named("subsystem");
    let event = prefixed_metrics.marker("event_c");
    let gauge = prefixed_metrics.gauge("gauge_d");

    // each kind of metric has a different method name to prevent errors
    counter.count(11);
    level.adjust(-4);
    level.adjust(5);

    gauge.value(22);
    gauge.value(-24);

    event.mark();

    // time can be measured multiple equivalent ways:
    // in microseconds (used internally)
    timer.interval_us(35573);

    // using the time! macro
    time!(timer, sleep(Duration::from_millis(5)));

    // using a closure
    timer.time(|| sleep(Duration::from_millis(5)));

    // using a "time handle"
    let start_time = timer.start();
    sleep(Duration::from_millis(5));
    timer.stop(start_time);
}
source§

impl Stream<File>

source

pub fn to_file<P: AsRef<Path>>(file: P) -> Result<Stream<File>>

👎Deprecated since 0.8.0: Use write_to_file()

Write metric values to a file.

source

pub fn write_to_file<P: AsRef<Path>>(file: P) -> Result<Stream<File>>

Write metric values to a file.

source

pub fn to_new_file<P: AsRef<Path>>( file: P, clobber: bool, ) -> Result<Stream<File>>

👎Deprecated since 0.8.0: Use write_to_new_file()

Write metrics to a new file.

Creates a new file to dump data into. If clobber is set to true, it allows overwriting existing file, if false, the attempt will result in an error.

source

pub fn write_to_new_file<P: AsRef<Path>>( file: P, clobber: bool, ) -> Result<Stream<File>>

Write metrics to a new file.

Creates a new file to dump data into. If clobber is set to true, it allows overwriting existing file, if false, the attempt will result in an error.

source§

impl Stream<Stderr>

source

pub fn to_stderr() -> Stream<Stderr>

👎Deprecated since 0.8.0: Use write_to_stderr()

Write metric values to stderr.

source

pub fn write_to_stderr() -> Stream<Stderr>

Write metric values to stderr.

Examples found in repository?
examples/text_format_label.rs (line 33)
32
33
34
35
36
37
38
39
40
41
42
43
fn main() {
    let counter = Stream::write_to_stderr()
        .formatting(MyFormat)
        .metrics()
        .counter("counter_a");
    AppLabel::set("abc", "xyz");
    loop {
        // report some metric values from our "application" loop
        counter.count(11);
        sleep(Duration::from_millis(500));
    }
}
More examples
Hide additional examples
examples/multi_input.rs (line 15)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
fn main() {
    // will output metrics to graphite and to stdout
    let different_type_metrics = MultiInput::new()
        .add_target(Graphite::send_to("localhost:2003").expect("Connecting"))
        .add_target(Stream::write_to_stdout())
        .metrics();

    // will output metrics twice, once with "both.yeah" prefix and once with "both.ouch" prefix.
    let same_type_metrics = MultiInput::new()
        .add_target(Stream::write_to_stderr().named("yeah"))
        .add_target(Stream::write_to_stderr().named("ouch"))
        .named("both")
        .metrics();

    loop {
        different_type_metrics.counter("counter_a").count(123);
        same_type_metrics.timer("timer_a").interval_us(2000000);
        std::thread::sleep(Duration::from_millis(400));
    }
}
examples/custom_publish.rs (line 43)
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
51
52
53
54
55
56
57
58
59
fn main() {
    fn custom_statistics(
        kind: InputKind,
        mut name: MetricName,
        score: ScoreType,
    ) -> Option<(InputKind, MetricName, MetricValue)> {
        match (kind, score) {
            // do not export gauge scores
            (InputKind::Gauge, _) => None,

            // prepend and append to metric name
            (_, ScoreType::Count(count)) => {
                if let Some(last) = name.pop_back() {
                    Some((
                        InputKind::Counter,
                        name.append("customized_add_prefix")
                            .append(format!("{}_and_a_suffix", last)),
                        count,
                    ))
                } else {
                    None
                }
            }

            // scaling the score value and appending unit to name
            (kind, ScoreType::Sum(sum)) => Some((kind, name.append("per_thousand"), sum / 1000)),

            // using the unmodified metric name
            (kind, ScoreType::Mean(avg)) => Some((kind, name, avg.round() as MetricValue)),

            // do not export min and max
            _ => None,
        }
    }

    // send application metrics to aggregator
    AtomicBucket::default_drain(Stream::write_to_stderr());
    AtomicBucket::default_stats(custom_statistics);

    let app_metrics = AtomicBucket::new();

    // schedule aggregated metrics to be printed every 3 seconds
    app_metrics.flush_every(Duration::from_secs(3));

    let counter = app_metrics.counter("counter_a");
    let timer = app_metrics.timer("timer_b");
    let gauge = app_metrics.gauge("gauge_c");
    loop {
        counter.count(11);
        timer.interval_us(654654);
        gauge.value(3534);
    }
}
source§

impl Stream<Stdout>

source

pub fn to_stdout() -> Stream<Stdout>

👎Deprecated since 0.8.0: Use write_to_stdout()

Write metric values to stdout.

source

pub fn write_to_stdout() -> Stream<Stdout>

Write metric values to stdout.

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));
    }
}
More examples
Hide additional examples
examples/buffered_flush_on_drop.rs (line 9)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let input = Stream::write_to_stdout().buffered(Buffering::Unlimited);

    loop {
        println!("\n------- open scope");

        let metrics = input.metrics();

        metrics.marker("marker_a").mark();

        sleep(Duration::from_millis(1000));

        println!("------- close scope: ");
    }
}
examples/bucket_cleanup.rs (line 10)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    let bucket = AtomicBucket::new();
    AtomicBucket::default_drain(Stream::write_to_stdout());

    let persistent_marker = bucket.marker("persistent");

    let mut i = 0;

    loop {
        i += 1;
        let transient_marker = bucket.marker(&format!("marker_{}", i));

        transient_marker.mark();
        persistent_marker.mark();

        bucket.flush().unwrap();

        sleep(Duration::from_secs(1));
    }
}
examples/async_queue.rs (line 9)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let async_metrics = Stream::write_to_stdout().queued(100).metrics();
    let counter = async_metrics.counter("counter_a");
    for _ in 0..4 {
        let counter = counter.clone();
        thread::spawn(move || {
            loop {
                // report some metric values from our "application" loop
                counter.count(11);
                sleep(Duration::from_millis(50));
            }
        });
    }
    sleep(Duration::from_secs(5000));
}
examples/bench_bucket.rs (line 28)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    let bucket = AtomicBucket::new();
    let event = bucket.marker("a");
    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.stats(stats_all);
    bucket
        .flush_to(&Stream::write_to_stdout().metrics())
        .unwrap();
}
examples/bench_bucket_proxy.rs (line 31)
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();
}

Trait Implementations§

source§

impl<W: Write + Send + Sync + 'static> Buffered for Stream<W>

source§

fn buffered(&self, buffering: Buffering) -> Self

Return a clone with the specified buffering set.
source§

fn get_buffering(&self) -> Buffering

Return the current buffering strategy.
source§

fn is_buffered(&self) -> bool

Returns false if the current buffering strategy is Buffering::Unbuffered. Returns true otherwise.
source§

impl<W: Write + Send + Sync + 'static> CachedInput for Stream<W>

source§

fn cached(self, max_size: usize) -> InputCache

Wrap an input with a metric definition cache. This can provide performance benefits for metrics that are dynamically defined at runtime on each access. Caching is useless if all metrics are statically declared or instantiated programmatically in advance and referenced by a long living variable.
source§

impl<W: Write + Send + Sync + 'static> Clone for Stream<W>

source§

fn clone(&self) -> Self

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<W: Write + Send + Sync + 'static> Formatting for Stream<W>

source§

fn formatting(&self, format: impl LineFormat + 'static) -> Self

Specify formatting of output.
source§

impl<W: Write + Send + Sync + 'static> Input for Stream<W>

§

type SCOPE = TextScope<W>

The type of Scope returned byt this input.
source§

fn metrics(&self) -> Self::SCOPE

Open a new scope from this input.
source§

fn input(&self) -> Self::SCOPE

👎Deprecated since 0.7.2: Use metrics()
Open a new scope from this input.
source§

fn new_scope(&self) -> Self::SCOPE

👎Deprecated since 0.8.0: Use metrics()
Open a new scope from this input.
source§

impl<W: Write + Send + Sync + 'static> QueuedInput for Stream<W>

source§

fn queued(self, max_size: usize) -> InputQueue

Wrap this output with an asynchronous dispatch queue of specified length.
source§

impl<W: Write + Send + Sync + 'static> WithAttributes for Stream<W>

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<W> Freeze for Stream<W>

§

impl<W> !RefUnwindSafe for Stream<W>

§

impl<W> Send for Stream<W>

§

impl<W> Sync for Stream<W>

§

impl<W> Unpin for Stream<W>

§

impl<W> !UnwindSafe for Stream<W>

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> InputDyn for T
where T: Input + Send + Sync + 'static,

source§

fn input_dyn(&self) -> Arc<dyn InputScope + Send + Sync>

Open a new scope from this output.
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> 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> 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.