pub trait MetricBackend: Sealed {
    fn send_metric<M>(&self, metric: &M) -> MetricResult<()>
    where
        M: Metric
; fn consume_error(&self, err: MetricError); }
Expand description

Typically internal client methods for sending metrics and handling errors.

This trait exposes methods of the client that would normally be internal but may be useful for consumers of the library to extend it in unforseen ways. Most consumers of the library shouldn’t need to make use of this extension point.

This trait is not exposed in the prelude module since it isn’t required to use the client for sending metrics. It is only exposed in the ext module which is used to encompass advanced extension points for the library.

NOTE: This is a sealed trait and so it cannot be implemented outside of the library.

Example

use cadence::{Metric, MetricResult, StatsdClient, NopMetricSink};
use cadence::ext::MetricBackend;

struct CustomMetric {
    repr: String,
}

impl Metric for CustomMetric {
    fn as_metric_str(&self) -> &str {
        &self.repr
    }
}

impl From<String> for CustomMetric {
    fn from(v: String) -> Self {
        CustomMetric { repr: v }
    }
}

struct MyCustomClient {
    prefix: String,
    wrapped: StatsdClient,
}

impl MyCustomClient {
    fn new(prefix: &str, client: StatsdClient) -> Self {
        MyCustomClient {
            prefix: prefix.to_string(),
            wrapped: client,
        }
    }

    fn send_event(&self, key: &str, val: i64) -> MetricResult<CustomMetric> {
        let metric = CustomMetric::from(format!("{}.{}:{}|e", self.prefix, key, val));
        self.wrapped.send_metric(&metric)?;
        Ok(metric)
    }

    fn send_event_quietly(&self, key: &str, val: i64) {
        if let Err(e) = self.send_event(key, val) {
            self.wrapped.consume_error(e);
        }
    }
}

let prefix = "some.prefix";
let inner = StatsdClient::from_sink(&prefix, NopMetricSink);
let custom = MyCustomClient::new(&prefix, inner);

custom.send_event("some.event", 123).unwrap();
custom.send_event_quietly("some.event", 456);

Required Methods

Send a full formed Metric implementation via the underlying MetricSink

Obtain a &str representation of a metric, encode it as UTF-8 bytes, and send it to the underlying MetricSink, verbatim. Note that the metric is expected to be full formed already, including any prefix or tags.

Note that if you simply want to emit standard metrics, you don’t need to use this method. This is only useful if you are extending Cadence with a custom metric type or something similar.

Consume a possible error from attempting to send a metric.

When callers have elected to quietly send metrics via the MetricBuilder::send() method, this method will be invoked if an error is encountered. By default the handler is a no-op, meaning that errors are discarded.

Note that if you simply want to emit standard metrics, you don’t need to use this method. This is only useful if you are extending Cadence with a custom metric type or something similar.

Implementors