Skip to content

Commit

Permalink
Make MetricsClientHttpRequestInterceptor defensive against metrics re…
Browse files Browse the repository at this point in the history
…coding failures

See gh-24753
  • Loading branch information
izeye authored and snicoll committed Jan 18, 2021
1 parent 5df4011 commit ddfa1e6
Showing 1 changed file with 11 additions and 2 deletions.
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

0 comments on commit ddfa1e6

Please sign in to comment.