Skip to content

Commit 2f30038

Browse files
authored
[prometheus-metrics-instrumentation-caffeine] implement getPrometheusNames (#1206)
Signed-off-by: Petar Heyken <[email protected]>
1 parent e17c0a1 commit 2f30038

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

prometheus-metrics-instrumentation-caffeine/src/main/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollector.java

+38-9
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@
5555
public class CacheMetricsCollector implements MultiCollector {
5656
private static final double NANOSECONDS_PER_SECOND = 1_000_000_000.0;
5757

58+
private static final String METRIC_NAME_CACHE_HIT = "caffeine_cache_hit";
59+
private static final String METRIC_NAME_CACHE_MISS = "caffeine_cache_miss";
60+
private static final String METRIC_NAME_CACHE_REQUESTS = "caffeine_cache_requests";
61+
private static final String METRIC_NAME_CACHE_EVICTION = "caffeine_cache_eviction";
62+
private static final String METRIC_NAME_CACHE_EVICTION_WEIGHT = "caffeine_cache_eviction_weight";
63+
private static final String METRIC_NAME_CACHE_LOAD_FAILURE = "caffeine_cache_load_failure";
64+
private static final String METRIC_NAME_CACHE_LOADS = "caffeine_cache_loads";
65+
private static final String METRIC_NAME_CACHE_ESTIMATED_SIZE = "caffeine_cache_estimated_size";
66+
private static final String METRIC_NAME_CACHE_LOAD_DURATION_SECONDS =
67+
"caffeine_cache_load_duration_seconds";
68+
69+
private static final List<String> ALL_METRIC_NAMES =
70+
Collections.unmodifiableList(
71+
Arrays.asList(
72+
METRIC_NAME_CACHE_HIT,
73+
METRIC_NAME_CACHE_MISS,
74+
METRIC_NAME_CACHE_REQUESTS,
75+
METRIC_NAME_CACHE_EVICTION,
76+
METRIC_NAME_CACHE_EVICTION_WEIGHT,
77+
METRIC_NAME_CACHE_LOAD_FAILURE,
78+
METRIC_NAME_CACHE_LOADS,
79+
METRIC_NAME_CACHE_ESTIMATED_SIZE,
80+
METRIC_NAME_CACHE_LOAD_DURATION_SECONDS));
81+
5882
protected final ConcurrentMap<String, Cache<?, ?>> children = new ConcurrentHashMap<>();
5983

6084
/**
@@ -107,40 +131,40 @@ public MetricSnapshots collect() {
107131
final List<String> labelNames = Arrays.asList("cache");
108132

109133
final CounterSnapshot.Builder cacheHitTotal =
110-
CounterSnapshot.builder().name("caffeine_cache_hit").help("Cache hit totals");
134+
CounterSnapshot.builder().name(METRIC_NAME_CACHE_HIT).help("Cache hit totals");
111135

112136
final CounterSnapshot.Builder cacheMissTotal =
113-
CounterSnapshot.builder().name("caffeine_cache_miss").help("Cache miss totals");
137+
CounterSnapshot.builder().name(METRIC_NAME_CACHE_MISS).help("Cache miss totals");
114138

115139
final CounterSnapshot.Builder cacheRequestsTotal =
116140
CounterSnapshot.builder()
117-
.name("caffeine_cache_requests")
141+
.name(METRIC_NAME_CACHE_REQUESTS)
118142
.help("Cache request totals, hits + misses");
119143

120144
final CounterSnapshot.Builder cacheEvictionTotal =
121145
CounterSnapshot.builder()
122-
.name("caffeine_cache_eviction")
146+
.name(METRIC_NAME_CACHE_EVICTION)
123147
.help("Cache eviction totals, doesn't include manually removed entries");
124148

125149
final GaugeSnapshot.Builder cacheEvictionWeight =
126150
GaugeSnapshot.builder()
127-
.name("caffeine_cache_eviction_weight")
151+
.name(METRIC_NAME_CACHE_EVICTION_WEIGHT)
128152
.help("Cache eviction weight");
129153

130154
final CounterSnapshot.Builder cacheLoadFailure =
131-
CounterSnapshot.builder().name("caffeine_cache_load_failure").help("Cache load failures");
155+
CounterSnapshot.builder().name(METRIC_NAME_CACHE_LOAD_FAILURE).help("Cache load failures");
132156

133157
final CounterSnapshot.Builder cacheLoadTotal =
134158
CounterSnapshot.builder()
135-
.name("caffeine_cache_loads")
159+
.name(METRIC_NAME_CACHE_LOADS)
136160
.help("Cache loads: both success and failures");
137161

138162
final GaugeSnapshot.Builder cacheSize =
139-
GaugeSnapshot.builder().name("caffeine_cache_estimated_size").help("Estimated cache size");
163+
GaugeSnapshot.builder().name(METRIC_NAME_CACHE_ESTIMATED_SIZE).help("Estimated cache size");
140164

141165
final SummarySnapshot.Builder cacheLoadSummary =
142166
SummarySnapshot.builder()
143-
.name("caffeine_cache_load_duration_seconds")
167+
.name(METRIC_NAME_CACHE_LOAD_DURATION_SECONDS)
144168
.help("Cache load duration: both success and failures");
145169

146170
for (final Map.Entry<String, Cache<?, ?>> c : children.entrySet()) {
@@ -223,4 +247,9 @@ public MetricSnapshots collect() {
223247
.metricSnapshot(cacheLoadSummary.build())
224248
.build();
225249
}
250+
251+
@Override
252+
public List<String> getPrometheusNames() {
253+
return ALL_METRIC_NAMES;
254+
}
226255
}

prometheus-metrics-instrumentation-caffeine/src/test/java/io/prometheus/metrics/instrumentation/caffeine/CacheMetricsCollectorTest.java

+30
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
1515
import io.prometheus.metrics.model.snapshots.DataPointSnapshot;
1616
import io.prometheus.metrics.model.snapshots.Labels;
17+
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
1718
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
1819
import java.io.ByteArrayOutputStream;
1920
import java.io.IOException;
2021
import java.io.UncheckedIOException;
2122
import java.nio.charset.StandardCharsets;
23+
import java.util.List;
2224
import org.junit.jupiter.api.Test;
2325

2426
@SuppressWarnings("CheckReturnValue")
@@ -115,6 +117,34 @@ public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
115117
assertThat(loadDuration.getSum()).isGreaterThan(0);
116118
}
117119

120+
@Test
121+
public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() {
122+
final CacheMetricsCollector collector = new CacheMetricsCollector();
123+
124+
final PrometheusRegistry registry = new PrometheusRegistry();
125+
registry.register(collector);
126+
127+
final MetricSnapshots metricSnapshots = registry.scrape();
128+
final List<String> prometheusNames = collector.getPrometheusNames();
129+
130+
assertThat(prometheusNames).hasSize(metricSnapshots.size());
131+
}
132+
133+
@Test
134+
public void collectedMetricNamesAreKnownPrometheusNames() {
135+
final CacheMetricsCollector collector = new CacheMetricsCollector();
136+
137+
final PrometheusRegistry registry = new PrometheusRegistry();
138+
registry.register(collector);
139+
140+
final MetricSnapshots metricSnapshots = registry.scrape();
141+
final List<String> prometheusNames = collector.getPrometheusNames();
142+
143+
metricSnapshots.forEach(
144+
metricSnapshot ->
145+
assertThat(prometheusNames).contains(metricSnapshot.getMetadata().getPrometheusName()));
146+
}
147+
118148
private void assertCounterMetric(
119149
PrometheusRegistry registry, String name, String cacheName, double value) {
120150
final CounterSnapshot.CounterDataPointSnapshot dataPointSnapshot =

0 commit comments

Comments
 (0)