2
2
3
3
import io .prometheus .client .CollectorRegistry ;
4
4
import io .prometheus .client .MetricNameFilter ;
5
+ import io .prometheus .client .Predicate ;
5
6
import io .prometheus .client .exporter .common .TextFormat ;
6
7
7
8
import java .io .ByteArrayOutputStream ;
11
12
import java .net .InetSocketAddress ;
12
13
import java .net .URLDecoder ;
13
14
import java .nio .charset .Charset ;
14
- import java .util .ArrayList ;
15
- import java .util .Collection ;
16
- import java .util .Collections ;
17
15
import java .util .HashSet ;
18
16
import java .util .List ;
19
17
import java .util .Set ;
@@ -65,16 +63,16 @@ protected ByteArrayOutputStream initialValue()
65
63
public static class HTTPMetricHandler implements HttpHandler {
66
64
private final CollectorRegistry registry ;
67
65
private final LocalByteArray response = new LocalByteArray ();
68
- private final Config config ;
66
+ private final Predicate < String > metricNameFilter ;
69
67
private final static String HEALTHY_RESPONSE = "Exporter is Healthy." ;
70
68
71
69
HTTPMetricHandler (CollectorRegistry registry ) {
72
70
this (registry , null );
73
71
}
74
72
75
- HTTPMetricHandler (CollectorRegistry registry , Config config ) {
73
+ HTTPMetricHandler (CollectorRegistry registry , Predicate < String > metricNameFilter ) {
76
74
this .registry = registry ;
77
- this .config = config == null ? new Config () : config ;
75
+ this .metricNameFilter = metricNameFilter ;
78
76
}
79
77
80
78
@ Override
@@ -90,12 +88,12 @@ public void handle(HttpExchange t) throws IOException {
90
88
} else {
91
89
String contentType = TextFormat .chooseContentType (t .getRequestHeaders ().getFirst ("Accept" ));
92
90
t .getResponseHeaders ().set ("Content-Type" , contentType );
93
- MetricNameFilter filter = new MetricNameFilter . Builder ()
94
- . includeNames ( parseQuery ( query ))
95
- . includePrefixes ( config . getIncludedPrefixes ())
96
- . excludePrefixes ( config . getExcludedPrefixes ())
97
- . build ( );
98
- TextFormat . writeFormat ( contentType , osw , registry . filteredMetricFamilySamples ( filter ));
91
+ Predicate < String > filter = composeFilter ( query );
92
+ if ( filter == null ) {
93
+ TextFormat . writeFormat ( contentType , osw , registry . metricFamilySamples ());
94
+ } else {
95
+ TextFormat . writeFormat ( contentType , osw , registry . filteredMetricFamilySamples ( filter ) );
96
+ }
99
97
}
100
98
101
99
osw .close ();
@@ -118,6 +116,21 @@ public void handle(HttpExchange t) throws IOException {
118
116
t .close ();
119
117
}
120
118
119
+ private Predicate <String > composeFilter (String query ) throws IOException {
120
+ Predicate <String > result = this .metricNameFilter ;
121
+ Set <String > includedNames = parseQuery (query );
122
+ if (!includedNames .isEmpty ()) {
123
+ MetricNameFilter filterFromQueryParams = new MetricNameFilter .Builder ()
124
+ .includePrefixes (includedNames )
125
+ .build ();
126
+ if (result == null ) {
127
+ result = filterFromQueryParams ;
128
+ } else {
129
+ result = filterFromQueryParams .and (result );
130
+ }
131
+ }
132
+ return result ;
133
+ }
121
134
}
122
135
123
136
protected static boolean shouldUseCompression (HttpExchange exchange ) {
@@ -179,50 +192,6 @@ static ThreadFactory defaultThreadFactory(boolean daemon) {
179
192
protected final HttpServer server ;
180
193
protected final ExecutorService executorService ;
181
194
182
- /**
183
- * Configure the HTTPServer to include / exclude metrics by name prefix.
184
- */
185
- public static class Config {
186
-
187
- private final Collection <String > includedPrefixes ;
188
- private final Collection <String > excludedPrefixes ;
189
-
190
- /**
191
- * Empty config means nothing is filtered; all metrics are exported.
192
- */
193
- public Config () {
194
- includedPrefixes = Collections .emptyList ();
195
- excludedPrefixes = Collections .emptyList ();
196
- }
197
-
198
- /**
199
- * If {@code includedNamePrefixes} is not empty, only metrics with a name starting with one of these
200
- * prefixes will be exported. If {@code excludedNamePrefixes} is not empty, metrics with a name
201
- * starting with one of these prefixes will not be exported. If both parameters are not empty, metrics
202
- * must match both criteria in order to be exported.
203
- */
204
- public Config (Collection <String > includedNamePrefixes , Collection <String > excludedNamePrefixes ) {
205
- this .includedPrefixes = unmodifiableCopy (includedNamePrefixes );
206
- this .excludedPrefixes = unmodifiableCopy (excludedNamePrefixes );
207
- }
208
-
209
- private Collection <String > unmodifiableCopy (Collection <String > collection ) {
210
- if (collection == null ) {
211
- return Collections .emptyList ();
212
- } else {
213
- return Collections .unmodifiableCollection (new ArrayList <String >(collection ));
214
- }
215
- }
216
-
217
- public Collection <String > getIncludedPrefixes () {
218
- return includedPrefixes ;
219
- }
220
-
221
- public Collection <String > getExcludedPrefixes () {
222
- return excludedPrefixes ;
223
- }
224
- }
225
-
226
195
/**
227
196
* Start an HTTP server serving the default Prometheus registry using non-daemon threads.
228
197
*/
@@ -231,11 +200,11 @@ public HTTPServer(int port) throws IOException {
231
200
}
232
201
233
202
/**
234
- * Like {@link #HTTPServer(int)}, but with an additional {@link Config } parameter to configure
235
- * which metrics should be exported.
203
+ * Like {@link #HTTPServer(int)}, but with an additional {@code metricNameFilter } parameter
204
+ * to configure which metrics should be exported.
236
205
*/
237
- public HTTPServer (int port , Config config ) throws IOException {
238
- this (port , false , config );
206
+ public HTTPServer (int port , Predicate < String > metricNameFilter ) throws IOException {
207
+ this (port , false , metricNameFilter );
239
208
}
240
209
241
210
/**
@@ -246,11 +215,11 @@ public HTTPServer(int port, boolean daemon) throws IOException {
246
215
}
247
216
248
217
/**
249
- * Like {@link #HTTPServer(int, boolean)}, but with an additional {@link Config }
218
+ * Like {@link #HTTPServer(int, boolean)}, but with an additional {@code metricNameFilter }
250
219
* parameter to configure which metrics should be exported.
251
220
*/
252
- public HTTPServer (int port , boolean daemon , Config config ) throws IOException {
253
- this (new InetSocketAddress (port ), CollectorRegistry .defaultRegistry , daemon , config );
221
+ public HTTPServer (int port , boolean daemon , Predicate < String > metricNameFilter ) throws IOException {
222
+ this (new InetSocketAddress (port ), CollectorRegistry .defaultRegistry , daemon , metricNameFilter );
254
223
}
255
224
256
225
/**
@@ -261,11 +230,11 @@ public HTTPServer(String host, int port) throws IOException {
261
230
}
262
231
263
232
/**
264
- * Like {@link #HTTPServer(String, int)}, but with an additional {@link Config }
233
+ * Like {@link #HTTPServer(String, int)}, but with an additional {@code metricNameFilter }
265
234
* parameter to configure which metrics should be exported.
266
235
*/
267
- public HTTPServer (String host , int port , Config config ) throws IOException {
268
- this (host , port , false , config );
236
+ public HTTPServer (String host , int port , Predicate < String > metricNameFilter ) throws IOException {
237
+ this (host , port , false , metricNameFilter );
269
238
}
270
239
271
240
/**
@@ -276,11 +245,11 @@ public HTTPServer(String host, int port, boolean daemon) throws IOException {
276
245
}
277
246
278
247
/**
279
- * Like {@link #HTTPServer(String, int, boolean)}, but with an additional {@link Config }
248
+ * Like {@link #HTTPServer(String, int, boolean)}, but with an additional {@code metricNameFilter }
280
249
* parameter to configure which metrics should be exported.
281
250
*/
282
- public HTTPServer (String host , int port , boolean daemon , Config config ) throws IOException {
283
- this (new InetSocketAddress (host , port ), CollectorRegistry .defaultRegistry , daemon , config );
251
+ public HTTPServer (String host , int port , boolean daemon , Predicate < String > metricNameFilter ) throws IOException {
252
+ this (new InetSocketAddress (host , port ), CollectorRegistry .defaultRegistry , daemon , metricNameFilter );
284
253
}
285
254
286
255
/**
@@ -291,11 +260,11 @@ public HTTPServer(InetSocketAddress addr, CollectorRegistry registry) throws IOE
291
260
}
292
261
293
262
/**
294
- * Like {@link #HTTPServer(InetSocketAddress, CollectorRegistry)}, but with an additional {@link Config }
263
+ * Like {@link #HTTPServer(InetSocketAddress, CollectorRegistry)}, but with an additional {@code metricNameFilter }
295
264
* parameter to configure which metrics should be exported.
296
265
*/
297
- public HTTPServer (InetSocketAddress addr , CollectorRegistry registry , Config config ) throws IOException {
298
- this (addr , registry , false , config );
266
+ public HTTPServer (InetSocketAddress addr , CollectorRegistry registry , Predicate < String > metricNameFilter ) throws IOException {
267
+ this (addr , registry , false , metricNameFilter );
299
268
}
300
269
301
270
/**
@@ -306,11 +275,11 @@ public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean da
306
275
}
307
276
308
277
/**
309
- * Like {@link #HTTPServer(InetSocketAddress, CollectorRegistry, boolean)}, but with an additional {@link Config}
310
- * parameter to configure which metrics should be exported.
278
+ * Like {@link #HTTPServer(InetSocketAddress, CollectorRegistry, boolean)}, but with an additional
279
+ * {@code metricNameFilter} parameter to configure which metrics should be exported.
311
280
*/
312
- public HTTPServer (InetSocketAddress addr , CollectorRegistry registry , boolean daemon , Config config ) throws IOException {
313
- this (HttpServer .create (addr , 3 ), registry , daemon , config );
281
+ public HTTPServer (InetSocketAddress addr , CollectorRegistry registry , boolean daemon , Predicate < String > metricNameFilter ) throws IOException {
282
+ this (HttpServer .create (addr , 3 ), registry , daemon , metricNameFilter );
314
283
}
315
284
316
285
/**
@@ -323,15 +292,15 @@ public HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean dae
323
292
324
293
325
294
/**
326
- * Like {@link #HTTPServer(HttpServer, CollectorRegistry, boolean)}, but with an additional {@link Config }
295
+ * Like {@link #HTTPServer(HttpServer, CollectorRegistry, boolean)}, but with an additional {@code metricNameFilter }
327
296
* parameter to configure which metrics should be exported.
328
297
*/
329
- public HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon , Config config ) throws IOException {
298
+ public HTTPServer (HttpServer httpServer , CollectorRegistry registry , boolean daemon , Predicate < String > metricNameFilter ) throws IOException {
330
299
if (httpServer .getAddress () == null )
331
300
throw new IllegalArgumentException ("HttpServer hasn't been bound to an address" );
332
301
333
302
server = httpServer ;
334
- HttpHandler mHandler = new HTTPMetricHandler (registry , config );
303
+ HttpHandler mHandler = new HTTPMetricHandler (registry , metricNameFilter );
335
304
server .createContext ("/" , mHandler );
336
305
server .createContext ("/metrics" , mHandler );
337
306
server .createContext ("/-/healthy" , mHandler );
0 commit comments