Type Alias vise::LabeledFamily

source ·
pub type LabeledFamily<S, M, const N: usize = 1> = Family<S, M, [&'static str; N]>;
Expand description

Family with separately specified label names.

Separately specifying labels allows to not define newtype wrappers for labels. Instead, labels (the first type param of LabeledFamily) can be specified as values (e.g., &'static str or u8), and the label names are provided separately using the labels = [..] attribute with the Metrics derive macro.

  • If there’s a single label, its value type must be specified directly: &'static str.
  • If there are several labels, they must be specified as a tuple: (&'static str, u16).
  • The number of labels must match the number of label names and the constant param of LabeledFamily (which is set to 1 by default). E.g., for two labels you should use LabeledFamily<_, _, 2>.

§Examples

§Family with single label

use vise::{Counter, LabeledFamily, Metrics};

#[derive(Debug, Metrics)]
struct TestMetrics {
    #[metrics(labels = ["method"])]
    counters: LabeledFamily<&'static str, Counter>,
}

// `counters` are keyed by a `&str`:
let metrics = TestMetrics::default();
metrics.counters[&"test"].inc();
metrics.counters[&"another_test"].inc_by(3);
// In the encoded metrics, these entries will be mentioned as follows:
let entries = [
    r#"counters_total{method="test"} 1"#,
    r#"counters_total{method="another_test"} 3"#,
];

§Family with multiple labels

const LABELS: [&str; 2] = ["method", "code"];
type Labels = (&'static str, u16);

#[derive(Debug, Metrics)]
struct TestMetrics {
    #[metrics(labels = LABELS, buckets = Buckets::LATENCIES)]
    latencies: LabeledFamily<Labels, Histogram<Duration>, 2>,
    // ^ note that label names and type can be extracted elsewhere
}

let metrics = TestMetrics::default();
metrics.latencies[&("call", 200)].observe(Duration::from_millis(25));
metrics.latencies[&("send", 502)].observe(Duration::from_secs(1));
// In the encoded metrics, these entries will be mentioned as follows:
let entries = [
    r#"latencies_sum{method="call",code="200"} 0.025"#,
    r#"latencies_sum{method="send",code="502"} 1.0"#,
];

Aliased Type§

struct LabeledFamily<S, M, const N: usize = 1> { /* private fields */ }