Class MetricsAggregator
java.lang.Object
com.loomcache.server.metrics.MetricsAggregator
Aggregates high-cardinality per-key metrics into lower-cardinality rollup metrics.
Goals: - Aggregate per-key metrics (e.g., cache.get.count{key="user:123"}) into cache.get.count (without key label) - Roll up per-client metrics into per-client-type metrics - Provide pre-aggregated views for dashboard queries - Support downsampling of historical metrics
Thread-safe implementation using a concurrent rule registry plus a capped cache guarded by a lock.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordAggregation rule: defines how to roll up metrics matching a pattern.static enumAggregation type. -
Constructor Summary
ConstructorsConstructorDescriptionMetricsAggregator(io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a new MetricsAggregator with the given MeterRegistry. -
Method Summary
Modifier and TypeMethodDescriptionvoidClear all aggregation cache.voiddownsample(int downsampleFactor) Downsample metrics: keep only every Nth point.doublegetAggregatedValue(String aggregatedMetricName) Get an aggregated metric value.Get all aggregated metrics.doubleGet cardinality reduction estimate.longgetLastAggregationTime(String metricName) Get the last aggregation timestamp for a metric.voidPerform aggregation based on registered rules.voidRegister default aggregation rules for cache metrics.voidregisterPerKeyAggregation(String metricPattern, MetricsAggregator.AggregationType aggregationType) Register a simple per-key aggregation rule.voidRegister an aggregation rule.
-
Constructor Details
-
MetricsAggregator
public MetricsAggregator(io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a new MetricsAggregator with the given MeterRegistry.- Parameters:
meterRegistry- the MeterRegistry for registering aggregated metrics (must not be null)- Throws:
NullPointerException- if meterRegistry is null
-
-
Method Details
-
registerRule
Register an aggregation rule. Example: aggregate all cache.get.{key=*} metrics to cache.get.total- Parameters:
rule- the aggregation rule (must not be null)- Throws:
NullPointerException- if rule is null
-
registerPerKeyAggregation
public void registerPerKeyAggregation(String metricPattern, MetricsAggregator.AggregationType aggregationType) Register a simple per-key aggregation rule. Strips the "key" label and aggregates to the base metric name.- Parameters:
metricPattern- the metric name pattern (e.g., "cache.get.count") (must not be null)aggregationType- the aggregation type (SUM, AVG, etc.) (must not be null)- Throws:
NullPointerException- if metricPattern or aggregationType is null
-
performAggregation
public void performAggregation()Perform aggregation based on registered rules. Called periodically to compute aggregated metrics. -
getAggregatedValue
Get an aggregated metric value.- Parameters:
aggregatedMetricName- the aggregated metric name (must not be null)- Returns:
- the aggregated value, or 0 if not found
- Throws:
NullPointerException- if aggregatedMetricName is null
-
getLastAggregationTime
Get the last aggregation timestamp for a metric.- Parameters:
metricName- the aggregated metric name (must not be null)- Returns:
- the last aggregation time in millis, or -1 if never aggregated
-
getAllAggregatedMetrics
-
clearCache
public void clearCache()Clear all aggregation cache. -
downsample
public void downsample(int downsampleFactor) Downsample metrics: keep only every Nth point. Useful for reducing storage of historical metrics.- Parameters:
downsampleFactor- the factor (e.g., 10 means keep 1 in 10 points)
-
registerDefaultCacheAggregationRules
public void registerDefaultCacheAggregationRules()Register default aggregation rules for cache metrics. -
getCardinalityReductionPercent
public double getCardinalityReductionPercent()Get cardinality reduction estimate. Shows how much cardinality is reduced by aggregation.- Returns:
- percentage of cardinality reduction
-