apm-agent-python icon indicating copy to clipboard operation
apm-agent-python copied to clipboard

No spans in APM when transaction is active

Open josefkorbel opened this issue 3 years ago • 1 comments

Hello! I have tried to use master branch to connect the dots in async background tasks (using the elasticapm.get_client)

Here is my "stack"

                                                                             
      +-------------------+                                                  
      |  Backend Service  |                                                  
      |                   |                                                  
      |  with APM Agent   |                                                  
      +---------|---------+                                                  
                | TraceParent is passed if request originated                
                | from another APM Agent, otherwise transaction is created   
                | and TraceParent is created and passed                      
      +-------------------+                                                  
      |    uWSGI Spooler  |                                                  
      |    Task Queue     |                                                  
      |    (Async)        |                                                  
      +-------------------+                                                  
                | Transaction is active, visible and connected in APM,       
                | however SPANs to this transactions are not visible anywhere
      +---------|---------+                                                  
      |   Asynchronnous   |             -                                    
      |   Background      |                                                  
      |   Task            |                                                  
      +-------------------+                                                  
                             

This somehow works, and I can see the whole transaction in APM even though the request only submits the tasks to queue and returns immidately after few miliseconds, so it is correctly paired with the trace id.

Screenshot from 2021-03-15 10-35-11

However inside the task, which has this transaction active, I am creating spans with elasticapm.capture_span decorator, and they are not visible in APM. I've added logging to really double check that there is indeed a valid active transaction.

simplified codeblock:

logger.info(f"Transaction before caputuring span: {elasticapm.get_transaction_id()}")
with elasticapm.capture_span("my_span"):
    do_stuff()

Which returns:

[info] Transaction before caputuring span: 44d90007a40c8780

That matches the transaction that I can see in APM: Screenshot from 2021-03-15 10-54-23

But I do not see the spans in APM, what am I doing wrong?

josefkorbel avatar Mar 15 '21 09:03 josefkorbel

Spans are only created if the parent transaction is sampled. Can you try grabbing the actual transaction object using execution_context like in the linked code, and check if it's set to be sampled?

basepi avatar Jun 14 '21 15:06 basepi