Skip to content

Commit b5ea592

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

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

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

+6-11
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,25 +105,20 @@ 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

126-
private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange) throws IOException {
120+
private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange) throws
121+
IOException {
127122
String debugParam = exchange.getRequest().getParameter("debug");
128123
PrometheusHttpResponse response = exchange.getResponse();
129124
if (debugParam == null) {

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.sun.net.httpserver.HttpsServer;
99
import io.prometheus.metrics.config.ExporterHttpServerProperties;
1010
import io.prometheus.metrics.config.PrometheusProperties;
11+
import io.prometheus.metrics.model.registry.Collector;
1112
import io.prometheus.metrics.model.registry.PrometheusRegistry;
1213

1314
import java.io.Closeable;
@@ -46,7 +47,7 @@ public class HTTPServer implements Closeable {
4647
protected final HttpServer server;
4748
protected final ExecutorService executorService;
4849

49-
private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, PrometheusRegistry registry, Authenticator authenticator, HttpHandler defaultHandler) {
50+
private HTTPServer(PrometheusProperties config, ExecutorService executorService, HttpServer httpServer, Collector registry, Authenticator authenticator, HttpHandler defaultHandler) {
5051
if (httpServer.getAddress() == null) {
5152
throw new IllegalArgumentException("HttpServer hasn't been bound to an address");
5253
}
@@ -104,7 +105,7 @@ public static class Builder {
104105
private String hostname = null;
105106
private InetAddress inetAddress = null;
106107
private ExecutorService executorService = null;
107-
private PrometheusRegistry registry = null;
108+
private Collector registry = null;
108109
private Authenticator authenticator = null;
109110
private HttpsConfigurator httpsConfigurator = null;
110111
private HttpHandler defaultHandler = null;
@@ -153,7 +154,7 @@ public Builder executorService(ExecutorService executorService) {
153154
/**
154155
* Optional: Default is {@link PrometheusRegistry#defaultRegistry}.
155156
*/
156-
public Builder registry(PrometheusRegistry registry) {
157+
public Builder registry(Collector registry) {
157158
this.registry = registry;
158159
return this;
159160
}

Diff for: 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

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

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class MetricNameFilter implements Predicate<String> {
1414

1515
/**
1616
* For convenience, a filter that allows all names.
17+
* TODO: either Builder.build() should return Predicate<String>, or this one should be MetricNameFilter
1718
*/
1819
public static final Predicate<String> ALLOW_ALL = name -> true;
1920

@@ -190,6 +191,7 @@ public Builder nameMustNotStartWith(Collection<String> prefixes) {
190191
}
191192

192193
public MetricNameFilter build() {
194+
// TODO: should return MetricNameFilter.ALLOW_ALL if no filtering is needed
193195
return new MetricNameFilter(nameEqualTo, nameNotEqualTo, nameStartsWith, nameDoesNotStartWith);
194196
}
195197
}

0 commit comments

Comments
 (0)