3434import com .github .benmanes .caffeine .cache .stats .CacheStats ;
3535import com .github .benmanes .caffeine .cache .stats .StatsCounter ;
3636
37+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_EVICTION ;
38+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_EVICTION_WEIGHT ;
39+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_HITS ;
40+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_LOAD_FAILURE ;
41+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_LOAD_FAILURE_TIME ;
42+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_LOAD_SUCCESS ;
43+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_LOAD_SUCCESS_TIME ;
44+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_MISSES ;
45+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .CACHE_SIZE ;
46+ import static io .aiven .kafka .tieredstorage .metrics .CaffeineMetricsRegistry .METRIC_CONTEXT ;
47+
3748/**
3849 * Records cache metrics managed by Caffeine {@code Cache#stats}.
3950 *
4354 * <a href="https://github.com/micrometer-metrics/micrometer/blob/main/micrometer-core/src/main/java/io/micrometer/core/instrument/binder/cache/CaffeineStatsCounter.java">Micrometer</a>
4455 */
4556public class CaffeineStatsCounter implements StatsCounter {
46-
47- static final String CACHE_HITS = "cache-hits" ;
48- static final String CACHE_HITS_TOTAL = CACHE_HITS + "-total" ;
49- static final String CACHE_MISSES = "cache-misses" ;
50- static final String CACHE_MISSES_TOTAL = CACHE_MISSES + "-total" ;
51- static final String CACHE_LOAD = "cache-load" ;
52- static final String CACHE_LOAD_SUCCESS = CACHE_LOAD + "-success" ;
53- static final String CACHE_LOAD_SUCCESS_TOTAL = CACHE_LOAD_SUCCESS + "-total" ;
54- static final String CACHE_LOAD_SUCCESS_TIME = CACHE_LOAD_SUCCESS + "-time" ;
55- static final String CACHE_LOAD_SUCCESS_TIME_TOTAL = CACHE_LOAD_SUCCESS_TIME + "-total" ;
56- static final String CACHE_LOAD_FAILURE = CACHE_LOAD + "-failure" ;
57- static final String CACHE_LOAD_FAILURE_TOTAL = CACHE_LOAD_FAILURE + "-total" ;
58- static final String CACHE_LOAD_FAILURE_TIME = CACHE_LOAD_FAILURE + "-time" ;
59- static final String CACHE_LOAD_FAILURE_TIME_TOTAL = CACHE_LOAD_FAILURE_TIME + "-total" ;
60-
61- static final String CACHE_EVICTION = "cache-eviction" ;
62- static final String CACHE_EVICTION_TOTAL = CACHE_EVICTION + "-total" ;
63- static final String CACHE_EVICTION_WEIGHT = CACHE_EVICTION + "-weight" ;
64- static final String CACHE_EVICTION_WEIGHT_TOTAL = CACHE_EVICTION_WEIGHT + "-total" ;
65-
66- static final String CACHE_SIZE = "cache-size" ;
67- static final String CACHE_SIZE_TOTAL = CACHE_SIZE + "-total" ;
68-
6957 private final org .apache .kafka .common .metrics .Metrics metrics ;
7058
7159 private final LongAdder cacheHitCount ;
@@ -78,7 +66,7 @@ public class CaffeineStatsCounter implements StatsCounter {
7866 private final LongAdder cacheEvictionWeightTotal ;
7967 private final ConcurrentHashMap <RemovalCause , LongAdder > cacheEvictionCountByCause ;
8068 private final ConcurrentHashMap <RemovalCause , LongAdder > cacheEvictionWeightByCause ;
81- private final String groupName ;
69+ private final CaffeineMetricsRegistry metricsRegistry ;
8270
8371 public CaffeineStatsCounter (final String groupName ) {
8472 cacheHitCount = new LongAdder ();
@@ -90,8 +78,6 @@ public CaffeineStatsCounter(final String groupName) {
9078 cacheEvictionCountTotal = new LongAdder ();
9179 cacheEvictionWeightTotal = new LongAdder ();
9280
93- this .groupName = groupName ;
94-
9581 cacheEvictionCountByCause = new ConcurrentHashMap <>();
9682 Arrays .stream (RemovalCause .values ()).forEach (cause -> cacheEvictionCountByCause .put (cause , new LongAdder ()));
9783
@@ -102,42 +88,84 @@ public CaffeineStatsCounter(final String groupName) {
10288
10389 metrics = new org .apache .kafka .common .metrics .Metrics (
10490 new MetricConfig (), List .of (reporter ), Time .SYSTEM ,
105- new KafkaMetricsContext ("aiven.kafka.server.tieredstorage.cache" )
91+ new KafkaMetricsContext (METRIC_CONTEXT )
10692 );
10793
108- initSensor (CACHE_HITS , CACHE_HITS_TOTAL , cacheHitCount );
109- initSensor (CACHE_MISSES , CACHE_MISSES_TOTAL , cacheMissCount );
110- initSensor (CACHE_LOAD_SUCCESS , CACHE_LOAD_SUCCESS_TOTAL , cacheLoadSuccessCount );
111- initSensor (CACHE_LOAD_SUCCESS_TIME , CACHE_LOAD_SUCCESS_TIME_TOTAL , cacheLoadSuccessTimeTotal );
112- initSensor (CACHE_LOAD_FAILURE , CACHE_LOAD_FAILURE_TOTAL , cacheLoadFailureCount );
113- initSensor (CACHE_LOAD_FAILURE_TIME , CACHE_LOAD_FAILURE_TIME_TOTAL , cacheLoadFailureTimeTotal );
114- initSensor (CACHE_EVICTION , CACHE_EVICTION_TOTAL , cacheEvictionCountTotal );
94+ metricsRegistry = new CaffeineMetricsRegistry (groupName );
95+ initSensor (
96+ metricsRegistry .cacheHitsMetricName ,
97+ CACHE_HITS ,
98+ cacheHitCount
99+ );
100+ initSensor (
101+ metricsRegistry .cacheMissesMetricName ,
102+ CACHE_MISSES ,
103+ cacheMissCount
104+ );
105+ initSensor (
106+ metricsRegistry .cacheLoadSuccessMetricName ,
107+ CACHE_LOAD_SUCCESS ,
108+ cacheLoadSuccessCount
109+ );
110+ initSensor (
111+ metricsRegistry .cacheLoadSuccessTimeMetricName ,
112+ CACHE_LOAD_SUCCESS_TIME ,
113+ cacheLoadSuccessTimeTotal
114+ );
115+ initSensor (
116+ metricsRegistry .cacheLoadFailureMetricName ,
117+ CACHE_LOAD_FAILURE ,
118+ cacheLoadFailureCount
119+ );
120+ initSensor (
121+ metricsRegistry .cacheLoadFailureTimeMetricName ,
122+ CACHE_LOAD_FAILURE_TIME ,
123+ cacheLoadFailureTimeTotal
124+ );
125+ initSensor (
126+ metricsRegistry .cacheEvictionMetricName ,
127+ CACHE_EVICTION ,
128+ cacheEvictionCountTotal
129+ );
115130 Arrays .stream (RemovalCause .values ()).forEach (cause ->
116- initSensor ("cause." + cause .name () + "." + CACHE_EVICTION , CACHE_EVICTION_TOTAL ,
117- cacheEvictionCountByCause .get (cause ), () -> Map .of ("cause" , cause .name ()), "cause" )
131+ initSensor (
132+ metricsRegistry .cacheEvictionByCauseMetricName ,
133+ "cause." + cause .name () + "." + CACHE_EVICTION ,
134+ cacheEvictionCountByCause .get (cause ),
135+ () -> Map .of ("cause" , cause .name ())
136+ )
118137 );
119138
120- initSensor (CACHE_EVICTION_WEIGHT , CACHE_EVICTION_WEIGHT_TOTAL , cacheEvictionWeightTotal );
139+ initSensor (
140+ metricsRegistry .cacheEvictionWeightMetricName ,
141+ CACHE_EVICTION_WEIGHT ,
142+ cacheEvictionWeightTotal
143+ );
121144
122145 Arrays .stream (RemovalCause .values ()).forEach (cause ->
123- initSensor ("cause." + cause .name () + "." + CACHE_EVICTION , CACHE_EVICTION_WEIGHT_TOTAL ,
124- cacheEvictionWeightByCause .get (cause ), () -> Map .of ("cause" , cause .name ()), "cause" )
146+ initSensor (
147+ metricsRegistry .cacheEvictionWeightByCauseMetricName ,
148+ "cause." + cause .name () + "." + CACHE_EVICTION ,
149+ cacheEvictionWeightByCause .get (cause ),
150+ () -> Map .of ("cause" , cause .name ())
151+ )
125152 );
126153 }
127154
128- private void initSensor (final String sensorName ,
129- final String metricName ,
130- final LongAdder value ,
131- final Supplier < Map < String , String >> tagsSupplier ,
132- final String ... tagNames ) {
133- final var name = new MetricNameTemplate ( metricName , groupName , "" , tagNames );
155+ private void initSensor (
156+ final MetricNameTemplate metricNameTemplate ,
157+ final String sensorName ,
158+ final LongAdder value ,
159+ final Supplier < Map < String , String >> tagsSupplier
160+ ) {
134161 new SensorProvider (metrics , sensorName , tagsSupplier )
135- .with (name , new MeasurableValue (value ::sum ))
162+ .with (metricNameTemplate , new MeasurableValue (value ::sum ))
136163 .get ();
137164 }
138165
139- private void initSensor (final String sensorName , final String metricName , final LongAdder value ) {
140- initSensor (sensorName , metricName , value , Collections ::emptyMap );
166+ private void initSensor (final MetricNameTemplate metricNameTemplate , final String sensorName ,
167+ final LongAdder value ) {
168+ initSensor (metricNameTemplate , sensorName , value , Collections ::emptyMap );
141169 }
142170
143171 @ Override
@@ -193,9 +221,8 @@ public void recordHit() {
193221 * @param sizeSupplier operation from cache to provide cache size value
194222 */
195223 public void registerSizeMetric (final Supplier <Long > sizeSupplier ) {
196- final var name = new MetricNameTemplate (CACHE_SIZE_TOTAL , groupName , "" );
197224 new SensorProvider (metrics , CACHE_SIZE )
198- .with (name , new MeasurableValue (sizeSupplier ))
225+ .with (metricsRegistry . cacheSizeTotalMetricName , new MeasurableValue (sizeSupplier ))
199226 .get ();
200227 }
201228
0 commit comments