# How can we pass elasticapm object for a function inside the threadpool executor

**URL:** <https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646>\
**Category:** APM\
**Tags:** python, aws-lambda\
**Created:** [July 5, 2023, 9:59am UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646 "2023-07-05T09:59:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sairam\_kadakuntla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sairam_kadakuntla/32/122994_2.png) [@sairam\_kadakuntla](https://discuss.elastic.co/u/sairam_kadakuntla)\
**Post date:** [July 5, 2023, 9:59am UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/1 "2023-07-05T09:59:57Z")

</div>

how can we pass elastic apm object through threadpool and add labels to that function

---

<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:** [July 5, 2023, 1:01pm UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/2 "2023-07-05T13:01:03Z")

</div>

Hi @sairam_kadakuntla , could you explain a bit further your need here ?

The APM agents usually take care of context propagation, so if a task is created when there is a transaction/span active, then submitting the task execution to a an execution pool means the task execution in the pool should be able to get the "current" active transaction/span.

Could you maybe provide a few code samples of what you've tried so far ?

---

<div class="post-metadata">

**Author:** ![sairam\_kadakuntla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sairam_kadakuntla/32/122994_2.png) [@sairam\_kadakuntla](https://discuss.elastic.co/u/sairam_kadakuntla)\
**Post date:** [July 5, 2023, 1:22pm UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/3 "2023-07-05T13:22:40Z")

</div>

```
with concurrent.futures.ThreadPoolExecutor() as executor:
    running_tasks = [
        executor.submit(distribute_calls_for_vertica_mongo, req[0], req[1], req[2], req[3], elasticapm) for req in to_call
    ] this is the code after passing elasticapm object and adding labels in this functions its not reflecting in kibana

```

---

<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:** [July 5, 2023, 1:44pm UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/4 "2023-07-05T13:44:48Z")

</div>

What language is that ? we only support Java, and have minimal support for Kotlin and a bit of Scala.

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [July 5, 2023, 7:07pm UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/5 "2023-07-05T19:07:44Z")

</div>

Hey @sairam_kadakuntla!

I'm still going to need a little more context around your code snippet. Is this code within a larger framework that's creating the transactions for you? Are you creating transactions manually? I see you tagged this with `aws-lambda`, so perhaps you're using a threadpool executor within a lambda function? Are you using our lambda layer, and our lambda extension as shown in the latest [docs](https://www.elastic.co/guide/en/apm/agent/python/current/lambda-support.html)?

I'll definitely need to see the code where you're applying the labels, to see if I can spot any red flags there.

---

<div class="post-metadata">

**Author:** ![sairam\_kadakuntla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sairam_kadakuntla/32/122994_2.png) [@sairam\_kadakuntla](https://discuss.elastic.co/u/sairam_kadakuntla)\
**Post date:** [July 6, 2023, 6:31am UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/6 "2023-07-06T06:31:14Z")

</div>

hey @basepi !  
ya iam using aws-lamda of python code, and iam using the layer too, and while adding labels before the threadpool by using elasticapm object its reflecting in kibana but after passing in threadpool for a function and while adding labels in that function its not reflecting in kibana please help me ...

---

<div class="post-metadata">

**Author:** ![sairam\_kadakuntla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sairam_kadakuntla/32/122994_2.png) [@sairam\_kadakuntla](https://discuss.elastic.co/u/sairam_kadakuntla)\
**Post date:** [July 6, 2023, 12:42pm UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/7 "2023-07-06T12:42:44Z")

</div>

hi @basepi !  
while we pass elasticapm object in threadpool for a function and when we add labels to that functions those labels are not reflecting in kibana but if we pass elasticapm object to function without threadpool its working what is tyhe issue here ,please help to solve it.....

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [July 6, 2023, 5:45pm UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/8 "2023-07-06T17:45:11Z")

</div>

I think the issue is probably around execution context. We use `contextvars` to keep the current transaction and span for the given context (whether for threads or async coroutines). When you start the threadpool, I'm betting that it loses that context. Typically, threadpools would be used by the underlying framework, and a given transaction would only be active on a single thread.

You might be able to get around this by passing the transaction object into the threadpool along with the elasticapm object, and setting it in the [execution\_context](https://github.com/elastic/apm-agent-python/blob/main/elasticapm/context/contextvars.py):

```python
# before the threadpool
from elasticapm.traces import execution_context
transaction = execution_context.get_transaction()

```

```python
#inside of the threadpool
from elasticapm.traces import execution_context
execution_context.set_transaction(transaction)

```

Then your calls to `elasticapm.label()` will actually fetch the running transaction successfully.

Keep an eye out for other oddities, this is a usage of the agent that we're not really testing.

---

<div class="post-metadata">

**Author:** ![sairam\_kadakuntla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sairam_kadakuntla/32/122994_2.png) [@sairam\_kadakuntla](https://discuss.elastic.co/u/sairam_kadakuntla)\
**Post date:** [July 18, 2023, 11:52am UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/9 "2023-07-18T11:52:17Z")

</div>

i got label not found error

---

<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 15, 2023, 11:53am UTC](https://discuss.elastic.co/t/how-can-we-pass-elasticapm-object-for-a-function-inside-the-threadpool-executor/337646/10 "2023-08-15T11:53:03Z")

</div>

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