Skip to content

Commit

Permalink
Added health check api to the client. (#534)
Browse files Browse the repository at this point in the history
* Added health check api to the client.
prometheus/jmx_exporter#465

Signed-off-by: Brajesh Kumar <brjkumar@amazon.com>
  • Loading branch information
bristy committed Feb 28, 2020
1 parent 7259307 commit c3dc1f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ protected ByteArrayOutputStream initialValue()
}
}

/**
* Handles Metrics collections from the given registry.
*/
static class HTTPMetricHandler implements HttpHandler {
private CollectorRegistry registry;
private final LocalByteArray response = new LocalByteArray();
private final static String HEALTHY_RESPONSE = "Exporter is Healthy.";

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

String contextPath = t.getHttpContext().getPath();
ByteArrayOutputStream response = this.response.get();
response.reset();
OutputStreamWriter osw = new OutputStreamWriter(response);
TextFormat.write004(osw,
registry.filteredMetricFamilySamples(parseQuery(query)));
if ("/-/healthy".equals(contextPath)) {
osw.write(HEALTHY_RESPONSE);
} else {
TextFormat.write004(osw,
registry.filteredMetricFamilySamples(parseQuery(query)));
}

osw.flush();
osw.close();
response.flush();
response.close();

t.getResponseHeaders().set("Content-Type",
TextFormat.CONTENT_TYPE_004);
if (shouldUseCompression(t)) {
Expand Down Expand Up @@ -154,6 +163,7 @@ public HTTPServer(HttpServer httpServer, CollectorRegistry registry, boolean dae
HttpHandler mHandler = new HTTPMetricHandler(registry);
server.createContext("/", mHandler);
server.createContext("/metrics", mHandler);
server.createContext("/-/healthy", mHandler);
executorService = Executors.newFixedThreadPool(5, NamedDaemonThreadFactory.defaultThreadFactory(daemon));
server.setExecutor(executorService);
start(daemon);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,26 @@ public void cleanup() {
s.stop();
}

String request(String suffix) throws IOException {
String url = "http://localhost:" + s.server.getAddress().getPort() + "/metrics" + suffix;

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

String request(String suffix) throws IOException {
return request("/metrics", suffix);
}

String requestWithCompression(String suffix) throws IOException {
String url = "http://localhost:" + s.server.getAddress().getPort() + "/metrics" + suffix;
return requestWithCompression("/metrics", suffix);
}

String requestWithCompression(String context, String suffix) throws IOException {
String url = "http://localhost:" + s.server.getAddress().getPort() + context + suffix;
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
Expand All @@ -63,8 +72,8 @@ public void testUnbound() throws IOException {
HTTPServer s = new HTTPServer(HttpServer.create(), registry, true);
s.stop();
fail("Should refuse to use an unbound HttpServer");
} catch (IllegalArgumentException expected) {
}
catch (IllegalArgumentException expected) {}
}

@Test
Expand Down Expand Up @@ -114,4 +123,16 @@ public void testGzipCompression() throws IOException {
assertThat(response).contains("b 0.0");
assertThat(response).contains("c 0.0");
}

@Test
public void testHealth() throws IOException {
String response = request("/-/healthy", "");
assertThat(response).contains("Exporter is Healthy");
}

@Test
public void testHealthGzipCompression() throws IOException {
String response = requestWithCompression("/-/healthy", "");
assertThat(response).contains("Exporter is Healthy");
}
}

0 comments on commit c3dc1f2

Please sign in to comment.