-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Problem
Errors being logged by HttpClientAdapter::throwExceptionIfErrorIsPresent (used by JavaHttpClientAdapter) do not contain tracing information (e.g., trace/span ID):
cleverclient/src/main/java/io/github/sashirestela/cleverclient/client/HttpClientAdapter.java
Line 105 in 4eabaa5
| logger.error(RESPONSE_FORMAT, data); |
Background
While debugging some unrelated errors when interacting with an embedding model, I noticed that logs produced by JavaHttpClientAdapter did not include trace information.
Initially, I was using JavaHttpClientAdapter directly with SimpleOpenAI. To preserve trace context, I created a custom adapter that extends JavaHttpClientAdapter and restores the trace context across async boundaries via CurrentTraceContext:
(see code below)
Custom Adapter:
// TracingJavaHttpClientAdapter.java
...
@Override
protected Object sendAsync(RequestData request) {
TraceContext contextSnapshot = traceContext.get();
...
return httpResponseFuture.thenApply(httpResponse -> {
try (Scope scope = traceContext.maybeScope(contextSnapshot)) {
...
}
});
}However, this approach is limited due to several visibility restrictions in JavaHttpClientAdapter:
getFunctions()is privateconvertToHttpRequest()is privateconvertToResponseData()is privateFunctionsByCategoryis private
Because of this, I had to re-implement several internal parts of the adapter, which led me to abandon subclassing JavaHttpClientAdapter and instead extend HttpClientAdapter directly.
I also explored using composition (delegate pattern), but send() and sendAsync() are protected, making it impossible to forward calls cleanly to a JavaHttpClientAdapter instance.
Conclusion
Although I now have a working adapter, I believe this could be cleaner and more maintainable with some visibility changes in JavaHttpClientAdapter.
I understand this is a bit of a unique situation as I have to modify the behavior all the way down to the httpResponseFuture callback in the thenApply. If there's an existing way to accomplish this that I'm missing, I would appreciate any assistance!