Skip to content

Commit

Permalink
HTTPServer using Collector rather than CollectorRegistry.
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Kielar <[email protected]>
  • Loading branch information
zorba128 committed Dec 8, 2023
1 parent 879c93b commit c454b3f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void handleRequest(PrometheusHttpExchange exchange) throws IOException {

private Predicate<String> 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())
Expand All @@ -105,22 +105,16 @@ private Predicate<String> makeNameFilter(ExporterFilterProperties props) {
}

private MetricSnapshots scrape(PrometheusHttpRequest request) {

Predicate<String> filter = makeNameFilter(request.getParameterValues("name[]"));
return registry.collect(filter, request);
}

private Predicate<String> makeNameFilter(String[] includedNames) {
Predicate<String> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
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;
Expand Down Expand Up @@ -46,7 +46,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");
}
Expand Down Expand Up @@ -104,7 +104,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;
Expand Down Expand Up @@ -153,7 +153,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public Builder nameMustNotStartWith(Collection<String> prefixes) {
}

public MetricNameFilter build() {
// TODO: should return MetricNameFilter.ALLOW_ALL if no filtering is needed
return new MetricNameFilter(nameEqualTo, nameNotEqualTo, nameStartsWith, nameDoesNotStartWith);
}
}
Expand Down

0 comments on commit c454b3f

Please sign in to comment.