-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Exceptions thrown by custom error handlers are not recorded in RestTemplate observations #32060
Comments
I'm not sure I understand the goal of this customization. You can throw Before repurposing this issue in that direction, I would like to get your feedback about throwing |
Unfortunately, it's very complicated to redesign 200+ applications in that way. In fact, I've already been able to achive that what I need with ObservationHandler like this public class RestTemplateObservationHandler implements ObservationHandler<ClientRequestObservationContext> {
private static final ThreadLocal<ClientRequestObservationContext> CONTEXT_THREAD_LOCAL = new ThreadLocal<>();
public static Optional<ClientRequestObservationContext> getCurrentContext() {
return Optional.ofNullable(CONTEXT_THREAD_LOCAL.get());
}
@Override
public void onStart(@NonNull ClientRequestObservationContext context) {
CONTEXT_THREAD_LOCAL.set(context);
}
@Override
public void onStop(@NonNull ClientRequestObservationContext context) {
CONTEXT_THREAD_LOCAL.remove();
}
@Override
public boolean supportsContext(@NonNull Observation.Context context) {
return context instanceof ClientRequestObservationContext;
}
} and RestTemplateCustomizer (from spring-boot) like this @Bean
RestTemplateCustomizer addObservationErrorHandlerRestTemplateBuilderCustomizer() {
return restTemplate -> {
final var originalErrorHandler = restTemplate.getErrorHandler();
final var observationErrorHandler = new ResponseErrorHandler() {
@Override
public boolean hasError(@NonNull ClientHttpResponse response) throws IOException {
return originalErrorHandler.hasError(response);
}
@Override
public void handleError(@NonNull ClientHttpResponse response) throws IOException {
try {
originalErrorHandler.handleError(response);
} catch (Exception e) {
RestTemplateObservationHandler.getCurrentContext()
.ifPresent(context -> context.setError(e));
throw e;
}
}
};
restTemplate.setErrorHandler(observationErrorHandler);
};
} But it seems strange that WebClient handles all exception types while RestTemplate handles only RestClientException. I don't even know if it's worth closing the task. |
So you mean that if all exceptions would be recorded as errors by the observations this would solve the issue for you? I thought the main problem was about getting the current observation. |
Yes, you are right. |
No, that's not what I said. |
Custom ResponseErrorHandler could throw any Yes, this will solve problem for me. |
RestTemplate set error to observation only for IOException and RestClientException and there is no way to customize that.
I tried error handler like this
But observationRegistry#getCurrentObservation returns parent Observation (in my case
http.server.requests
).We use spring-web 6.0.14.
I think there should be some way for customization.
The text was updated successfully, but these errors were encountered: