Skip to content

Commit c454b3f

Browse files
committed
HTTPServer using Collector rather than CollectorRegistry.
Signed-off-by: Marcin Kielar <[email protected]>
1 parent 879c93b commit c454b3f

File tree

4 files changed

+10
-15
lines changed

4 files changed

+10
-15
lines changed

prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void handleRequest(PrometheusHttpExchange exchange) throws IOException {
9393

9494
private Predicate<String> makeNameFilter(ExporterFilterProperties props) {
9595
if (props.getAllowedMetricNames() == null && props.getExcludedMetricNames() == null && props.getAllowedMetricNamePrefixes() == null && props.getExcludedMetricNamePrefixes() == null) {
96-
return null;
96+
return MetricNameFilter.ALLOW_ALL;
9797
} else {
9898
return MetricNameFilter.builder()
9999
.nameMustBeEqualTo(props.getAllowedMetricNames())
@@ -105,22 +105,16 @@ private Predicate<String> makeNameFilter(ExporterFilterProperties props) {
105105
}
106106

107107
private MetricSnapshots scrape(PrometheusHttpRequest request) {
108-
109108
Predicate<String> filter = makeNameFilter(request.getParameterValues("name[]"));
110109
return registry.collect(filter, request);
111110
}
112111

113112
private Predicate<String> makeNameFilter(String[] includedNames) {
114-
Predicate<String> result = MetricNameFilter.ALLOW_ALL;
115113
if (includedNames != null && includedNames.length > 0) {
116-
result = MetricNameFilter.builder().nameMustBeEqualTo(includedNames).build();
117-
}
118-
if (result != null && nameFilter != null) {
119-
result = result.and(nameFilter);
120-
} else if (nameFilter != null) {
121-
result = nameFilter;
114+
return nameFilter.and(MetricNameFilter.builder().nameMustBeEqualTo(includedNames).build());
115+
} else {
116+
return nameFilter;
122117
}
123-
return result;
124118
}
125119

126120
private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange) throws IOException {

prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/HTTPServer.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import com.sun.net.httpserver.HttpServer;
77
import com.sun.net.httpserver.HttpsConfigurator;
88
import com.sun.net.httpserver.HttpsServer;
9-
import io.prometheus.metrics.config.ExporterHttpServerProperties;
109
import io.prometheus.metrics.config.PrometheusProperties;
10+
import io.prometheus.metrics.model.registry.Collector;
1111
import io.prometheus.metrics.model.registry.PrometheusRegistry;
1212

1313
import java.io.Closeable;
@@ -46,7 +46,7 @@ public class HTTPServer implements Closeable {
4646
protected final HttpServer server;
4747
protected final ExecutorService executorService;
4848

49-
private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, PrometheusRegistry registry, Authenticator authenticator, HttpHandler defaultHandler) {
49+
private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, Collector registry, Authenticator authenticator, HttpHandler defaultHandler) {
5050
if (httpServer.getAddress() == null) {
5151
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");
5252
}
@@ -104,7 +104,7 @@ public static class Builder {
104104
private String hostname = null;
105105
private InetAddress inetAddress = null;
106106
private ExecutorService executorService = null;
107-
private PrometheusRegistry registry = null;
107+
private Collector registry = null;
108108
private Authenticator authenticator = null;
109109
private HttpsConfigurator httpsConfigurator = null;
110110
private HttpHandler defaultHandler = null;
@@ -153,7 +153,7 @@ public Builder executorService(ExecutorService executorService) {
153153
/**
154154
* Optional: Default is {@link PrometheusRegistry#defaultRegistry}.
155155
*/
156-
public Builder registry(PrometheusRegistry registry) {
156+
public Builder registry(Collector registry) {
157157
this.registry = registry;
158158
return this;
159159
}

prometheus-metrics-exporter-httpserver/src/main/java/io/prometheus/metrics/exporter/httpserver/MetricsHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MetricsHandler(PrometheusProperties config) {
3535
prometheusScrapeHandler = new PrometheusScrapeHandler(config);
3636
}
3737

38-
public MetricsHandler(PrometheusProperties config, PrometheusRegistry registry) {
38+
public MetricsHandler(PrometheusProperties config, Collector registry) {
3939
prometheusScrapeHandler = new PrometheusScrapeHandler(config, registry);
4040
}
4141

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/MetricNameFilter.java

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public Builder nameMustNotStartWith(Collection<String> prefixes) {
190190
}
191191

192192
public MetricNameFilter build() {
193+
// TODO: should return MetricNameFilter.ALLOW_ALL if no filtering is needed
193194
return new MetricNameFilter(nameEqualTo, nameNotEqualTo, nameStartsWith, nameDoesNotStartWith);
194195
}
195196
}

0 commit comments

Comments
 (0)