# Possible to add lables before transaction starts?

**URL:** <https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817>\
**Category:** APM\
**Tags:** java\
**Created:** [July 10, 2019, 3:44pm UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817 "2019-07-10T15:44:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![OrangeDog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orangedog/32/4630_2.png) [@OrangeDog](https://discuss.elastic.co/u/OrangeDog)\
**Post date:** [July 10, 2019, 3:44pm UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/1 "2019-07-10T15:44:43Z")

</div>

With the Elastic APM Java Agent API you can add lables and custom context to an in-progress transaction and/or span.

Is it possible to add a label that will be used for the next, or all transactions, on the current thread?  
Similar to using logback's MDC.  
I'm guessing adding them to a No-Op Span doesn't work.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [July 10, 2019, 3:56pm UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/2 "2019-07-10T15:56:58Z")

</div>

Hi 🙂

No, that's not possible. What's you use case? Why is adding labels to an existing transaction not sufficient?

To add labels to all transactions, use the [`global_labels`](https://www.elastic.co/guide/en/apm/agent/java/current/config-core.html#config-global-labels) configuration option.

I hope that helps 👍

---

<div class="post-metadata">

**Author:** ![OrangeDog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orangedog/32/4630_2.png) [@OrangeDog](https://discuss.elastic.co/u/OrangeDog)\
**Post date:** [July 10, 2019, 3:59pm UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/3 "2019-07-10T15:59:34Z")

</div>

> Why is adding labels to an existing transaction not sufficient?

Because I'd have to repeat the same code in many places. It would be better if I could add it in a single place before any instrumented methods are called.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [July 11, 2019, 9:12am UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/4 "2019-07-11T09:12:20Z")

</div>

Would `global_labels` help for your scenario, then?

If not, could you elaborate on what your scenario is and where you want to add which tags to which transactions?

---

<div class="post-metadata">

**Author:** ![OrangeDog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orangedog/32/4630_2.png) [@OrangeDog](https://discuss.elastic.co/u/OrangeDog)\
**Post date:** [July 11, 2019, 10:07am UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/5 "2019-07-11T10:07:13Z")

</div>

No, because they're not global.

It's a non-HTTP request-based protocol. The connection is established and a thread assigned to it before any requests (i.e. transactions) begin. There will be many request of different types on each connection. I want to add details of the connection to the recorded transactions. I don't want to have to add code to do this in every single different request-handling method.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [July 11, 2019, 11:58am UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/6 "2019-07-11T11:58:02Z")

</div>

Which protocol and framework are you using? Is that something homegrown or an open source framework? Most frameworks have a way to register filters/interceptors. Is that possible with the framework you are using?

---

<div class="post-metadata">

**Author:** ![OrangeDog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orangedog/32/4630_2.png) [@OrangeDog](https://discuss.elastic.co/u/OrangeDog)\
**Post date:** [July 11, 2019, 2:14pm UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/7 "2019-07-11T14:14:19Z")

</div>

It is an in-house protocol, no framework.

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [July 11, 2019, 3:22pm UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/8 "2019-07-11T15:22:25Z")

</div>

In that case I would suggest to either add interceptor capabilities to that or to use Byte Buddy or some other bytecode manipulation/aspect oriented programing framework like AspectJ or Spring AOP to achieve something similar without requiring code changes.

---

<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 1, 2019, 11:22am UTC](https://discuss.elastic.co/t/possible-to-add-lables-before-transaction-starts/189817/9 "2019-08-01T11:22:29Z")

</div>

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