Record errors thrown by custom handler in RestTemplate observations

Prior to this commit, the `RestTemplate` observation instrumentation
would only record `RestClientException` and `IOException` as errors in
the observation. Other types of errors can be thrown by custom
components, such as `ResponseErrorHandler` and in this case they aren't
recorded with the observation.
Also, the current instrumentation does not create any observation scope
around the execution. While this would have a limited benefit as no
application code is executed there, developers could set up custom
components (such as, again, `ResponseErrorHandler`) that could use
contextual logging with trace ids.

This commit ensures that all `Throwable` are recorded as errors with the
observations and that an observation `Scope` is created around the
execution of the client exchange.

Fixes gh-32060
This commit is contained in:
Brian Clozel
2024-01-22 11:03:57 +01:00
parent 5856d2e54e
commit 70d9f7c62c
2 changed files with 51 additions and 2 deletions

View File

@@ -880,7 +880,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
Observation observation = ClientHttpObservationDocumentation.HTTP_CLIENT_EXCHANGES.observation(this.observationConvention,
DEFAULT_OBSERVATION_CONVENTION, () -> observationContext, this.observationRegistry).start();
ClientHttpResponse response = null;
try {
try (Observation.Scope scope = observation.openScope()){
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
@@ -894,7 +894,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
observation.error(exception);
throw exception;
}
catch (RestClientException exc) {
catch (Throwable exc) {
observation.error(exc);
throw exc;
}