# How to detect active transaction?

**URL:** <https://discuss.elastic.co/t/how-to-detect-active-transaction/313328>\
**Category:** APM\
**Tags:** java\
**Created:** [August 31, 2022, 7:57am UTC](https://discuss.elastic.co/t/how-to-detect-active-transaction/313328 "2022-08-31T07:57:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shaoranlaos](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@Shaoranlaos](https://discuss.elastic.co/u/Shaoranlaos)\
**Post date:** [August 31, 2022, 7:57am UTC](https://discuss.elastic.co/t/how-to-detect-active-transaction/313328/1 "2022-08-31T07:57:50Z")

</div>

**APM Server version** : 7.8.0

**APM Agent language and version** : java/1.33.0

I wrote an instrumentation plugin for our tibco message bus (commercial/not open) and would like to only create a publish span if there already is a transaction active.  
If i always create a transaction, the problem is that a publish generated from the application itself (without external prompt, e.g. Activity-Events (shows to the world that the application is running)) will generate a transaction in APM with the type `unknown` and there is no way to change this transaction type within the plugin.

I could not find a way to detect if there already is a transaction active or not.  
I tried to simply use `Span.Current()==null` but that is not working because `Span.Current()` always returns an object only that in the case that there was no previous span it will return a span that is invalid. But i can not find a way to find out if the returned Span is invalid or not.

Has anyone a way for me to detect in a plugin if there is already an active Span or not?

---

<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:** [August 31, 2022, 8:24am UTC](https://discuss.elastic.co/t/how-to-detect-active-transaction/313328/2 "2022-08-31T08:24:28Z")

</div>

Hi !

I am assuming that you are using the OpenTelemetry API here as you referred to `Span.current()`.  
For internal plugins it is different because we check the tracer status, which will return `null` for the active span if there is none.

In order to know if the returned span is valid or not, you can check the status of the span context with `Span.current().getSpanContext().isValid()`.

---

<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:** [September 21, 2022, 4:25am UTC](https://discuss.elastic.co/t/how-to-detect-active-transaction/313328/3 "2022-09-21T04:25:18Z")

</div>

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