From b5ea592d11ceff63ffb840251ecd58c9fddc9dab Mon Sep 17 00:00:00 2001 From: Marcin Kielar Date: Fri, 8 Dec 2023 15:14:14 +0100 Subject: [PATCH] HTTPServer using Collector rather than CollectorRegistry Signed-off-by: Marcin Kielar --- .../common/PrometheusScrapeHandler.java | 17 ++++++----------- .../metrics/exporter/httpserver/HTTPServer.java | 7 ++++--- .../exporter/httpserver/MetricsHandler.java | 2 +- .../model/registry/MetricNameFilter.java | 2 ++ 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java b/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java index 6260861a3..f3065c30b 100644 --- a/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java +++ b/prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java @@ -93,7 +93,7 @@ public void handleRequest(PrometheusHttpExchange exchange) throws IOException { private Predicate makeNameFilter(ExporterFilterProperties props) { if (props.getAllowedMetricNames() == null && props.getExcludedMetricNames() == null && props.getAllowedMetricNamePrefixes() == null && props.getExcludedMetricNamePrefixes() == null) { - return null; + return MetricNameFilter.ALLOW_ALL; } else { return MetricNameFilter.builder() .nameMustBeEqualTo(props.getAllowedMetricNames()) @@ -105,25 +105,20 @@ private Predicate makeNameFilter(ExporterFilterProperties props) { } private MetricSnapshots scrape(PrometheusHttpRequest request) { - Predicate filter = makeNameFilter(request.getParameterValues("name[]")); return registry.collect(filter, request); } private Predicate makeNameFilter(String[] includedNames) { - Predicate result = MetricNameFilter.ALLOW_ALL; if (includedNames != null && includedNames.length > 0) { - result = MetricNameFilter.builder().nameMustBeEqualTo(includedNames).build(); - } - if (result != null && nameFilter != null) { - result = result.and(nameFilter); - } else if (nameFilter != null) { - result = nameFilter; + return nameFilter.and(MetricNameFilter.builder().nameMustBeEqualTo(includedNames).build()); + } else { + return nameFilter; } - return result; } - private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange) throws IOException { + private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange) throws + IOException { String debugParam = exchange.getRequest().getParameter("debug"); PrometheusHttpResponse response = exchange.getResponse(); if (debugParam == null) { diff --git a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java index 81f8871e4..4bc013223 100644 --- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java +++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java @@ -8,6 +8,7 @@ import com.sun.net.httpserver.HttpsServer; import io.prometheus.metrics.config.ExporterHttpServerProperties; import io.prometheus.metrics.config.PrometheusProperties; +import io.prometheus.metrics.model.registry.Collector; import io.prometheus.metrics.model.registry.PrometheusRegistry; import java.io.Closeable; @@ -46,7 +47,7 @@ public class HTTPServer implements Closeable { protected final HttpServer server; protected final ExecutorService executorService; - private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, PrometheusRegistry registry, Authenticator authenticator, HttpHandler defaultHandler) { + private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, Collector registry, Authenticator authenticator, HttpHandler defaultHandler) { if (httpServer.getAddress() == null) { throw new IllegalArgumentException("HttpServer hasn't been bound to an address"); } @@ -104,7 +105,7 @@ public static class Builder { private String hostname = null; private InetAddress inetAddress = null; private ExecutorService executorService = null; - private PrometheusRegistry registry = null; + private Collector registry = null; private Authenticator authenticator = null; private HttpsConfigurator httpsConfigurator = null; private HttpHandler defaultHandler = null; @@ -153,7 +154,7 @@ public Builder executorService(ExecutorService executorService) { /** * Optional: Default is {@link PrometheusRegistry#defaultRegistry}. */ - public Builder registry(PrometheusRegistry registry) { + public Builder registry(Collector registry) { this.registry = registry; return this; } diff --git a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/MetricsHandler.java b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/MetricsHandler.java index 6a4a56749..827e701e4 100644 --- a/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/MetricsHandler.java +++ b/prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/MetricsHandler.java @@ -35,7 +35,7 @@ public MetricsHandler(PrometheusProperties config) { prometheusScrapeHandler = new PrometheusScrapeHandler(config); } - public MetricsHandler(PrometheusProperties config, PrometheusRegistry registry) { + public MetricsHandler(PrometheusProperties config, Collector registry) { prometheusScrapeHandler = new PrometheusScrapeHandler(config, registry); } diff --git a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MetricNameFilter.java b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MetricNameFilter.java index c0c345a1b..bc0be9596 100644 --- a/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MetricNameFilter.java +++ b/prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MetricNameFilter.java @@ -14,6 +14,7 @@ public class MetricNameFilter implements Predicate { /** * For convenience, a filter that allows all names. + * TODO: either Builder.build() should return Predicate, or this one should be MetricNameFilter */ public static final Predicate ALLOW_ALL = name -> true; @@ -190,6 +191,7 @@ public Builder nameMustNotStartWith(Collection prefixes) { } public MetricNameFilter build() { + // TODO: should return MetricNameFilter.ALLOW_ALL if no filtering is needed return new MetricNameFilter(nameEqualTo, nameNotEqualTo, nameStartsWith, nameDoesNotStartWith); } }