Class MetricsCardinalityManager
java.lang.Object
com.loomcache.server.metrics.MetricsCardinalityManager
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 Summary
ConstructorsConstructorDescriptionMetricsCardinalityManager(int maxCardinalityPerMetric, int maxCardinalityGlobal, long cleanupIntervalMs) -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckAndRegisterLabels(String metricName, io.micrometer.core.instrument.Tags tags) Check if a new label combination can be added for a metric.voidCleanup stale metrics (no access in the last interval).Get all metrics with their cardinalities.intgetMetricCardinality(String metricName) Get cardinality for a specific metric.longgetMetricEvictions(String metricName) Get evictions for a specific metric.longgetMetricOverflows(String metricName) Get overflow count for a specific metric.longGet total cardinality across all metrics.longGet total eviction count.longGet total overflow count.booleanperformLruEviction(String metricName) Perform LRU eviction for a specific metric.voidreset()Reset all tracking (for testing).
-
Constructor Details
-
MetricsCardinalityManager
public MetricsCardinalityManager(int maxCardinalityPerMetric, int maxCardinalityGlobal, long cleanupIntervalMs)
-
-
Method Details
-
checkAndRegisterLabels
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
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
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
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
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
-
reset
public void reset()Reset all tracking (for testing).
-