private void endTimerInner(ServerWebExchange exchange, Sample sample) { String outcome = "CUSTOM"; String status = "CUSTOM"; HttpStatus statusCode = exchange.getResponse().getStatusCode(); if (statusCode != null) { outcome = statusCode.series().name(); status = statusCode.name(); } else { // a non standard HTTPS status could be used. Let's be defensive here if (exchange.getResponse() instanceof AbstractServerHttpResponse) { Integer statusInt = ((AbstractServerHttpResponse) exchange.getResponse()) .getStatusCodeValue(); if (statusInt != null) { status = String.valueOf(statusInt); } else { status = "NA"; } } } Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); Tags tags = Tags.of("outcome", outcome, "status", status, "routeId", route.getId(), "routeUri", route.getUri().toString()); if (log.isTraceEnabled()) { log.trace("Stopping timer 'gateway.requests' with tags " + tags); } sample.stop(meterRegistry.timer("gateway.requests", tags)); } }
@Test public void gatewayRequestsMeterFilterHasTags() { testClient.get().uri("/headers") .header(HttpHeaders.HOST, "www.metricshappypath.org") .exchange() .expectStatus().isOk() .returnResult(String.class).consumeWith(result -> { assertMetricsContainsTag("outcome", HttpStatus.Series.SUCCESSFUL.name()); assertMetricsContainsTag("status", HttpStatus.OK.name()); assertMetricsContainsTag("routeId", "test_metrics_happy_path"); assertMetricsContainsTag("routeUri", "lb://testservice"); }); }
@Test public void gatewayRequestsMeterFilterHasTagsForBadTargetUri() { testClient.get().uri("/badtargeturi") .exchange() .expectStatus().is5xxServerError() .returnResult(String.class).consumeWith(result -> { assertMetricsContainsTag("outcome", HttpStatus.Series.SERVER_ERROR.name()); assertMetricsContainsTag("status", HttpStatus.INTERNAL_SERVER_ERROR.name()); assertMetricsContainsTag("routeId", "default_path_to_httpbin"); assertMetricsContainsTag("routeUri", testUri); }); }