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

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

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 ?

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

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

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?

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

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 ...

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.....

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:

# before the threadpool
from elasticapm.traces import execution_context
transaction = execution_context.get_transaction()
#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.

i got label not found error

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