# How to include thread name in span?

**URL:** <https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347>\
**Category:** APM\
**Tags:** java\
**Created:** [July 13, 2023, 3:47pm UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347 "2023-07-13T15:47:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [July 13, 2023, 3:47pm UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/1 "2023-07-13T15:47:01Z")

</div>

I'm using the java agent version 1.35.0.

Is there a way to include the thread name in the span? Knowing the thread name helps understand a lot about how the app is doing multi-threading.

thanks

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [July 14, 2023, 9:56am UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/2 "2023-07-14T09:56:09Z")

</div>

If you mean automatically included, there is no way. If you mean manually adding in thread name to spans you create, then you have the [setName()](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html#api-set-name) method

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [July 14, 2023, 10:08am UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/3 "2023-07-14T10:08:37Z")

</div>

In addition to what jack said, this will be soon possible via [baggage](https://github.com/elastic/apm-agent-java/issues/3004).  
E.g. you can then define the thread name as baggage for the scope of your thread's `run()` method and add the value as label to your spans and transactions.  
You can follow the linked issue to track the progress.

---

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [July 14, 2023, 2:07pm UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/4 "2023-07-14T14:07:43Z")

</div>

I certainly would not want to have to do this programmatically in every app. It should either be done automatically or via a config option. Knowing the thread name tells a lot about how effectively an app is using multiple threads.

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [July 14, 2023, 3:50pm UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/5 "2023-07-14T15:50:38Z")

</div>

That's more the logging domain than tracing. We fully support integrating logs in with spans, we even have [log\_sending](https://www.elastic.co/guide/en/apm/agent/java/current/logs.html#log-sending) that let's you do it with nothing other than a config change. Most logging frameworks will log the thread with the rest of the details. Here's transaction I looked at and clicking in the log tab shows me the associated log with the thread name in the metadata (I'm using log-sending)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/2/424a7a231decd5e9899bb1100e87ed0ab91a724d.png)

---

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [July 18, 2023, 4:25pm UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/6 "2023-07-18T16:25:27Z")

</div>

We are not using Elastic for logging. Will log sending help in this case?

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [July 21, 2023, 9:57am UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/7 "2023-07-21T09:57:15Z")

</div>

Not sure what you mean by not using Elastic for logging. If you are using any of the usual logging frameworks (log4j, logback, j.u.logging, ...) then [log correlation](https://www.elastic.co/guide/en/apm/agent/java/current/logs.html) would have that in the log, and if you send the log to Elastic APM using either [log\_sending or filebeat](https://www.elastic.co/guide/en/observability/8.8/application-logs.html) then you can see that in the UI

---

<div class="post-metadata">

**Author:** ![johngregg](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@johngregg](https://discuss.elastic.co/u/johngregg)\
**Post date:** [July 24, 2023, 5:46pm UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/8 "2023-07-24T17:46:47Z")

</div>

Even if we were allowed to do log sending, there doesn't appear to be a way to associate the log event with the span, only the trace. It would make a ton more sense if you added the thread name to the span. You already pay the cost of getting a stack trace. Adding process.thread.name to the span seems like a no-brainer.

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [July 25, 2023, 9:36am UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/9 "2023-07-25T09:36:04Z")

</div>

> It would make a ton more sense if you added the thread name to the span. You already pay the cost of getting a stack trace. Adding process.thread.name to the span seems like a no-brainer.

It's less about the performance impact but more about not bloating the span documents storage wise. It's not even necessary to get a stacktrace for finding out the thread name, all it takes is `Thread.currentThread().getName()`.

In addition a span does not necessarily run on a single thread. A span can be started on one thread and ended on another. We'd need to decide how to deal with these cases without presenting misleading information to users.

However, feel free to open a feature request in the GH repository. We'll consider the feature based on how requested it is.

---

<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:** [August 22, 2023, 9:36am UTC](https://discuss.elastic.co/t/how-to-include-thread-name-in-span/338347/10 "2023-08-22T09:36:58Z")

</div>

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