Skip to content

Commit

Permalink
[prometheus-metrics-instrumentation-guava] implement getPrometheusNam…
Browse files Browse the repository at this point in the history
…es (#1211)

Signed-off-by: Petar Heyken <mail@petar-heyken.de>
  • Loading branch information
pheyken authored Nov 22, 2024
1 parent 12fe8ae commit 03609eb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String, Cache<?, ?>> children = new ConcurrentHashMap<>();

/**
Expand Down Expand Up @@ -94,33 +117,33 @@ public MetricSnapshots collect() {
final List<String> 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<String, Cache<?, ?>> c : children.entrySet()) {
Expand Down Expand Up @@ -192,4 +215,9 @@ public MetricSnapshots collect() {

return metricSnapshotsBuilder.build();
}

@Override
public List<String> getPrometheusNames() {
return ALL_METRIC_NAMES;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String> 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<String> 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 =
Expand Down

0 comments on commit 03609eb

Please sign in to comment.