# Elastic Java Client Async APM Transaction

**URL:** <https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986>\
**Category:** APM\
**Tags:** docker, java\
**Created:** [January 12, 2023, 3:28am UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986 "2023-01-12T03:28:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![elastic\_rookie](https://avatars.discourse-cdn.com/v4/letter/e/a88e57/32.png) [@elastic\_rookie](https://discuss.elastic.co/u/elastic_rookie)\
**Post date:** [January 12, 2023, 3:28am UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/1 "2023-01-12T03:28:35Z")

</div>

If you are asking about a problem you are experiencing, please use the following template, as it will help us help you. If you have a different problem, please delete all of this text 🙂

TIP 1: select at least one tag that further categorizes your topic. For example `server` for APM Server related questions, `java` for questions regarding the Elastic APM Java agent, or `ui` for questions about the APM App within Kibana.

TIP 2: Check out the [troubleshooting guide](https://www.elastic.co/guide/en/apm/get-started/current/troubleshooting.html) first. Not only will it help you to resolve common problems faster but it also explains in more detail which information we need before we can properly help you.

**Kibana version** : 8.53

**Elasticsearch version** : 8.5.3

**APM Server version** :8.5.3

**APM Agent language and version** : Java / 8.5.3

**Browser version** :Chrome 108

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**:

**Fresh install or upgraded from other version?** Fresh

**Is there anything special in your setup?** For example, are you using the Logstash or Kafka outputs? Are you using a load balancer in front of the APM Servers? Have you changed index pattern, generated custom templates, changed agent configuration etc.  
Nothing special, basic docker-compose up setup.

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

I'm currently trying to instrument a transaction through an entire API service call, but seem to lose the ElasticApm.currentTransaction/Span information after any async call to Elasticsearch.

I setup an extremely simple test application with Jersey endpoints that receive a request. The Java agent automatically instruments the Jersey request and any method I call that has the appropriate Public Api annotations on them. This works for any synchronous calls _and_ asynchronous methos (ExecutorService / CompletableFutures), _except_ when I perform an async request with the Java Elastic client. Why is the context not propagating to any future stages after the call to elastic?

Here's a basic structure of the call I'm making

ExecutorService \_executorService = Executors.newFixedThreadPool(10);

```auto
return _asyncClient.search(s -> s
                        .index("products")
                        .query(q -> q
                                .term(t -> t
                                        .field("name")
                                        .value(v -> v.stringValue("bicycle"))
                                )),
                Product.class)
                .thenApplyAsync(response -> {

   /* THE BELOW VALUES ARE ALWAYS NOOP INSTANCES */
                    Transaction trans1 = ElasticApm.currentTransaction();
                    Span span1 = ElasticApm.currentSpan();
                    LOGGER.info("[Apm trans/span id -- {}/{}", trans1.getId(), span1.getId());

                    return response;
                }, _executorService);

```

Any subsequent stage doesn't have the Trace information and consequently the Public Api annotations don't pick anything up either.

Am I misunderstanding how this should be instrumented with Java Futures and the instrumentation from Executors in APM?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [January 13, 2023, 4:15pm UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/2 "2023-01-13T16:15:00Z")

</div>

Hi @elastic_rookie , thanks for reporting this !

I think it requires further investigation, but I have managed to reproduce a similar case as you reported, and I get the same result, thus it is probably a bug in the agent or something in the ES client that prevents the agent to work as expected.

Thus here the next step is on us to further investigate and fix the issue as this is not the expected behavior.

---

<div class="post-metadata">

**Author:** ![elastic\_rookie](https://avatars.discourse-cdn.com/v4/letter/e/a88e57/32.png) [@elastic\_rookie](https://discuss.elastic.co/u/elastic_rookie)\
**Post date:** [January 14, 2023, 1:16pm UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/3 "2023-01-14T13:16:38Z")

</div>

Hi @Sylvain_Juge ,

Thanks for investigating and letting me know! Do you have a ticket/issue link I can track to know when it's fixed?

Are there any potential workarounds that can be applied in the interim?

Thanks!

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [February 1, 2023, 5:56pm UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/4 "2023-02-01T17:56:38Z")

</div>

Hi @elastic_rookie ,

I haven't forgotten about your issue, and I am currently trying a few approaches how to fix this.  
There isn't any issue or PR because I still need to experiment a bit with a few different approaches.

I'll keep you posted on the progress, hopefully there should be a snapshot version to test soon 🙏 , would you be willing to try it on your application to make sure it works for you ?

---

<div class="post-metadata">

**Author:** ![elastic\_rookie](https://avatars.discourse-cdn.com/v4/letter/e/a88e57/32.png) [@elastic\_rookie](https://discuss.elastic.co/u/elastic_rookie)\
**Post date:** [February 7, 2023, 5:13pm UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/5 "2023-02-07T17:13:32Z")

</div>

@Sylvain_Juge sure thing, just let me know when it's ready to test!

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [February 8, 2023, 5:16pm UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/6 "2023-02-08T17:16:39Z")

</div>

Hi !

I have just opened a [PR](https://github.com/elastic/apm-agent-java/pull/3015) with a fix that should provide the active transaction/span in the `CompletableFuture` that you get from the 8.x Elasticsearch client.

Could you test with the following [snapshot](https://apm-ci.elastic.co/job/apm-agent-java/job/apm-agent-java-mbp/job/PR-3015/2/artifact/src/github.com/elastic/apm-agent-java/elastic-apm-agent/target/elastic-apm-agent-1.36.1-SNAPSHOT.jar) to confirm your use-case is properly covered ?

---

<div class="post-metadata">

**Author:** ![elastic\_rookie](https://avatars.discourse-cdn.com/v4/letter/e/a88e57/32.png) [@elastic\_rookie](https://discuss.elastic.co/u/elastic_rookie)\
**Post date:** [February 14, 2023, 2:16am UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/7 "2023-02-14T02:16:15Z")

</div>

@Sylvain_Juge This fixed work great for the 8.x client! In my simple test setup the transaction/span ids were picked up appropriately on subsequent future chains.

Do you know if this change will also work for the 7.X clients as well?

Looking forward to having this fixed, thanks for the support!

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [February 14, 2023, 9:01am UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/8 "2023-02-14T09:01:45Z")

</div>

Thanks for double-checking this @elastic_rookie !

This fix actually is implemented in the 7.x REST client (the 8.x client also relies on REST client), so the recent versions of 7.x Elasticsearch clients are covered as well. In the added [unit tests](https://github.com/elastic/apm-agent-java/blob/cfd570c6305e0a1fdf936ca11262b8791a41e6ee/apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-7_x/src/test/java/co/elastic/apm/agent/esrestclient/v7_x/ElasticsearchRestClientInstrumentationIT.java#L152-L188) you have an example of an equivalent code for 7.x.

Thanks again for the feedback, this will likely be shipped in the next agent release.  
You can subscribe to release notifications on github to get notified when it's available.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 7, 2023, 5:02am UTC](https://discuss.elastic.co/t/elastic-java-client-async-apm-transaction/322986/9 "2023-03-07T05:02:46Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
