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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user