Skip to content

Commit c3dc1f2

Browse files
authored
Added health check api to the client. (#534)
* Added health check api to the client. prometheus/jmx_exporter#465 Signed-off-by: Brajesh Kumar <[email protected]>
1 parent 7259307 commit c3dc1f2

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

simpleclient_httpserver/src/main/java/io/prometheus/client/exporter/HTTPServer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ protected ByteArrayOutputStream initialValue()
4242
}
4343
}
4444

45+
/**
46+
* Handles Metrics collections from the given registry.
47+
*/
4548
static class HTTPMetricHandler implements HttpHandler {
4649
private CollectorRegistry registry;
4750
private final LocalByteArray response = new LocalByteArray();
51+
private final static String HEALTHY_RESPONSE = "Exporter is Healthy.";
4852

4953
HTTPMetricHandler(CollectorRegistry registry) {
5054
this.registry = registry;
@@ -54,16 +58,21 @@ static class HTTPMetricHandler implements HttpHandler {
5458
public void handle(HttpExchange t) throws IOException {
5559
String query = t.getRequestURI().getRawQuery();
5660

61+
String contextPath = t.getHttpContext().getPath();
5762
ByteArrayOutputStream response = this.response.get();
5863
response.reset();
5964
OutputStreamWriter osw = new OutputStreamWriter(response);
60-
TextFormat.write004(osw,
61-
registry.filteredMetricFamilySamples(parseQuery(query)));
65+
if ("/-/healthy".equals(contextPath)) {
66+
osw.write(HEALTHY_RESPONSE);
67+
} else {
68+
TextFormat.write004(osw,
69+
registry.filteredMetricFamilySamples(parseQuery(query)));
70+
}
71+
6272
osw.flush();
6373
osw.close();
6474
response.flush();
6575
response.close();
66-
6776
t.getResponseHeaders().set("Content-Type",
6877
TextFormat.CONTENT_TYPE_004);
6978
if (shouldUseCompression(t)) {
@@ -154,6 +163,7 @@ public HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean dae
154163
HttpHandler mHandler = new HTTPMetricHandler(registry);
155164
server.createContext("/", mHandler);
156165
server.createContext("/metrics", mHandler);
166+
server.createContext("/-/healthy", mHandler);
157167
executorService = Executors.newFixedThreadPool(5, NamedDaemonThreadFactory.defaultThreadFactory(daemon));
158168
server.setExecutor(executorService);
159169
start(daemon);

simpleclient_httpserver/src/test/java/io/prometheus/client/exporter/TestHTTPServer.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,26 @@ public void cleanup() {
3535
s.stop();
3636
}
3737

38-
String request(String suffix) throws IOException {
39-
String url = "http://localhost:" + s.server.getAddress().getPort() + "/metrics" + suffix;
38+
39+
String request(String context, String suffix) throws IOException {
40+
String url = "http://localhost:" + s.server.getAddress().getPort() + context + suffix;
4041
URLConnection connection = new URL(url).openConnection();
4142
connection.setDoOutput(true);
4243
connection.connect();
4344
Scanner s = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A");
4445
return s.hasNext() ? s.next() : "";
4546
}
4647

48+
String request(String suffix) throws IOException {
49+
return request("/metrics", suffix);
50+
}
51+
4752
String requestWithCompression(String suffix) throws IOException {
48-
String url = "http://localhost:" + s.server.getAddress().getPort() + "/metrics" + suffix;
53+
return requestWithCompression("/metrics", suffix);
54+
}
55+
56+
String requestWithCompression(String context, String suffix) throws IOException {
57+
String url = "http://localhost:" + s.server.getAddress().getPort() + context + suffix;
4958
URLConnection connection = new URL(url).openConnection();
5059
connection.setDoOutput(true);
5160
connection.setDoInput(true);
@@ -63,8 +72,8 @@ public void testUnbound() throws IOException {
6372
HTTPServer s = new HTTPServer(HttpServer.create(), registry, true);
6473
s.stop();
6574
fail("Should refuse to use an unbound HttpServer");
75+
} catch (IllegalArgumentException expected) {
6676
}
67-
catch (IllegalArgumentException expected) {}
6877
}
6978

7079
@Test
@@ -114,4 +123,16 @@ public void testGzipCompression() throws IOException {
114123
assertThat(response).contains("b 0.0");
115124
assertThat(response).contains("c 0.0");
116125
}
126+
127+
@Test
128+
public void testHealth() throws IOException {
129+
String response = request("/-/healthy", "");
130+
assertThat(response).contains("Exporter is Healthy");
131+
}
132+
133+
@Test
134+
public void testHealthGzipCompression() throws IOException {
135+
String response = requestWithCompression("/-/healthy", "");
136+
assertThat(response).contains("Exporter is Healthy");
137+
}
117138
}

0 commit comments

Comments
 (0)