# How the trace methods works in java agent?

**URL:** <https://discuss.elastic.co/t/how-the-trace-methods-works-in-java-agent/333622>\
**Category:** APM\
**Tags:** java\
**Created:** [May 17, 2023, 5:09am UTC](https://discuss.elastic.co/t/how-the-trace-methods-works-in-java-agent/333622 "2023-05-17T05:09:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbhijithCV](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AbhijithCV](https://discuss.elastic.co/u/AbhijithCV)\
**Post date:** [May 17, 2023, 5:09am UTC](https://discuss.elastic.co/t/how-the-trace-methods-works-in-java-agent/333622/1 "2023-05-17T05:09:09Z")

</div>

Hi,  
I would like to know how the trace methods work in java agent which creates spans for each method invoked during a transaction.

1. How does the agent keep track of which functions belongs to which transaction, as there can be multiple transactions active at a time.
2. What if there are multiple threads within a transaction, how does the agent map a particular thread to transaction?

These above questions are based on my assumption of how it works. Any documentation I can read on its implementation will be appreciated.

Thanks, and regards.  
Abhijith

---

<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:** [May 17, 2023, 7:34am UTC](https://discuss.elastic.co/t/how-the-trace-methods-works-in-java-agent/333622/2 "2023-05-17T07:34:30Z")

</div>

Hi,

We have a general explaination in [our FAQ](https://www.elastic.co/guide/en/apm/agent/java/current/faq.html#faq-how-does-it-work).

Yes, there can be multiple transactions at a time, and the ability to track which transaction/span is currently active (and also parent/child relationships) is called context-propagation and it's a broad topic.

In general, if the span/transaction remains active on a given thread, then we use a thread-based context propagation with a stack of activated spans/transactions.

When the span/transaction is delegated to another thread, then we keep the link by wrapping the delegated task or using an intermediate object in a key/value map to be able to retrieve the context.

---

<div class="post-metadata">

**Author:** ![AbhijithCV](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@AbhijithCV](https://discuss.elastic.co/u/AbhijithCV)\
**Post date:** [May 17, 2023, 7:44am UTC](https://discuss.elastic.co/t/how-the-trace-methods-works-in-java-agent/333622/3 "2023-05-17T07:44:21Z")

</div>

Hi @Sylvain_Juge  
Thank you for the reply

---

<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:** [June 7, 2023, 3:44am UTC](https://discuss.elastic.co/t/how-the-trace-methods-works-in-java-agent/333622/4 "2023-06-07T03:44:28Z")

</div>

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