elastic4s icon indicating copy to clipboard operation
elastic4s copied to clipboard

Support adding custom headers to elastic requests

Open sangamon opened this issue 3 years ago • 0 comments

(elastic4s version: 8.2.1)

It would be nice if there were a way to add custom headers per request, e.g. propagating an X-Request-ID header for request correlation.

The underlying Java client supports adding headers to Request via RequestOptions, but ElasticRequest doesn't model headers. Request instance creation from an ElasticRequest instance is encapsulated within JavaClient#send(). This in turn delegates the created Request to Java RestClient#performRequestAsync(). The RestClient constructor is non-public, so subclassing it in order to inject headers in #performRequestAsync() doesn't seem to be an option, either.

So far I don't see any other way than subclassing JavaClient, overriding #send(), copying the original method body and adding header injection there. (There's the option of writing a dedicated HttpClient implementation from scratch, of course.) And this doesn't even cover bringing in the custom header values, yet. (We're running in Kleisli[IO, TraceHeaders, *] on top.) Am I missing some cleaner approach?

sangamon avatar Jul 12 '22 11:07 sangamon