elastic / apm-agent-python

https://www.elastic.co/guide/en/apm/agent/python/current/index.html
BSD 3-Clause "New" or "Revised" License
414 stars 220 forks source link

[Agent-Python] AsyncIO + ThreadPool Span Tracing #1324

Open gautam-ergo opened 3 years ago

gautam-ergo commented 3 years ago

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.

Environment (please complete the following information)

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.

beniwohli commented 3 years ago

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
gautam-ergo commented 3 years ago

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

beniwohli commented 3 years ago

The stock flask integration should be enough :+1:

gautam-ergo commented 3 years ago

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