I have a function that runs in a thread pool, but it only shows up in the Datadog tracing UI when I run it outside of my threadpool. In the screenshot below you can see it show up in sync_work but not in async_work.
Here is my code, contained in a script called ddtrace_threadpool_example.py:
from concurrent.futures import ThreadPoolExecutor
from ddtrace import tracer
from random import random
import time
def perform_work(input_value):
with tracer.trace('do_something') as _:
seconds = random()
time.sleep(seconds)
return input_value**2
def sync_work(input_values):
with tracer.trace('sync_work') as _:
results = []
for input_value in input_values:
result = perform_work(input_value=input_value)
results.append(result)
return results
def async_work(input_values):
with tracer.trace('async_work') as _:
thread_pool = ThreadPoolExecutor(max_workers=10)
futures = thread_pool.map(
lambda input_value:
perform_work(input_value=input_value),
input_values
)
results = list(futures)
return results
@tracer.wrap(service='ddtrace-example')
def start_work():
input_values = list(range(15))
sync_results = sync_work(input_values=input_values)
print(sync_results)
async_results = async_work(input_values=input_values)
print(async_results)
if __name__ == '__main__':
start_work()
I run the script like this: python ddtrace_threadpool_example.py. I'm using Python 3.7, and pip freeze shows ddtrace==0.29.0.

