From 03609ebeff864e3208275db3f0105e2db3e62a54 Mon Sep 17 00:00:00 2001 From: Petar Heyken <48122009+pheyken@users.noreply.github.com> Date: Fri, 22 Nov 2024 09:12:48 +0100 Subject: [PATCH] [prometheus-metrics-instrumentation-guava] implement getPrometheusNames (#1211) Signed-off-by: Petar Heyken --- .../guava/CacheMetricsCollector.java | 44 +++++++++++++++---- .../guava/CacheMetricsCollectorTest.java | 30 +++++++++++++ 2 files changed, 66 insertions(+), 8 deletions(-) diff --git a/prometheus-metrics-instrumentation-guava/src/main/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollector.java b/prometheus-metrics-instrumentation-guava/src/main/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollector.java index 2962b0bf3..42473c686 100644 --- a/prometheus-metrics-instrumentation-guava/src/main/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollector.java +++ b/prometheus-metrics-instrumentation-guava/src/main/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollector.java @@ -9,6 +9,7 @@ import io.prometheus.metrics.model.snapshots.Labels; import io.prometheus.metrics.model.snapshots.MetricSnapshots; import io.prometheus.metrics.model.snapshots.SummarySnapshot; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Map; @@ -54,6 +55,28 @@ public class CacheMetricsCollector implements MultiCollector { private static final double NANOSECONDS_PER_SECOND = 1_000_000_000.0; + private static final String METRIC_NAME_CACHE_HIT = "guava_cache_hit"; + private static final String METRIC_NAME_CACHE_MISS = "guava_cache_miss"; + private static final String METRIC_NAME_CACHE_REQUESTS = "guava_cache_requests"; + private static final String METRIC_NAME_CACHE_EVICTION = "guava_cache_eviction"; + private static final String METRIC_NAME_CACHE_LOAD_FAILURE = "guava_cache_load_failure"; + private static final String METRIC_NAME_CACHE_LOADS = "guava_cache_loads"; + private static final String METRIC_NAME_CACHE_SIZE = "guava_cache_size"; + private static final String METRIC_NAME_CACHE_LOAD_DURATION_SECONDS = + "guava_cache_load_duration_seconds"; + + private static final List ALL_METRIC_NAMES = + Collections.unmodifiableList( + Arrays.asList( + METRIC_NAME_CACHE_HIT, + METRIC_NAME_CACHE_MISS, + METRIC_NAME_CACHE_REQUESTS, + METRIC_NAME_CACHE_EVICTION, + METRIC_NAME_CACHE_LOAD_FAILURE, + METRIC_NAME_CACHE_LOADS, + METRIC_NAME_CACHE_SIZE, + METRIC_NAME_CACHE_LOAD_DURATION_SECONDS)); + protected final ConcurrentMap> children = new ConcurrentHashMap<>(); /** @@ -94,33 +117,33 @@ public MetricSnapshots collect() { final List labelNames = Collections.singletonList("cache"); final CounterSnapshot.Builder cacheHitTotal = - CounterSnapshot.builder().name("guava_cache_hit").help("Cache hit totals"); + CounterSnapshot.builder().name(METRIC_NAME_CACHE_HIT).help("Cache hit totals"); final CounterSnapshot.Builder cacheMissTotal = - CounterSnapshot.builder().name("guava_cache_miss").help("Cache miss totals"); + CounterSnapshot.builder().name(METRIC_NAME_CACHE_MISS).help("Cache miss totals"); final CounterSnapshot.Builder cacheRequestsTotal = - CounterSnapshot.builder().name("guava_cache_requests").help("Cache request totals"); + CounterSnapshot.builder().name(METRIC_NAME_CACHE_REQUESTS).help("Cache request totals"); final CounterSnapshot.Builder cacheEvictionTotal = CounterSnapshot.builder() - .name("guava_cache_eviction") + .name(METRIC_NAME_CACHE_EVICTION) .help("Cache eviction totals, doesn't include manually removed entries"); final CounterSnapshot.Builder cacheLoadFailure = - CounterSnapshot.builder().name("guava_cache_load_failure").help("Cache load failures"); + CounterSnapshot.builder().name(METRIC_NAME_CACHE_LOAD_FAILURE).help("Cache load failures"); final CounterSnapshot.Builder cacheLoadTotal = CounterSnapshot.builder() - .name("guava_cache_loads") + .name(METRIC_NAME_CACHE_LOADS) .help("Cache loads: both success and failures"); final GaugeSnapshot.Builder cacheSize = - GaugeSnapshot.builder().name("guava_cache_size").help("Cache size"); + GaugeSnapshot.builder().name(METRIC_NAME_CACHE_SIZE).help("Cache size"); final SummarySnapshot.Builder cacheLoadSummary = SummarySnapshot.builder() - .name("guava_cache_load_duration_seconds") + .name(METRIC_NAME_CACHE_LOAD_DURATION_SECONDS) .help("Cache load duration: both success and failures"); for (final Map.Entry> c : children.entrySet()) { @@ -192,4 +215,9 @@ public MetricSnapshots collect() { return metricSnapshotsBuilder.build(); } + + @Override + public List getPrometheusNames() { + return ALL_METRIC_NAMES; + } } diff --git a/prometheus-metrics-instrumentation-guava/src/test/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollectorTest.java b/prometheus-metrics-instrumentation-guava/src/test/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollectorTest.java index b3d67502f..a03fcdb0c 100644 --- a/prometheus-metrics-instrumentation-guava/src/test/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollectorTest.java +++ b/prometheus-metrics-instrumentation-guava/src/test/java/io/prometheus/metrics/instrumentation/guava/CacheMetricsCollectorTest.java @@ -14,11 +14,13 @@ import io.prometheus.metrics.model.snapshots.CounterSnapshot; import io.prometheus.metrics.model.snapshots.DataPointSnapshot; import io.prometheus.metrics.model.snapshots.Labels; +import io.prometheus.metrics.model.snapshots.MetricSnapshots; import io.prometheus.metrics.model.snapshots.SummarySnapshot; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.charset.StandardCharsets; +import java.util.List; import org.junit.jupiter.api.Test; class CacheMetricsCollectorTest { @@ -110,6 +112,34 @@ public void loadingCacheExposesMetricsForLoadsAndExceptions() throws Exception { assertThat(loadDuration.getSum()).isGreaterThan(0); } + @Test + public void getPrometheusNamesHasSameSizeAsMetricSizeWhenScraping() { + final CacheMetricsCollector collector = new CacheMetricsCollector(); + + final PrometheusRegistry registry = new PrometheusRegistry(); + registry.register(collector); + + final MetricSnapshots metricSnapshots = registry.scrape(); + final List prometheusNames = collector.getPrometheusNames(); + + assertThat(prometheusNames).hasSize(metricSnapshots.size()); + } + + @Test + public void collectedMetricNamesAreKnownPrometheusNames() { + final CacheMetricsCollector collector = new CacheMetricsCollector(); + + final PrometheusRegistry registry = new PrometheusRegistry(); + registry.register(collector); + + final MetricSnapshots metricSnapshots = registry.scrape(); + final List prometheusNames = collector.getPrometheusNames(); + + metricSnapshots.forEach( + metricSnapshot -> + assertThat(prometheusNames).contains(metricSnapshot.getMetadata().getPrometheusName())); + } + private void assertCounterMetric( PrometheusRegistry registry, String name, String cacheName, double value) { final CounterSnapshot.CounterDataPointSnapshot dataPointSnapshot =