Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failures when recording metrics in MetricsClientHttpRequestInterceptor may interfere with RestTemplate's main behaviour #24753

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,8 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.boot.actuate.metrics.AutoTimer;
import org.springframework.core.NamedThreadLocal;
Expand All @@ -43,6 +45,8 @@
*/
class MetricsClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {

private static final Log logger = LogFactory.getLog(MetricsClientHttpRequestInterceptor.class);

private static final ThreadLocal<Deque<String>> urlTemplate = new UrlTemplateThreadLocal();

private final MeterRegistry meterRegistry;
Expand Down Expand Up @@ -82,8 +86,13 @@ public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttp
return response;
}
finally {
getTimeBuilder(request, response).register(this.meterRegistry).record(System.nanoTime() - startTime,
TimeUnit.NANOSECONDS);
try {
getTimeBuilder(request, response).register(this.meterRegistry).record(System.nanoTime() - startTime,
TimeUnit.NANOSECONDS);
}
catch (Exception ex) {
logger.info("Failed to record metrics.", ex);
}
if (urlTemplate.get().isEmpty()) {
urlTemplate.remove();
}
Expand Down