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
This commit is contained in:
parent
adf22d6e4a
commit
ec5ef0f246
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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}.
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue