skywalking icon indicating copy to clipboard operation
skywalking copied to clipboard

http async client does not create span

Open zhaobnm opened this issue 6 years ago • 12 comments

Please answer these questions before submitting your issue.

  • Why do you submit this issue?
  • [ ] Question or discussion
  • [x] Bug
  • [ ] Requirement
  • [ ] Feature or performance improvement

Bug

  • Which version of SkyWalking, OS and JRE? skywalking-oap-server:8.1.0-es7 apm-httpasyncclient-4.x-plugin-8.1.0

  • What happened? the plugin just work with the frist http request, it does not create span when use http connection in pool because the function completed in sessionrequest does not called

zhaobnm avatar Oct 13 '20 01:10 zhaobnm

You could check with our test demo, https://github.com/apache/skywalking/tree/master/test/plugin/scenarios/httpasyncclient-scenario to find what is different, which is helpful to locate the issue.

wu-sheng avatar Oct 13 '20 04:10 wu-sheng

the demo does not reuse httpclient, it create a new httpclient in request when use one httpclient, it's not work

the first test image the secend test image

zhaobnm avatar Oct 13 '20 07:10 zhaobnm

this is a bug. Is there a plan to fix this bug? @wu-sheng

jintianxiayu avatar Nov 30 '20 01:11 jintianxiayu

@jintianxiayu hi, there is always a bug :) Considering countless plugins with versions, the SkyWalking community encourages all developers to involve in the enhancement and bug fix. If any of you has found the cause, then it should not be hard to fix. We could help with reviewing and CI testing.

wu-sheng avatar Nov 30 '20 01:11 wu-sheng

I would like to try fixing this issue.

This is what I have tried till now and have failed to replicate this issue. Can someone point out anything wrong that I am doing. I guess through the bug description the issue happens when we try to use a pooled HTTP connection and the request spans do not get created for subsequent requests.

Here is the code that I wrote to replicate the issue and got the following spans, which in my humble opinion seem fine.

@GetMapping("/hellopoogle3")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) throws Exception {
	ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
	PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
	cm.setMaxTotal(100);
	try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
			.setConnectionManager(cm)
			.build()) {
		httpclient.start();

		String[] urisToGet = {
				"http://hc.apache.org/test",
				"http://hc.apache.org/httpcomponents-core-ga/",
				"http://www.google.com/blah"
		};

		final CountDownLatch latch = new CountDownLatch(urisToGet.length);
		for (final String uri : urisToGet) {
			final HttpGet httpget = new HttpGet(uri);
			httpclient.execute(httpget, new FutureCallback<HttpResponse>() {

				@Override
				public void completed(final HttpResponse response) {
					latch.countDown();
					System.out.println(httpget.getRequestLine() + "->" + response.getStatusLine());
				}

				@Override
				public void failed(final Exception ex) {
					latch.countDown();
					System.out.println(httpget.getRequestLine() + "->" + ex);
				}

				@Override
				public void cancelled() {
					latch.countDown();
					System.out.println(httpget.getRequestLine() + " cancelled");
				}

			});
		}
		latch.await();
		return "Walking";

	}
}

trace-list

Any help/pointers is appreciated.

jsankalp avatar Dec 11 '20 10:12 jsankalp

@jsankalp Thanks for the feedback. Let's wait to see the progress.

wu-sheng avatar Dec 11 '20 11:12 wu-sheng

@jsankalp Don't close httpclient, second request you can see the bug. beacuse httpclient will cache the connection.

    private static final CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();

    static {
        httpclient.start();
    }

    @GetMapping("/httpasyncclient")
    public String front() throws Exception {
        return asyncRequest3("http://127.0.0.1:8089/httpasyncclient/back");
    }

    private String asyncRequest3(String url) throws IOException {

        final HttpGet request3 = new HttpGet(url);
        HttpAsyncRequestProducer producer3 = HttpAsyncMethods.create(request3);
        AsyncCharConsumer<HttpResponse> consumer3 = new AsyncCharConsumer<HttpResponse>() {
            HttpResponse response;

            @Override
            protected void onResponseReceived(final HttpResponse response) {
                this.response = response;
            }

            @Override
            protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws IOException {
            }

            @Override
            protected void releaseResources() {
            }

            @Override
            protected HttpResponse buildResult(final HttpContext context) {
                return this.response;
            }
        };

        httpclient.execute(producer3, consumer3, new FutureCallback<HttpResponse>() {
            public void completed(final HttpResponse response3) {
                LOGGER.info(request3.getRequestLine() + "->" + response3.getStatusLine());
            }

            public void failed(final Exception ex) {
                LOGGER.error(request3.getRequestLine() + "->" + ex);
            }

            public void cancelled() {
                LOGGER.error(request3.getRequestLine() + " cancelled");
            }

        });
        return "Success";
    }

haoyann avatar Dec 11 '20 14:12 haoyann

@haoyann Thanks for the tip. Now I am able to reproduce it. Will try to fix it as well.

jsankalp avatar Dec 12 '20 10:12 jsankalp

Went through the issue and it seems that when we use a static httpclient the connections are reused from the pool.

So the instrumentation code never goes beyond line no 439 as it finds a free connection from the pool and reuses it. Code for checking free connections

The instrumentation code relies on a new SessionRequestImpl instance creation being intercepted from SessionRequestConstructorInterceptor which is never reached in case of static httpclient and the trace breaks. Making a new connection

What would be a good way to fix this because as per my understanding the assumption that a new SessionRequestImpl will be created for every request is erroneous and making httpclient static is actually the right way to use it.

jsankalp avatar Jan 07 '21 17:01 jsankalp

Hi, I am not very familiar with the codes of this part, so the following answer would be based on your description only.


The key usage of SessionRequestConstructorInterceptor is for propagating the tracing context into the async thread through #setSkyWalkingDynamicField. When(no matter what is the right way to use httpclient) you do from a connection pool, the execution should be also in another thread(right?). You need to find a place the thread-switch happens, and update SessionRequestImpl. SessionRequestImpl should only be used in one thread at once, right?

wu-sheng avatar Jan 08 '21 00:01 wu-sheng

No activity for months.

wu-sheng avatar Sep 20 '21 09:09 wu-sheng

I also found this problem, the httpasyncclient-4.x-plugin did not take effect most of the time. The connections are reused from the pool, so the SessionRequest will not be instantiated.

heihaozi avatar Sep 29 '21 03:09 heihaozi