diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/MetricsClientHttpRequestInterceptor.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/MetricsClientHttpRequestInterceptor.java index 91c751a312..68a0657612 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/MetricsClientHttpRequestInterceptor.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/MetricsClientHttpRequestInterceptor.java @@ -93,14 +93,9 @@ class MetricsClientHttpRequestInterceptor implements ClientHttpRequestIntercepto private Timer.Builder getTimeBuilder(HttpRequest request, ClientHttpResponse response) { - String url = ensureLeadingSlash(urlTemplate.get()); return Timer.builder(this.metricName) - .tags(this.tagProvider.getTags(url, request, response)) + .tags(this.tagProvider.getTags(urlTemplate.get(), request, response)) .description("Timer of RestTemplate operation"); } - private String ensureLeadingSlash(String url) { - return (url == null || url.startsWith("/") ? url : "/" + url); - } - } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java index 8fd908f93f..b960e8dcf4 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,7 +55,7 @@ public final class RestTemplateExchangeTags { * @return the uri tag */ public static Tag uri(HttpRequest request) { - return Tag.of("uri", stripUri(request.getURI().toString())); + return Tag.of("uri", ensureLeadingSlash(stripUri(request.getURI().toString()))); } /** @@ -65,13 +65,17 @@ public final class RestTemplateExchangeTags { */ public static Tag uri(String uriTemplate) { String uri = StringUtils.hasText(uriTemplate) ? uriTemplate : "none"; - return Tag.of("uri", stripUri(uri)); + return Tag.of("uri", ensureLeadingSlash(stripUri(uri))); } private static String stripUri(String uri) { return uri.replaceAll("^https?://[^/]+/", ""); } + private static String ensureLeadingSlash(String url) { + return (url == null || url.startsWith("/") ? url : "/" + url); + } + /** * Creates a {@code status} {@code Tag} derived from the * {@link ClientHttpResponse#getRawStatusCode() status} of the given {@code response}. diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java index 83c466d99e..ead7838d65 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.metrics.web.client; +import java.net.URI; +import java.net.URISyntaxException; import java.util.stream.StreamSupport; import io.micrometer.core.instrument.MeterRegistry; @@ -99,4 +101,18 @@ public class MetricsRestTemplateCustomizerTests { this.mockServer.verify(); } + @Test + public void interceptRestTemplateWithUri() throws URISyntaxException { + this.mockServer + .expect(MockRestRequestMatchers.requestTo("http://localhost/test/123")) + .andExpect(MockRestRequestMatchers.method(HttpMethod.GET)) + .andRespond(MockRestResponseCreators.withSuccess("OK", + MediaType.APPLICATION_JSON)); + String result = this.restTemplate + .getForObject(new URI("http://localhost/test/123"), String.class); + assertThat(result).isEqualTo("OK"); + this.registry.get("http.client.requests").tags("uri", "/test/123").timer(); + this.mockServer.verify(); + } + }