Skip to content

Commit

Permalink
feat(origin health check): add metrics for counting total health chec…
Browse files Browse the repository at this point in the history
…ks sent
  • Loading branch information
kenluluuuluuuuu committed Sep 4, 2024
1 parent ce98188 commit b0080a6
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
*/
public class UrlRequestHealthCheck implements OriginHealthCheckFunction {
private final String healthCheckUri;
private final SimpleCache<Origin, Counter> meterCache;
private final SimpleCache<Origin, Counter> failuresMeter;
private final SimpleCache<Origin, Counter> healthChecksMeter;

/**
* Construct an instance.
Expand All @@ -41,7 +42,8 @@ public class UrlRequestHealthCheck implements OriginHealthCheckFunction {
*/
public UrlRequestHealthCheck(String healthCheckUri, CentralisedMetrics metrics) {
this.healthCheckUri = uriWithInitialSlash(healthCheckUri);
this.meterCache = metrics.proxy().client().originHealthCheckFailures();
this.failuresMeter = metrics.proxy().client().originHealthCheckFailures();
this.healthChecksMeter = metrics.proxy().client().originHealthChecks();
}

private static String uriWithInitialSlash(String uri) {
Expand All @@ -54,15 +56,16 @@ public void check(HttpClient client, Origin origin, OriginHealthCheckFunction.Ca

client.send(request)
.handle((response, cause) -> {
healthChecksMeter.get(origin).increment();
if (response != null) {
if (response.status().equals(OK)) {
responseCallback.originStateResponse(HEALTHY);
} else {
meterCache.get(origin).increment();
failuresMeter.get(origin).increment();
responseCallback.originStateResponse(UNHEALTHY);
}
} else if (cause != null) {
meterCache.get(origin).increment();
failuresMeter.get(origin).increment();
responseCallback.originStateResponse(UNHEALTHY);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void declaresOriginHealthyOnOkResponseCode() {

assertThat(originState, is(HEALTHY));
assertThat(meters(id -> id.getName().equals("proxy.client.originHealthCheckFailures")).size(), is(0));
assertThat(meters(id -> id.getName().equals("proxy.client.originHealthChecks")).size(), is(1));
}

@Test
Expand All @@ -92,6 +93,7 @@ public void declaresOriginUnhealthyOnNon200Ok() {
assertThat(metrics.getRegistry().find("proxy.client.originHealthCheckFailures")
.tags("originId", someOrigin.id().toString(), "appId", someOrigin.applicationId().toString()).counter().count(), is(1.0));
assertThat(meters(id -> id.getName().equals("proxy.client.originHealthCheckFailures")).size(), is(1));
assertThat(meters(id -> id.getName().equals("proxy.client.originHealthChecks")).size(), is(1));
}

@Test
Expand All @@ -106,6 +108,7 @@ public void declaredOriginUnhealthyOnTransportException() {
.tags("originId", someOrigin.id().toString(), "appId", someOrigin.applicationId().toString()).counter().count(), is(1.0));

assertThat(meters(id -> id.getName().equals("proxy.client.originHealthCheckFailures")).size(), is(1));
assertThat(meters(id -> id.getName().equals("proxy.client.originHealthChecks")).size(), is(1));
}

private List<Meter> meters(Predicate<Meter.Id> predicate) {
Expand Down
Loading

0 comments on commit b0080a6

Please sign in to comment.