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

[Agent-Python] AsyncIO + ThreadPool Span Tracing

Open gautam-ergo opened this issue 4 years ago • 4 comments

Describe the bug:

Current setup: A flask endpoint that summons a sync function running asyncio code block and a threadpool executor that dovetails on the results of the asyncio code block

Looking for a way to monitor the function execution via the instrumentation of code running in those several threads.

  • Both @elasticapm.capture_span() and async_capture_span were not able to trace the function execution as desired.
  • Implemented the workaround suggested in #499 but did not work in my case.

Any advice/directions on this would be of great help. TIA.

Environment (please complete the following information)

  • OS: System Version: macOS 10.14.6 (18G6020) Kernel Version: Darwin 18.7.0
  • Python version: 3.8.0
  • Framework and version: Flask 1.1.4
  • APM Server version: 7.6.2
  • Agent version: 6.4.0

Additional context Attaching a blueprint of the function block that gets triggered on hitting the flask request endpoint.

image

Essentially a bulk indexer job that posts to ES /_bulk endpoint post various data pre-processing.

On the APM front, only the request call/response duration is being captured by the agent:

image

Any advice/directions on this would be of great help. TIA.

gautam-ergo avatar Sep 13 '21 21:09 gautam-ergo

Hi @gautam-ergo! I experimented with patching ThreadPoolExecutor to make the transaction/span available in the executing thread a while ago in #705. I updated that branch just now to current master. If you have a test environment available, it would be great if you could give it a try by installing it like so:

pip install https://github.com/beniwohli/apm-agent-python/archive/refs/heads/thread-executor-support.zip\#egg\=elastic-apm\=\=100

beniwohli avatar Sep 14 '21 08:09 beniwohli

Thanks and appreciate the prompt response @beniwohli , will try this out and post the results; just one clarification, do I instrument the code with begin_transaction and end_transaction in conjunction with capture_span or is the flask's app level configuration just about enough to capture the info

gautam-ergo avatar Sep 14 '21 15:09 gautam-ergo

The stock flask integration should be enough :+1:

beniwohli avatar Sep 14 '21 15:09 beniwohli

Hey @beniwohli tried testing with the updated branch, APM is not able to pick up the trace info as expected, apm output is the same as the one I had posted earlier...not sure if i missed anything with the stock integration, any merit in trying the approach outlined in this repo's asyncio-tests

gautam-ergo avatar Sep 20 '21 19:09 gautam-ergo