Skip to main content

static_metrics

Attribute Macro static_metrics 

Source
#[static_metrics]
Expand description

Defines a container struct of statically defined metrics.

Applied to a struct whose fields are metric handles, this generates the registration, accessors, and metadata for the whole group. It is re-exported (and intended to be used) as saluki_metrics::static_metrics.

Because it can rewrite field storage and add hidden fields (see “Mapped metrics” and “Derived gauges” below), it must appear before any #[derive(...)] on the struct.

§Fields

Every field must be typed as Counter, Gauge, or Histogram (from the metrics crate, re-exported by saluki-metrics). Each field becomes one registered metric whose name is "<prefix>_<field_name>". A derived histogram also registers one gauge for each derived value.

§Arguments

  • prefix = <ident> (required): a bare identifier prefixed to every metric name, for example prefix = cache.
  • labels(a, b, ...) (optional): the names of labels applied to every metric in the group. The label values are supplied to the generated new() and may be of any type implementing saluki_metrics::Stringable.

§Field attributes

  • #[metric(level = info | debug | trace)] (optional, default info): the metric’s verbosity level.
  • #[metric(mapped(...))] (optional): see “Mapped metrics” below.
  • #[metric(derived(...))] (optional, Histogram fields only): see “Derived gauges” below.

§Generated code

For a struct Foo, this generates Foo::new(<label>, ...) -> Foo (generic over each label value’s type), a Foo::<field>(&self) -> &<Handle> accessor per metric, a Foo::<field>_name() -> &'static str helper, and a Debug implementation that prints only the struct name. Clone is not generated; derive it directly where needed. Mapped and derived fields get different accessors, which their sections below describe.

§Mapped metrics

A field can be mapped by one or more labels whose values are supplied at emission time rather than at construction:

ⓘ
#[static_metrics(prefix = component, labels(component_id))]
#[derive(Clone)]
struct Metrics {
    events_sent_total: Counter,
    // one label, sourced from any `Stringable`:
    #[metric(mapped(reason))]
    events_discarded_total: Counter,
    // or pinned to a concrete type for misuse resistance (and, potentially, cheaper conversion):
    #[metric(mapped(reason: DiscardReason))]
    other_total: Counter,
}

A mapped field’s storage is rewritten to hold a concurrent map of the dynamic label values to lazily registered handles (the source keeps writing Counter/Gauge/Histogram). Its accessor accepts each label value either by value or by reference and returns an owned handle. A typed label still only accepts its concrete type (or a reference to it), so lightweight Copy values can be passed directly without borrowing:

ⓘ
// bare label -> generic `Stringable` parameter; typed label -> `Borrow<T>`, so `T` or `&T` are both accepted
metrics.events_discarded_total("queue_full").increment(1);
metrics.other_total(DiscardReason::QueueFull).increment(1);
metrics.other_total(&DiscardReason::QueueFull).increment(1);

Each mapped handle is registered with the fixed struct labels plus the mapped labels. One or many labels may be given, comma-separated, mixing bare and typed forms.

§Derived gauges

A Histogram field can track the lowest value, the highest value, or both, that it records:

ⓘ
#[static_metrics(prefix = component, labels(component_id))]
#[derive(Clone)]
struct Metrics {
    #[metric(derived(max))]
    payload_size_bytes: Histogram,
    #[metric(derived(min, max))]
    latency_seconds: Histogram,
}

Each derived value registers a gauge named "<prefix>_<field_name>_min" or "<prefix>_<field_name>_max", with the same level and labels as the histogram. The Foo::<field>_min_name() and Foo::<field>_max_name() helpers return these names. The macro adds one hidden field, __<field_name>_derived, at the end of the struct to hold the gauges.

The accessor returns a saluki_metrics::DerivedHistogram<'_>. Its record and record_many methods record the value to the histogram, then set each gauge only if the value is a new lowest or highest value:

ⓘ
metrics.payload_size_bytes().record(body_len as f64);
  • The gauges track values for the life of the struct, and never reset.
  • Clones share the tracked values. Separate new() calls with the same labels do not, and can move the shared gauges back. Create the struct once for each label set, and clone it.
  • new() registers the gauges. With the internal recorder, a gauge reads 0 until the histogram records a value.
  • NaN values go to the histogram, but never change a gauge.
  • Always record through the accessor. Code that can see the Histogram field can record to it directly, and that skips the gauges.
  • A field can’t be both mapped and derived.