Apply any root URI to RestTemplate metric's URI tag
Previously, a root URI configured via RestTemplateBuilder's rootUri method and RootUriTemplateHandler was not taken into account when generated the URI tag for RestTemplate request metrics. This commit updates MetricsClientHttpRequestInterceptor to be aware of RootUriTemplateHandler and capture the URI template once the root URI has been applied. Fixes gh-25744
This commit is contained in:
@@ -29,6 +29,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.boot.actuate.metrics.AutoTimer;
|
||||
import org.springframework.boot.web.client.RootUriTemplateHandler;
|
||||
import org.springframework.core.NamedThreadLocal;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
@@ -100,21 +101,10 @@ class MetricsClientHttpRequestInterceptor implements ClientHttpRequestIntercepto
|
||||
}
|
||||
|
||||
UriTemplateHandler createUriTemplateHandler(UriTemplateHandler delegate) {
|
||||
return new UriTemplateHandler() {
|
||||
|
||||
@Override
|
||||
public URI expand(String url, Map<String, ?> arguments) {
|
||||
urlTemplate.get().push(url);
|
||||
return delegate.expand(url, arguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI expand(String url, Object... arguments) {
|
||||
urlTemplate.get().push(url);
|
||||
return delegate.expand(url, arguments);
|
||||
}
|
||||
|
||||
};
|
||||
if (delegate instanceof RootUriTemplateHandler) {
|
||||
return ((RootUriTemplateHandler) delegate).withHandlerWrapper(CapturingUriTemplateHandler::new);
|
||||
}
|
||||
return new CapturingUriTemplateHandler(delegate);
|
||||
}
|
||||
|
||||
private Timer.Builder getTimeBuilder(HttpRequest request, ClientHttpResponse response) {
|
||||
@@ -123,6 +113,28 @@ class MetricsClientHttpRequestInterceptor implements ClientHttpRequestIntercepto
|
||||
.description("Timer of RestTemplate operation");
|
||||
}
|
||||
|
||||
private static final class CapturingUriTemplateHandler implements UriTemplateHandler {
|
||||
|
||||
private final UriTemplateHandler delegate;
|
||||
|
||||
private CapturingUriTemplateHandler(UriTemplateHandler delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI expand(String url, Map<String, ?> arguments) {
|
||||
urlTemplate.get().push(url);
|
||||
return this.delegate.expand(url, arguments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI expand(String url, Object... arguments) {
|
||||
urlTemplate.get().push(url);
|
||||
return this.delegate.expand(url, arguments);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final class UrlTemplateThreadLocal extends NamedThreadLocal<Deque<String>> {
|
||||
|
||||
private UrlTemplateThreadLocal() {
|
||||
|
||||
Reference in New Issue
Block a user