Skip to content

Commit ec5ef0f

Browse files
Jon Schneiderphilwebb
authored andcommitted
Fix URI tag on RestTemplate requests based on URIs
Move leading slash logic from `MetricsClientHttpRequestInterceptor` to `RestTemplateExchangeTags` so that URI based calls are also managed. Closes gh-12126
1 parent adf22d6 commit ec5ef0f

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/MetricsClientHttpRequestInterceptor.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,9 @@ public URI expand(String url, Object... arguments) {
9393

9494
private Timer.Builder getTimeBuilder(HttpRequest request,
9595
ClientHttpResponse response) {
96-
String url = ensureLeadingSlash(urlTemplate.get());
9796
return Timer.builder(this.metricName)
98-
.tags(this.tagProvider.getTags(url, request, response))
97+
.tags(this.tagProvider.getTags(urlTemplate.get(), request, response))
9998
.description("Timer of RestTemplate operation");
10099
}
101100

102-
private String ensureLeadingSlash(String url) {
103-
return (url == null || url.startsWith("/") ? url : "/" + url);
104-
}
105-
106101
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,7 +55,7 @@ public static Tag method(HttpRequest request) {
5555
* @return the uri tag
5656
*/
5757
public static Tag uri(HttpRequest request) {
58-
return Tag.of("uri", stripUri(request.getURI().toString()));
58+
return Tag.of("uri", ensureLeadingSlash(stripUri(request.getURI().toString())));
5959
}
6060

6161
/**
@@ -65,13 +65,17 @@ public static Tag uri(HttpRequest request) {
6565
*/
6666
public static Tag uri(String uriTemplate) {
6767
String uri = StringUtils.hasText(uriTemplate) ? uriTemplate : "none";
68-
return Tag.of("uri", stripUri(uri));
68+
return Tag.of("uri", ensureLeadingSlash(stripUri(uri)));
6969
}
7070

7171
private static String stripUri(String uri) {
7272
return uri.replaceAll("^https?://[^/]+/", "");
7373
}
7474

75+
private static String ensureLeadingSlash(String url) {
76+
return (url == null || url.startsWith("/") ? url : "/" + url);
77+
}
78+
7579
/**
7680
* Creates a {@code status} {@code Tag} derived from the
7781
* {@link ClientHttpResponse#getRawStatusCode() status} of the given {@code response}.

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/MetricsRestTemplateCustomizerTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.metrics.web.client;
1818

19+
import java.net.URI;
20+
import java.net.URISyntaxException;
1921
import java.util.stream.StreamSupport;
2022

2123
import io.micrometer.core.instrument.MeterRegistry;
@@ -99,4 +101,18 @@ public void normalizeUriToContainLeadingSlash() {
99101
this.mockServer.verify();
100102
}
101103

104+
@Test
105+
public void interceptRestTemplateWithUri() throws URISyntaxException {
106+
this.mockServer
107+
.expect(MockRestRequestMatchers.requestTo("http://localhost/test/123"))
108+
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
109+
.andRespond(MockRestResponseCreators.withSuccess("OK",
110+
MediaType.APPLICATION_JSON));
111+
String result = this.restTemplate
112+
.getForObject(new URI("http://localhost/test/123"), String.class);
113+
assertThat(result).isEqualTo("OK");
114+
this.registry.get("http.client.requests").tags("uri", "/test/123").timer();
115+
this.mockServer.verify();
116+
}
117+
102118
}

0 commit comments

Comments
 (0)