Skip to content
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

Apache HTTP Client interceptor for use with HttpAsyncClient #1886

Closed
candresrv opened this issue Mar 7, 2020 · 2 comments
Closed

Apache HTTP Client interceptor for use with HttpAsyncClient #1886

candresrv opened this issue Mar 7, 2020 · 2 comments
Labels
enhancement A general enhancement
Milestone

Comments

@candresrv
Copy link

candresrv commented Mar 7, 2020

Hi there, I see there is a Micrometer implementation to track the time that http requests take using MicrometerHttpRequestExecutor, but in my project I'm executing these requests using CloseableHttpAsyncClient:

i.e. : myCloseableHttpAsyncClient.execute(req, cxt, callback);

Where:

  • req is an HttpUriRequest obj
  • cxt is a HttpClientContext obj

Is there any way to use Micrometer under this implementation?

Thanks!

@jkschneider jkschneider added this to the 1.4.0 milestone Mar 18, 2020
@jkschneider
Copy link
Contributor

@candresrv Hopefully the method provided below will work for you with the new MicrometerHttpClientInterceptor. Please try out the 1.4.0 snapshot if you get a chance, as this release will be coming out soon.

CloseableHttpAsyncClient asyncClient() {
    MicrometerHttpClientInterceptor interceptor = new MicrometerHttpClientInterceptor(registry,
            request -> request.getRequestLine().getUri(),
            Tags.empty(),
            true);

    return HttpAsyncClients.custom()
            .addInterceptorFirst(interceptor.getRequestInterceptor())
            .addInterceptorLast(interceptor.getResponseInterceptor())
            .build();
}

@jkschneider jkschneider changed the title How to create metrics to HTTP requests that use CloseableHttpAsyncClient Apache HTTP Client interceptor for use with HttpAsyncClient Mar 18, 2020
@shakuzen shakuzen added the enhancement A general enhancement label Mar 18, 2020
@flozano
Copy link

flozano commented May 1, 2023

One question - why is not the Timer.ResourceSample stored directly inside the HttpContext instead of a ConcurrentMap in the MicrometerHttpClientInterceptor?

I am having unexpected behaviours; my interceptors are properly configured (visualVM snapshot of the actual running code):
image

but then sometimes I get:

java.lang.NullPointerException: Cannot invoke "io.micrometer.core.instrument.Timer$ResourceSample.tag(String, String)" because the return value of "java.util.Map.remove(Object)" is null
	at io.micrometer.core.instrument.binder.httpcomponents.MicrometerHttpClientInterceptor.lambda$new$1(MicrometerHttpClientInterceptor.java:75)
	at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:142)
	at org.apache.http.impl.nio.client.MainClientExec.responseReceived(MainClientExec.java:290)
	at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseReceived(DefaultClientExchangeHandlerImpl.java:152)
	at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.responseReceived(HttpAsyncRequestExecutor.java:315)
	at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:255)
	at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:87)
	at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:40)
	at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:121)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
	at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
	at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
	at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
	at java.base/java.lang.Thread.run(Thread.java:833)

which basically means that the HttpContext is not found in the map.

While troubleshooting this I was thinking to rewrite using the HttpContext as the holder of the timer, and I was wondering why this approach was taken instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants