Skip to content

ClientAdapter Visibility #121

@andjf

Description

@andjf

Problem

Errors being logged by HttpClientAdapter::throwExceptionIfErrorIsPresent (used by JavaHttpClientAdapter) do not contain tracing information (e.g., trace/span ID):


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 private
  • convertToHttpRequest() is private
  • convertToResponseData() is private
  • FunctionsByCategory is 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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions