Merge pull request #12126 from jkschneider

* pr/12126:
  Fix URI tag on RestTemplate requests based on URIs
pull/12117/merge
Phillip Webb 7 years ago
commit cc9a5b72e1

@ -93,14 +93,9 @@ class MetricsClientHttpRequestInterceptor implements ClientHttpRequestIntercepto
private Timer.Builder getTimeBuilder(HttpRequest request, private Timer.Builder getTimeBuilder(HttpRequest request,
ClientHttpResponse response) { ClientHttpResponse response) {
String url = ensureLeadingSlash(urlTemplate.get());
return Timer.builder(this.metricName) 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"); .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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @return the uri tag
*/ */
public static Tag uri(HttpRequest request) { 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) { public static Tag uri(String uriTemplate) {
String uri = StringUtils.hasText(uriTemplate) ? uriTemplate : "none"; 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) { private static String stripUri(String uri) {
return uri.replaceAll("^https?://[^/]+/", ""); 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 * Creates a {@code status} {@code Tag} derived from the
* {@link ClientHttpResponse#getRawStatusCode() status} of the given {@code response}. * {@link ClientHttpResponse#getRawStatusCode() status} of the given {@code response}.

@ -16,6 +16,8 @@
package org.springframework.boot.actuate.metrics.web.client; package org.springframework.boot.actuate.metrics.web.client;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.MeterRegistry;
@ -99,4 +101,18 @@ public class MetricsRestTemplateCustomizerTests {
this.mockServer.verify(); 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…
Cancel
Save