Skip to content

Commit 03609eb

Browse files
authored
[prometheus-metrics-instrumentation-guava] implement getPrometheusNames (#1211)
Signed-off-by: Petar Heyken <[email protected]>
1 parent 12fe8ae commit 03609eb

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.prometheus.metrics.model.snapshots.Labels;
1010
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
1111
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
12+
import java.util.Arrays;
1213
import java.util.Collections;
1314
import java.util.List;
1415
import java.util.Map;
@@ -54,6 +55,28 @@ public class CacheMetricsCollector implements MultiCollector {
5455

5556
private static final double NANOSECONDS_PER_SECOND = 1_000_000_000.0;
5657

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+
5780
protected final ConcurrentMap<String, Cache<?, ?>> children = new ConcurrentHashMap<>();
5881

5982
/**
@@ -94,33 +117,33 @@ public MetricSnapshots collect() {
94117
final List<String> labelNames = Collections.singletonList("cache");
95118

96119
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");
98121

99122
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");
101124

102125
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");
104127

105128
final CounterSnapshot.Builder cacheEvictionTotal =
106129
CounterSnapshot.builder()
107-
.name("guava_cache_eviction")
130+
.name(METRIC_NAME_CACHE_EVICTION)
108131
.help("Cache eviction totals, doesn't include manually removed entries");
109132

110133
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");
112135

113136
final CounterSnapshot.Builder cacheLoadTotal =
114137
CounterSnapshot.builder()
115-
.name("guava_cache_loads")
138+
.name(METRIC_NAME_CACHE_LOADS)
116139
.help("Cache loads: both success and failures");
117140

118141
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");
120143

121144
final SummarySnapshot.Builder cacheLoadSummary =
122145
SummarySnapshot.builder()
123-
.name("guava_cache_load_duration_seconds")
146+
.name(METRIC_NAME_CACHE_LOAD_DURATION_SECONDS)
124147
.help("Cache load duration: both success and failures");
125148

126149
for (final Map.Entry<String, Cache<?, ?>> c : children.entrySet()) {
@@ -192,4 +215,9 @@ public MetricSnapshots collect() {
192215

193216
return metricSnapshotsBuilder.build();
194217
}
218+
219+
@Override
220+
public List<String> getPrometheusNames() {
221+
return ALL_METRIC_NAMES;
222+
}
195223
}

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

Lines changed: 30 additions & 0 deletions
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
class CacheMetricsCollectorTest {
@@ -110,6 +112,34 @@ public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception {
110112
assertThat(loadDuration.getSum()).isGreaterThan(0);
111113
}
112114

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

0 commit comments

Comments
 (0)