Skip to content

Commit

Permalink
Initial App Status implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebr72 committed Aug 21, 2024
1 parent 40889e2 commit 602743c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.mapfish.print.metrics;

import com.codahale.metrics.health.HealthCheck;
import org.mapfish.print.servlet.job.JobQueue;
import org.springframework.beans.factory.annotation.Autowired;

class ApplicationStatus extends HealthCheck {
public static final int MAX_WAITING_JOBS_TILL_UNHEALTHY = 5;
@Autowired private JobQueue jobQueue;

@Override
protected Result check() throws Exception {
long waitingJobsCount = jobQueue.getWaitingJobsCount();
String health = "Number of jobs waiting is " + waitingJobsCount;

if (waitingJobsCount >= MAX_WAITING_JOBS_TILL_UNHEALTHY) {
return Result.unhealthy(health + ". It is too high.");
} else {
return Result.healthy(health);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.mapfish.print.metrics;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;

public class HealthCheckRegistry extends com.codahale.metrics.health.HealthCheckRegistry {
@Autowired private ApplicationStatus applicationStatus;

@PostConstruct
public void registerHealthCheck() {
register("application", applicationStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
<bean id="fileReportLoader" class="org.mapfish.print.servlet.job.loader.FileReportLoader"/>

<bean id="metricRegistry" class="com.codahale.metrics.MetricRegistry"/>
<bean id="healthCheckRegistry" class="com.codahale.metrics.health.HealthCheckRegistry"/>
<bean id="applicationStatus" class="org.mapfish.print.metrics.ApplicationStatus"/>
<bean id="healthCheckRegistry" class="org.mapfish.print.metrics.HealthCheckRegistry"/>
<bean id="httpClientFactory" class="org.mapfish.print.http.MfClientHttpRequestFactoryImpl">
<constructor-arg index="0" value="${maxConnectionsTotal}" />
<constructor-arg index="1" value="${maxConnectionsPerRoute}" />
Expand Down
26 changes: 14 additions & 12 deletions examples/src/test/java/org/mapfish/print/MetricsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
Expand All @@ -26,7 +27,7 @@ public class MetricsApiTest extends AbstractApiTest {

@Test
public void testMetrics() throws Exception {
ClientHttpRequest request = getMetricsRequest("metrics", HttpMethod.GET);
ClientHttpRequest request = getMetricsRequest("metrics");
try (ClientHttpResponse response = request.execute()) {
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType());
Expand All @@ -37,7 +38,7 @@ public void testMetrics() throws Exception {

@Test
public void testPing() throws Exception {
ClientHttpRequest request = getMetricsRequest("ping", HttpMethod.GET);
ClientHttpRequest request = getMetricsRequest("ping");
try (ClientHttpResponse response = request.execute()) {
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals("pong", getBodyAsText(response).trim());
Expand All @@ -46,7 +47,7 @@ public void testPing() throws Exception {

@Test
public void testThreads() throws Exception {
ClientHttpRequest request = getMetricsRequest("threads", HttpMethod.GET);
ClientHttpRequest request = getMetricsRequest("threads");
try (ClientHttpResponse response = request.execute()) {
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(MediaType.TEXT_PLAIN, response.getHeaders().getContentType());
Expand All @@ -56,18 +57,19 @@ public void testThreads() throws Exception {

@Test
public void testHealthcheck() throws Exception {
ClientHttpRequest request = getMetricsRequest("healthcheck", HttpMethod.GET);
ClientHttpRequest request = getMetricsRequest("healthcheck");
try (ClientHttpResponse response = request.execute()) {
// TODO not implemented?
assertEquals(HttpStatus.NOT_IMPLEMENTED, response.getStatusCode());
// assertEquals(HttpStatus.OK, response.getStatusCode());
// assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType());
// assertNotNull(new JSONObject(getBodyAsText(response)));
assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(MediaType.APPLICATION_JSON, response.getHeaders().getContentType());
String bodyAsText = getBodyAsText(response);
assertNotNull(bodyAsText);
JSONObject healthcheck = new JSONObject(bodyAsText);
JSONObject application = healthcheck.getJSONObject("application");
assertTrue(application.getBoolean("healthy"));
}
}

private ClientHttpRequest getMetricsRequest(String path, HttpMethod method)
throws IOException, URISyntaxException {
return getRequest("metrics/" + path, method);
private ClientHttpRequest getMetricsRequest(String path) throws IOException, URISyntaxException {
return getRequest("metrics/" + path, HttpMethod.GET);
}
}

0 comments on commit 602743c

Please sign in to comment.