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
impl Proxy
sourcepub fn new() -> Self
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.
sourcepub fn set_target<T: InputScope + Send + Sync + 'static>(&self, target: T)
👎Deprecated since 0.7.2: Use target()
pub fn set_target<T: InputScope + Send + Sync + 'static>(&self, target: T)
Replace target for this proxy and its children.
sourcepub fn target<T: InputScope + Send + Sync + 'static>(&self, target: T)
pub fn target<T: InputScope + Send + Sync + 'static>(&self, target: T)
Replace target for this proxy and its children.
Examples found in repository?
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
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));
}
}
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!()
}
}
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));
}
}
sourcepub fn unset_target(&self)
pub fn unset_target(&self)
Replace target for this proxy and its children.
Examples found in repository?
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!()
}
}
sourcepub fn set_default_target<T: InputScope + Send + Sync + 'static>(target: T)
👎Deprecated since 0.7.2: Use default_target()
pub fn set_default_target<T: InputScope + Send + Sync + 'static>(target: T)
Install a new default target for all proxies.
sourcepub fn default_target<T: InputScope + Send + Sync + 'static>(target: T)
pub fn default_target<T: InputScope + Send + Sync + 'static>(target: T)
Install a new default target for all proxies.
sourcepub fn unset_default_target(&self)
pub fn unset_default_target(&self)
Revert to initial state any installed default target for all proxies.
Trait Implementations§
source§impl InputScope for Proxy
impl InputScope for Proxy
source§fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric
fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric
Lookup or create a proxy stub for the requested metric.
source§impl WithAttributes for Proxy
impl WithAttributes for Proxy
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 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> 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> Observe for Twhere
T: InputScope + WithAttributes,
impl<T> Observe for Twhere
T: InputScope + WithAttributes,
§type Inner = T
type Inner = T
ObserveWhen
. Read moresource§impl<T> OnFlush for Twhere
T: Flush + WithAttributes,
impl<T> OnFlush for Twhere
T: Flush + WithAttributes,
source§fn notify_flush_listeners(&self)
fn notify_flush_listeners(&self)
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.
source§fn prefix_append<S: Into<MetricName>>(&self, name: S) -> MetricName
fn prefix_append<S: Into<MetricName>>(&self, name: S) -> MetricName
source§fn prefix_prepend<S: Into<MetricName>>(&self, name: S) -> MetricName
fn prefix_prepend<S: Into<MetricName>>(&self, name: S) -> MetricName
source§impl<T> ScheduleFlush for T
impl<T> ScheduleFlush for T
source§fn flush_every(&self, period: Duration) -> CancelHandle
fn flush_every(&self, period: Duration) -> CancelHandle
Flush this scope at regular intervals.