Class MetricsAggregator

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

public class MetricsAggregator extends Object
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.

  • 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

      public void registerRule(MetricsAggregator.AggregationRule rule)
      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

      public double getAggregatedValue(String aggregatedMetricName)
      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

      public long getLastAggregationTime(String metricName)
      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

      public Map<String,Double> getAllAggregatedMetrics()
      Get all aggregated metrics.
      Returns:
      an unmodifiable copy of all aggregated metrics
    • 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