Class MetricsRegistry

java.lang.Object
com.jsgalactic.axiom.metrics.MetricsRegistry
All Implemented Interfaces:
Metrics

public final class MetricsRegistry extends Object implements Metrics
An in-memory Metrics implementation with a bounded number of series. Use it to assert on measurements in tests, or render it with PrometheusText for a scrape endpoint.

Recording is lock-free and allocation-free: counters and gauges are LongAdders and a timer adds to one of a fixed set of bucket adders. Looking an instrument up takes a lock the first time a series is created. The registry is thread-safe; readers see each value as it is updated, so a read taken while requests are in flight is not an atomic snapshot across series.

Cardinality. The registry keeps at most maxSeries distinct name-and-tag combinations. Once the limit is reached, a request for a further new series gets a working instrument that is not stored, and droppedSeries() counts the request, so an application bug that puts user input into a tag cannot exhaust memory. Existing series keep recording. Tag values longer than 256 characters are truncated.

Histograms and help. Timers are histograms with DEFAULT_BUCKETS unless MetricsRegistry.Builder.timerBuckets sets other bounds, for every timer or for one name. The registry keeps no quantiles and no exemplars: a histogram lets the monitoring system derive quantiles across instances, and an exemplar would need a per-request identifier that must never be a tag. MetricsRegistry.Builder.help gives a metric text for the exposition's # HELP line.

Consistency. A metric name always has one kind (counter, gauge or timer) and one set of tag keys; using it differently fails with IllegalArgumentException.

  • Field Details

    • DEFAULT_MAX_SERIES

      public static final int DEFAULT_MAX_SERIES
      Series limit of create().
      See Also:
    • MAX_VALUE_LENGTH

      public static final int MAX_VALUE_LENGTH
      Longest stored tag value; longer values are truncated.
      See Also:
    • MAX_NAME_LENGTH

      public static final int MAX_NAME_LENGTH
      Longest metric name.
      See Also:
    • MAX_BUCKETS

      public static final int MAX_BUCKETS
      Most upper bounds one timer may have, not counting the implicit final bucket.
      See Also:
    • MAX_HELP_LENGTH

      public static final int MAX_HELP_LENGTH
      Longest help text.
      See Also:
    • DEFAULT_BUCKETS

      public static final List<Duration> DEFAULT_BUCKETS
      Timer bucket upper bounds used unless a timer is configured otherwise: 1 ms, 5 ms, 10 ms, 25 ms, 50 ms, 100 ms, 250 ms, 500 ms, 1 s, 2.5 s, 5 s and 10 s. An implicit final bucket holds everything larger.
  • Method Details

    • builder

      public static MetricsRegistry.Builder builder()
      Starts a registry with explicit settings: the series limit, the histogram buckets of all timers or of one timer, and help text.
      Returns:
      a new builder with the defaults of create()
    • create

      public static MetricsRegistry create()
      Creates a registry that keeps up to DEFAULT_MAX_SERIES series.
      Returns:
      an empty registry
    • create

      public static MetricsRegistry create(int maxSeries)
      Creates a registry with an explicit series limit.
      Parameters:
      maxSeries - most distinct name-and-tag combinations to keep, at least one
      Returns:
      an empty registry
      Throws:
      IllegalArgumentException - if the limit is below one
    • counter

      public Metrics.Counter counter(String name, String... tags)
      Description copied from interface: Metrics
      Returns the monotonically increasing counter for a series, creating it if needed.
      Specified by:
      counter in interface Metrics
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe counter; never null
    • gauge

      public Metrics.Gauge gauge(String name, String... tags)
      Description copied from interface: Metrics
      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.
      Specified by:
      gauge in interface Metrics
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe gauge; never null
    • timer

      public Metrics.Timer timer(String name, String... tags)
      Description copied from interface: Metrics
      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.
      Specified by:
      timer in interface Metrics
      Parameters:
      name - dotted lowercase metric name
      tags - alternating tag keys and values, possibly none
      Returns:
      a thread-safe timer; never null
    • prometheusText

      public Optional<String> prometheusText()
      Description copied from interface: Metrics
      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.
      Specified by:
      prometheusText in interface Metrics
      Returns:
      exposition text, or empty when this implementation has no text form
    • counterValue

      public long counterValue(String name, String... tags)
      Reads a counter.
      Parameters:
      name - metric name
      tags - alternating tag keys and values
      Returns:
      its value, or zero if the series does not exist
    • gaugeValue

      public long gaugeValue(String name, String... tags)
      Reads a gauge.
      Parameters:
      name - metric name
      tags - alternating tag keys and values
      Returns:
      its value, or zero if the series does not exist
    • timerSnapshot

      public MetricsRegistry.TimerSnapshot timerSnapshot(String name, String... tags)
      Reads a timer.
      Parameters:
      name - metric name
      tags - alternating tag keys and values
      Returns:
      its observation count and total, or zeros if the series does not exist
    • seriesCount

      public int seriesCount()
      Returns how many distinct series are stored.
      Returns:
      series count, at most the limit
    • droppedSeries

      public long droppedSeries()
      Returns how many lookups asked for a new series after the limit was reached.
      Returns:
      number of refused series creations
    • help

      public Optional<String> help(String name)
      Returns the help text of a metric.
      Parameters:
      name - metric name
      Returns:
      the text given to MetricsRegistry.Builder.help, or empty
    • timerBuckets

      public List<Duration> timerBuckets(String name)
      Returns a timer's bucket upper bounds.
      Parameters:
      name - metric name
      Returns:
      the configured bounds in ascending order, or the registry default