forked from prometheus/client_java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrometheusScrapeHandler.java
175 lines (159 loc) · 7.63 KB
/
PrometheusScrapeHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package io.prometheus.metrics.exporter.common;
import io.prometheus.metrics.config.ExporterFilterProperties;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
import io.prometheus.metrics.expositionformats.ExpositionFormats;
import io.prometheus.metrics.model.registry.MetricNameFilter;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.zip.GZIPOutputStream;
/**
* Prometheus scrape endpoint.
*/
public class PrometheusScrapeHandler {
private final PrometheusRegistry registry;
private final ExpositionFormats expositionFormats;
private final Predicate<String> nameFilter;
private AtomicInteger lastResponseSize = new AtomicInteger(2 << 9); // 0.5 MB
public PrometheusScrapeHandler() {
this(PrometheusProperties.get(), PrometheusRegistry.defaultRegistry);
}
public PrometheusScrapeHandler(PrometheusRegistry registry) {
this(PrometheusProperties.get(), registry);
}
public PrometheusScrapeHandler(PrometheusProperties config) {
this(config, PrometheusRegistry.defaultRegistry);
}
public PrometheusScrapeHandler(PrometheusProperties config, PrometheusRegistry registry) {
this.expositionFormats = ExpositionFormats.init(config.getExporterProperties());
this.registry = registry;
this.nameFilter = makeNameFilter(config.getExporterFilterProperties());
}
public void handleRequest(PrometheusHttpExchange exchange) throws IOException {
try {
PrometheusHttpRequest request = exchange.getRequest();
PrometheusHttpResponse response = exchange.getResponse();
MetricSnapshots snapshots = scrape(request);
if (writeDebugResponse(snapshots, exchange)) {
return;
}
ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream(lastResponseSize.get() + 1024);
String acceptHeader = request.getHeader("Accept");
ExpositionFormatWriter writer = expositionFormats.findWriter(acceptHeader);
writer.write(responseBuffer, snapshots);
lastResponseSize.set(responseBuffer.size());
response.setHeader("Content-Type", writer.getContentType());
if (shouldUseCompression(request)) {
response.setHeader("Content-Encoding", "gzip");
try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(response.sendHeadersAndGetBody(200, 0))) {
responseBuffer.writeTo(gzipOutputStream);
}
} else {
int contentLength = responseBuffer.size();
if (contentLength > 0) {
response.setHeader("Content-Length", String.valueOf(contentLength));
}
if (request.getMethod().equals("HEAD")) {
// The HTTPServer implementation will throw an Exception if we close the output stream
// without sending a response body, so let's not close the output stream in case of a HEAD response.
response.sendHeadersAndGetBody(200, -1);
} else {
try (OutputStream outputStream = response.sendHeadersAndGetBody(200, contentLength)) {
responseBuffer.writeTo(outputStream);
}
}
}
} catch (IOException e) {
exchange.handleException(e);
} catch (RuntimeException e) {
exchange.handleException(e);
} finally {
exchange.close();
}
}
private Predicate<String> makeNameFilter(ExporterFilterProperties props) {
if (props.getAllowedMetricNames() == null && props.getExcludedMetricNames() == null && props.getAllowedMetricNamePrefixes() == null && props.getExcludedMetricNamePrefixes() == null) {
return null;
} else {
return MetricNameFilter.builder()
.nameMustBeEqualTo(props.getAllowedMetricNames())
.nameMustNotBeEqualTo(props.getExcludedMetricNames())
.nameMustStartWith(props.getAllowedMetricNamePrefixes())
.nameMustNotStartWith(props.getExcludedMetricNamePrefixes())
.build();
}
}
private MetricSnapshots scrape(PrometheusHttpRequest request) {
PrometheusHttpScrapeRequest scrapeRequest = new PrometheusHttpScrapeRequest(request);
Predicate<String> filter = makeNameFilter(request.getParameterValues("name[]"));
if (filter != null) {
return registry.scrape(filter, scrapeRequest);
} else {
return registry.scrape(scrapeRequest);
}
}
private Predicate<String> makeNameFilter(String[] includedNames) {
Predicate<String> result = null;
if (includedNames != null) {
result = MetricNameFilter.builder().nameMustBeEqualTo(includedNames).build();
}
if (result != null && nameFilter != null) {
result = result.and(nameFilter);
} else if (nameFilter != null) {
result = nameFilter;
}
return result;
}
private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange) throws IOException {
String debugParam = exchange.getRequest().getParameter("debug");
PrometheusHttpResponse response = exchange.getResponse();
if (debugParam == null) {
return false;
} else {
response.setHeader("Content-Type", "text/plain; charset=utf-8");
boolean supportedFormat = Arrays.asList("openmetrics", "text", "prometheus-protobuf").contains(debugParam);
int responseStatus = supportedFormat ? 200 : 500;
OutputStream body = response.sendHeadersAndGetBody(responseStatus, 0);
switch (debugParam) {
case "openmetrics":
expositionFormats.getOpenMetricsTextFormatWriter().write(body, snapshots);
break;
case "text":
expositionFormats.getPrometheusTextFormatWriter().write(body, snapshots);
break;
case "prometheus-protobuf":
String debugString = expositionFormats.getPrometheusProtobufWriter().toDebugString(snapshots);
body.write(debugString.getBytes(StandardCharsets.UTF_8));
break;
default:
body.write(("debug=" + debugParam + ": Unsupported query parameter. Valid values are 'openmetrics', 'text', and 'prometheus-protobuf'.").getBytes(StandardCharsets.UTF_8));
break;
}
return true;
}
}
private boolean shouldUseCompression(PrometheusHttpRequest request) {
Enumeration<String> encodingHeaders = request.getHeaders("Accept-Encoding");
if (encodingHeaders == null) {
return false;
}
while (encodingHeaders.hasMoreElements()) {
String encodingHeader = encodingHeaders.nextElement();
String[] encodings = encodingHeader.split(",");
for (String encoding : encodings) {
if (encoding.trim().equalsIgnoreCase("gzip")) {
return true;
}
}
}
return false;
}
}