From c7188c1ea7d6162986e39c52a9e94d1e56671c8a Mon Sep 17 00:00:00 2001 From: David Chan Date: Sun, 28 Aug 2022 13:00:31 -0400 Subject: [PATCH] Check if metric exists in scope when querying across scopes --- .../java/io/smallrye/metrics/MetricsRequestHandler.java | 6 ++++++ .../metrics/exporters/PrometheusMetricsExporter.java | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java b/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java index 4aff065d..90d1c042 100644 --- a/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java +++ b/implementation/src/main/java/io/smallrye/metrics/MetricsRequestHandler.java @@ -176,6 +176,12 @@ else if (scope != null && metricName != null) { */ else if (scope == null && metricName != null) { output = exporter.exportOneMetricAcrossScopes(metricName); + + if (output == null || output.isEmpty() || output.length() == 0) { + responder.respondWith(404, "Metric " + metricName + " not found in any scope ", + Collections.emptyMap()); + return; + } } /* * Something went wrong :( diff --git a/implementation/src/main/java/io/smallrye/metrics/exporters/PrometheusMetricsExporter.java b/implementation/src/main/java/io/smallrye/metrics/exporters/PrometheusMetricsExporter.java index d88103ae..fedfdf76 100644 --- a/implementation/src/main/java/io/smallrye/metrics/exporters/PrometheusMetricsExporter.java +++ b/implementation/src/main/java/io/smallrye/metrics/exporters/PrometheusMetricsExporter.java @@ -92,6 +92,12 @@ public String exportOneMetricAcrossScopes(String name) { StringBuilder sb = new StringBuilder(); for (MeterRegistry meterRegistry : prometheusRegistryList) { PrometheusMeterRegistry promMeterRegistry = (PrometheusMeterRegistry) meterRegistry; + + //Skip scrape if meter doesn't contain this metric + if (promMeterRegistry.find(name).meter() == null) { + continue; + } + /* * For each Prometheus registry found: * 1. Calculate potential formatted names