How to keep parent-child relation of spans in Python Elastic APM while using child processes?

30 Views Asked by At

I'm trying to implement APM using elastic-apm in Python and I have the following case: the main process spawns a child subprocess, as shown in the example below:

from concurrent.futures import ProcessPoolExecutor,
from collections.abc import Callable

def subprocess():
    # do some work here
    pass


def _fork_handler(handler: Callable):
    with ProcessPoolExecutor(max_workers=1) as executor:
        handler_future = executor.submit(handler)
        handler_future.result()

if __name__ == "__main__":
    initialize_apm()
    client = get_client()
    client.begin_transaction("main")
    _fork_handler(subprocess)
    client.end_transaction("main")

The problem is, that the span relation chain falls apart once I spawn a subprocess - the code inside subprocess is not instrumented (I see no spans).

I checked the context vars initialized by begin_transaction and they seem to be have correct values inside the subprocess function. Is there some magic I can do to instruct elastic-apm that the subprocess span is the child of `_fork_handler' (as if it was called from that method)?

0

There are 0 best solutions below