Crate relay_statsd

source ·
Expand description

A high-level StatsD metric client built on cadence.

§Defining Metrics

In order to use metrics, one needs to first define one of the metric traits on a custom enum. The following types of metrics are available: counter, timer, gauge, histogram, and set. For explanations on what that means see Metric Types.

The metric traits serve only to provide a type safe metric name. All metric types have exactly the same form, they are different only to ensure that a metric can only be used for the type for which it was defined, (e.g. a counter metric cannot be used as a timer metric). See the traits for more detailed examples.

§Initializing the Client

Metrics can be used without initializing a statsd client. In that case, invoking with_client or the metric! macro will become a noop. Only when configured, metrics will actually be collected.

To initialize the client, either use set_client to pass a custom client, or use init to create a default client with known arguments:

relay_statsd::init("myprefix", "localhost:8125", BTreeMap::new(), 1.0);

§Macro Usage

The recommended way to record metrics is by using the metric! macro. See the trait docs for more information on how to record each type of metric.

use relay_statsd::{metric, CounterMetric};

struct MyCounter;

impl CounterMetric for MyCounter {
    fn name(&self) -> &'static str {

metric!(counter(MyCounter) += 1);

§Manual Usage

use relay_statsd::prelude::*;

relay_statsd::with_client(|client| {
    client.count("mymetric", 1).ok();


  • The metrics prelude that is necessary to use the client.