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>
impl<W: Write + Send + Sync + 'static> Stream<W>
sourcepub fn write_to(write: W) -> Stream<W>
pub fn write_to(write: W) -> Stream<W>
Write metric values to provided Write target.
Examples found in repository?
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
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>
impl Stream<File>
sourcepub fn to_file<P: AsRef<Path>>(file: P) -> Result<Stream<File>>
👎Deprecated since 0.8.0: Use write_to_file()
pub fn to_file<P: AsRef<Path>>(file: P) -> Result<Stream<File>>
Write metric values to a file.
sourcepub fn write_to_file<P: AsRef<Path>>(file: P) -> Result<Stream<File>>
pub fn write_to_file<P: AsRef<Path>>(file: P) -> Result<Stream<File>>
Write metric values to a file.
sourcepub fn to_new_file<P: AsRef<Path>>(
file: P,
clobber: bool,
) -> Result<Stream<File>>
👎Deprecated since 0.8.0: Use write_to_new_file()
pub fn 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>
impl Stream<Stderr>
sourcepub fn to_stderr() -> Stream<Stderr>
👎Deprecated since 0.8.0: Use write_to_stderr()
pub fn to_stderr() -> Stream<Stderr>
Write metric values to stderr.
sourcepub fn write_to_stderr() -> Stream<Stderr>
pub fn write_to_stderr() -> Stream<Stderr>
Write metric values to stderr.
Examples found in repository?
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
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));
}
}
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>
impl Stream<Stdout>
sourcepub fn to_stdout() -> Stream<Stdout>
👎Deprecated since 0.8.0: Use write_to_stdout()
pub fn to_stdout() -> Stream<Stdout>
Write metric values to stdout.
sourcepub fn write_to_stdout() -> Stream<Stdout>
pub fn write_to_stdout() -> Stream<Stdout>
Write metric values to stdout.
Examples found in repository?
More examples
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: ");
}
}
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));
}
}
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));
}
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();
}
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>
impl<W: Write + Send + Sync + 'static> Buffered for Stream<W>
source§fn buffered(&self, buffering: Buffering) -> Self
fn buffered(&self, buffering: Buffering) -> Self
source§fn get_buffering(&self) -> Buffering
fn get_buffering(&self) -> Buffering
source§fn is_buffered(&self) -> bool
fn is_buffered(&self) -> bool
Buffering::Unbuffered
.
Returns true otherwise.source§impl<W: Write + Send + Sync + 'static> CachedInput for Stream<W>
impl<W: Write + Send + Sync + 'static> CachedInput for Stream<W>
source§fn cached(self, max_size: usize) -> InputCache
fn cached(self, max_size: usize) -> InputCache
source§impl<W: Write + Send + Sync + 'static> Formatting for Stream<W>
impl<W: Write + Send + Sync + 'static> Formatting for Stream<W>
source§fn formatting(&self, format: impl LineFormat + 'static) -> Self
fn formatting(&self, format: impl LineFormat + 'static) -> Self
source§impl<W: Write + Send + Sync + 'static> Input for Stream<W>
impl<W: Write + Send + Sync + 'static> Input for Stream<W>
source§impl<W: Write + Send + Sync + 'static> QueuedInput for Stream<W>
impl<W: Write + Send + Sync + 'static> QueuedInput for Stream<W>
source§fn queued(self, max_size: usize) -> InputQueue
fn queued(self, max_size: usize) -> InputQueue
source§impl<W: Write + Send + Sync + 'static> WithAttributes for Stream<W>
impl<W: Write + Send + Sync + 'static> WithAttributes for Stream<W>
source§fn get_attributes(&self) -> &Attributes
fn get_attributes(&self) -> &Attributes
source§fn mut_attributes(&mut self) -> &mut Attributes
fn mut_attributes(&mut self) -> &mut Attributes
source§fn with_attributes<F: Fn(&mut Attributes)>(&self, edit: F) -> Self
fn with_attributes<F: Fn(&mut Attributes)>(&self, edit: F) -> Self
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Prefixed for Twhere
T: WithAttributes,
impl<T> Prefixed for Twhere
T: WithAttributes,
source§fn get_prefixes(&self) -> &NameParts
fn get_prefixes(&self) -> &NameParts
Returns namespace of component.
source§fn add_prefix<S>(&self, name: S) -> T
👎Deprecated since 0.7.2: Use named() or add_name()
fn add_prefix<S>(&self, name: S) -> T
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
fn add_name<S>(&self, name: S) -> T
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
fn named<S>(&self, name: S) -> T
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.