Class MetricsCardinalityManager

java.lang.Object
com.loomcache.server.metrics.MetricsCardinalityManager

public class MetricsCardinalityManager extends Object
Manages metric label cardinality to prevent memory explosion from unbounded labels.

Tracks unique label combinations per metric and enforces limits: - Per-metric cardinality limit (default 1000) - Global cardinality limit across all metrics (default 10000) - LRU eviction when limits are exceeded - Overflow bucket for excess metrics - Warning logs at 80% threshold

Thread-safe implementation using ConcurrentHashMap and ReentrantReadWriteLock.

  • Constructor Details

    • MetricsCardinalityManager

      public MetricsCardinalityManager(int maxCardinalityPerMetric, int maxCardinalityGlobal, long cleanupIntervalMs)
  • Method Details

    • checkAndRegisterLabels

      public boolean checkAndRegisterLabels(String metricName, io.micrometer.core.instrument.Tags tags)
      Check if a new label combination can be added for a metric. If limit exceeded, may evict LRU entries or return false for overflow handling.
      Parameters:
      metricName - the metric name (must not be null)
      tags - the label tags (must not be null)
      Returns:
      true if the label can be added, false if overflow bucket should be used
      Throws:
      NullPointerException - if metricName or tags is null
    • performLruEviction

      public boolean performLruEviction(String metricName)
      Perform LRU eviction for a specific metric. Evicts the least-recently-used label to make room for a new one.
      Parameters:
      metricName - the metric name (must not be null)
      Returns:
      true if eviction was successful
      Throws:
      NullPointerException - if metricName is null
    • cleanupStaleMetrics

      public void cleanupStaleMetrics()
      Cleanup stale metrics (no access in the last interval). Called periodically to free up memory.
    • getMetricCardinality

      public int getMetricCardinality(String metricName)
      Get cardinality for a specific metric.
      Parameters:
      metricName - the metric name (must not be null)
      Returns:
      the cardinality count for the metric
      Throws:
      NullPointerException - if metricName is null
    • getTotalCardinality

      public long getTotalCardinality()
      Get total cardinality across all metrics.
    • getTotalEvicted

      public long getTotalEvicted()
      Get total eviction count.
    • getTotalOverflow

      public long getTotalOverflow()
      Get total overflow count.
    • getMetricEvictions

      public long getMetricEvictions(String metricName)
      Get evictions for a specific metric.
      Parameters:
      metricName - the metric name (must not be null)
      Returns:
      the eviction count for the metric
      Throws:
      NullPointerException - if metricName is null
    • getMetricOverflows

      public long getMetricOverflows(String metricName)
      Get overflow count for a specific metric.
      Parameters:
      metricName - the metric name (must not be null)
      Returns:
      the overflow count for the metric
      Throws:
      NullPointerException - if metricName is null
    • getAllMetricsCardinality

      public Map<String,Integer> getAllMetricsCardinality()
      Get all metrics with their cardinalities.
      Returns:
      an unmodifiable map of metric names to cardinality counts
    • reset

      public void reset()
      Reset all tracking (for testing).