Description
Is your feature request related to a problem? Please describe.
When using ThreadPoolExecutor to execute functions in a multi-threaded environment, the traces for these executor functions do not appear as child spans of the parent span. Instead, they are either missing or appear disconnected from the parent span, making it difficult to track the execution flow properly.
Feature Description
The New Relic Python Agent should correctly propagate trace context when using ThreadPoolExecutor, ensuring that functions executed in the thread pool are captured as child spans of the initiating parent span. This would allow better visibility into multi-threaded applications and maintain trace hierarchy.
Describe Alternatives
Explicitly wrapping executor functions with the New Relic tracing context before submitting them to ThreadPoolExecutor.
Additional context
Consider the following example Flask application using ThreadPoolExecutor:
from concurrent.futures import ThreadPoolExecutor
import time
import newrelic.agent
from flask import Flask
app = Flask(__name__)
# Initialize New Relic agent
newrelic.agent.initialize("newrelic.ini")
# Global ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=3)
@newrelic.agent.function_trace()
def function_a():
time.sleep(3)
@newrelic.agent.function_trace()
def demo():
futures = [executor.submit(function_a) for _ in range(3)]
for future in futures:
try:
future.result()
except Exception as e:
print(f"Thread execution error: {e}")
newrelic.agent.notice_error(e)
@app.get("/test")
def hello_world():
start_time = time.time()
demo()
end_time = time.time()
print("Elapsed time:", end_time - start_time)
return "Threads Executed!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
Expected Behavior:
The spans for function_a should appear as child spans of the demo function in the New Relic UI.
Current Behavior:
The spans for function_a do not appear as child spans under demo, leading to broken or missing trace relationships.
Priority
Please help us better understand this feature request by choosing a priority from the following options:
Really Want
I've searched, cloned this repo, but I haven't found this feature. Very happy to be corrected if it does exist.