Interface Metrics

All Known Implementing Classes:
MetricsRegistry, MicrometerMetrics, OtelMetrics

public interface Metrics
Receives the counters, gauges and timers the runtime records. The default is NOOP, which records nothing and allocates nothing; a real implementation is installed with Application.metrics(Metrics) before startup.

Lookup versus recording. counter(java.lang.String, java.lang.String...), gauge(java.lang.String, java.lang.String...) and timer(java.lang.String, java.lang.String...) look an instrument up, or create it, by name and tags. They may allocate and take a lock, so callers resolve instruments once and keep them; the runtime does. The recording methods of an instrument are the hot path: implementations make them thread-safe, non-blocking and allocation-free. The runtime never lets a failing instrument fail a request: a RuntimeException from any method is caught, reported once to System.Logger, and the measurement is dropped.

Names and tags. A name is lowercase words separated by dots, for example axiom.http.requests. Tags are given as alternating keys and values. Tag values must come from a small, fixed set: route templates, HTTP methods and status classes, never raw request paths, query strings, header values, identities or other user input. The runtime follows this rule itself, and an implementation should bound the number of series it keeps (the registry in axiom-metrics does) so that an application mistake cannot exhaust memory.

The same name and tags always identify the same series. The runtime's own metric names and tags are listed in the observability guide.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    A monotonically increasing count.
    static interface 
    A value that moves both ways.
    static interface 
    A recorder of durations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Metrics
    Records nothing.
  • Method Summary

    Modifier and Type
    Method
    Description
    counter(String name, String... tags)
    Returns the monotonically increasing counter for a series, creating it if needed.
    gauge(String name, String... tags)
    Returns the gauge for a series, creating it if needed.
    default Optional<String>
    Optional Prometheus text exposition (format 0.0.4) of this implementation's series.
    timer(String name, String... tags)
    Returns the timer for a series, creating it if needed.
  • Field Details

    • NOOP

      static final Metrics NOOP
      Records nothing. Its instruments are shared, immutable and free of allocation.
  • Method Details

    • counter

      Metrics.Counter counter(String name, String... tags)
      Returns the monotonically increasing counter for a series, creating it if needed.
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe counter; never null
      Throws:
      IllegalArgumentException - if the name or tags are invalid or conflict with the metric's earlier use
    • gauge

      Metrics.Gauge gauge(String name, String... tags)
      Returns the gauge for a series, creating it if needed. A gauge is a value that moves both ways, such as a queue depth. It is changed by deltas, so several components can share one series and the value stays the sum of their contributions.
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe gauge; never null
      Throws:
      IllegalArgumentException - if the name or tags are invalid or conflict with the metric's earlier use
    • timer

      Metrics.Timer timer(String name, String... tags)
      Returns the timer for a series, creating it if needed. A timer records durations and keeps their distribution, so latency percentiles can be derived from it.
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe timer; never null
      Throws:
      IllegalArgumentException - if the name or tags are invalid or conflict with the metric's earlier use
    • prometheusText

      default Optional<String> prometheusText()
      Optional Prometheus text exposition (format 0.0.4) of this implementation's series. The management listener's /metrics endpoint calls this on every scrape; the runtime caps the returned text at the configured response limit. The default is empty: an implementation that does not expose text serves an empty scrape. MetricsRegistry in axiom-metrics overrides this with PrometheusText.render(this); no rendering code or third-party type enters core.
      Returns:
      exposition text, or empty when this implementation has no text form