Bug description When tracing is enabled and the OpenAiChatModel is invoked using the stream method, the Response metric data at the Model level cannot be obtained in the ObservationHandler processing class
Environment The latest Spring ai version and oracle jdk21
Steps to reproduce - 1. Through debugging, it was found that there may be some issues with the following code:
.doFinally(s -> observation.stop())
.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation));
// @formatter:on
return new MessageAggregator().aggregate(flux, observationContext::setResponse);
This code may cause observation.stop() to run before observationContext::setResponse, which may lead to issues when DefaultChatClientObservationConvention generates metrics, Unable to obtain the metric data from the server response because the response is empty
-
- After the following modifications, it works OK
.doOnError(observation::error)
//.doFinally(s -> observation.stop());
.contextWrite(ctx -> ctx.put(ObservationThreadLocalAccessor.KEY, observation));
// @formatter:on
return new MessageAggregator().aggregate(flux, observationContext::setResponse)
.doFinally(s -> observation.stop());