|
9 | 9 | import io.prometheus.metrics.model.snapshots.Labels; |
10 | 10 | import io.prometheus.metrics.model.snapshots.MetricSnapshots; |
11 | 11 | import io.prometheus.metrics.model.snapshots.SummarySnapshot; |
| 12 | +import java.util.Arrays; |
12 | 13 | import java.util.Collections; |
13 | 14 | import java.util.List; |
14 | 15 | import java.util.Map; |
@@ -54,6 +55,28 @@ public class CacheMetricsCollector implements MultiCollector { |
54 | 55 |
|
55 | 56 | private static final double NANOSECONDS_PER_SECOND = 1_000_000_000.0; |
56 | 57 |
|
| 58 | + private static final String METRIC_NAME_CACHE_HIT = "guava_cache_hit"; |
| 59 | + private static final String METRIC_NAME_CACHE_MISS = "guava_cache_miss"; |
| 60 | + private static final String METRIC_NAME_CACHE_REQUESTS = "guava_cache_requests"; |
| 61 | + private static final String METRIC_NAME_CACHE_EVICTION = "guava_cache_eviction"; |
| 62 | + private static final String METRIC_NAME_CACHE_LOAD_FAILURE = "guava_cache_load_failure"; |
| 63 | + private static final String METRIC_NAME_CACHE_LOADS = "guava_cache_loads"; |
| 64 | + private static final String METRIC_NAME_CACHE_SIZE = "guava_cache_size"; |
| 65 | + private static final String METRIC_NAME_CACHE_LOAD_DURATION_SECONDS = |
| 66 | + "guava_cache_load_duration_seconds"; |
| 67 | + |
| 68 | + private static final List<String> ALL_METRIC_NAMES = |
| 69 | + Collections.unmodifiableList( |
| 70 | + Arrays.asList( |
| 71 | + METRIC_NAME_CACHE_HIT, |
| 72 | + METRIC_NAME_CACHE_MISS, |
| 73 | + METRIC_NAME_CACHE_REQUESTS, |
| 74 | + METRIC_NAME_CACHE_EVICTION, |
| 75 | + METRIC_NAME_CACHE_LOAD_FAILURE, |
| 76 | + METRIC_NAME_CACHE_LOADS, |
| 77 | + METRIC_NAME_CACHE_SIZE, |
| 78 | + METRIC_NAME_CACHE_LOAD_DURATION_SECONDS)); |
| 79 | + |
57 | 80 | protected final ConcurrentMap<String, Cache<?, ?>> children = new ConcurrentHashMap<>(); |
58 | 81 |
|
59 | 82 | /** |
@@ -94,33 +117,33 @@ public MetricSnapshots collect() { |
94 | 117 | final List<String> labelNames = Collections.singletonList("cache"); |
95 | 118 |
|
96 | 119 | final CounterSnapshot.Builder cacheHitTotal = |
97 | | - CounterSnapshot.builder().name("guava_cache_hit").help("Cache hit totals"); |
| 120 | + CounterSnapshot.builder().name(METRIC_NAME_CACHE_HIT).help("Cache hit totals"); |
98 | 121 |
|
99 | 122 | final CounterSnapshot.Builder cacheMissTotal = |
100 | | - CounterSnapshot.builder().name("guava_cache_miss").help("Cache miss totals"); |
| 123 | + CounterSnapshot.builder().name(METRIC_NAME_CACHE_MISS).help("Cache miss totals"); |
101 | 124 |
|
102 | 125 | final CounterSnapshot.Builder cacheRequestsTotal = |
103 | | - CounterSnapshot.builder().name("guava_cache_requests").help("Cache request totals"); |
| 126 | + CounterSnapshot.builder().name(METRIC_NAME_CACHE_REQUESTS).help("Cache request totals"); |
104 | 127 |
|
105 | 128 | final CounterSnapshot.Builder cacheEvictionTotal = |
106 | 129 | CounterSnapshot.builder() |
107 | | - .name("guava_cache_eviction") |
| 130 | + .name(METRIC_NAME_CACHE_EVICTION) |
108 | 131 | .help("Cache eviction totals, doesn't include manually removed entries"); |
109 | 132 |
|
110 | 133 | final CounterSnapshot.Builder cacheLoadFailure = |
111 | | - CounterSnapshot.builder().name("guava_cache_load_failure").help("Cache load failures"); |
| 134 | + CounterSnapshot.builder().name(METRIC_NAME_CACHE_LOAD_FAILURE).help("Cache load failures"); |
112 | 135 |
|
113 | 136 | final CounterSnapshot.Builder cacheLoadTotal = |
114 | 137 | CounterSnapshot.builder() |
115 | | - .name("guava_cache_loads") |
| 138 | + .name(METRIC_NAME_CACHE_LOADS) |
116 | 139 | .help("Cache loads: both success and failures"); |
117 | 140 |
|
118 | 141 | final GaugeSnapshot.Builder cacheSize = |
119 | | - GaugeSnapshot.builder().name("guava_cache_size").help("Cache size"); |
| 142 | + GaugeSnapshot.builder().name(METRIC_NAME_CACHE_SIZE).help("Cache size"); |
120 | 143 |
|
121 | 144 | final SummarySnapshot.Builder cacheLoadSummary = |
122 | 145 | SummarySnapshot.builder() |
123 | | - .name("guava_cache_load_duration_seconds") |
| 146 | + .name(METRIC_NAME_CACHE_LOAD_DURATION_SECONDS) |
124 | 147 | .help("Cache load duration: both success and failures"); |
125 | 148 |
|
126 | 149 | for (final Map.Entry<String, Cache<?, ?>> c : children.entrySet()) { |
@@ -192,4 +215,9 @@ public MetricSnapshots collect() { |
192 | 215 |
|
193 | 216 | return metricSnapshotsBuilder.build(); |
194 | 217 | } |
| 218 | + |
| 219 | + @Override |
| 220 | + public List<String> getPrometheusNames() { |
| 221 | + return ALL_METRIC_NAMES; |
| 222 | + } |
195 | 223 | } |
0 commit comments