Powertools for AWS Lambda (Java) should support multithreading properly, especially but not-only:
- with log context and MDC, so that custom keys are not lost between threads
- with traces and X-Ray, so that subsegment have the good parent segment when changing thread
Note that these 2 components use ThreadLocal.
What were you trying to accomplish?
Expected Behavior
- X-Ray traces should have the good hierarchy: subsegments within threads should be under segment within 'main' thread
- MDC keys (for logs) should be kept in all threads.
Current Behavior
Steps to Reproduce (for bugs)
- Use the powertools-examples-batch with SQS
- See traces:

Possible Solution
For logs, a temporary solution has been implemented (MultiThreadMDC.
We should think about a more "generic" or "universal" way of managing multithreading in Powertools to not only make logging and tracing work, but also enable users to use multithread more simply.
Initial thoughts:
- Having an
ThreadAware interface in the common module and a static list of ThreadAware classes.
ThreadAware interface should have at least 2 methods to act before the thread starts and within the thread execution, to be able to copy data from main thread (or perform another task like getting the parent x-ray segment) and reuse it within the thread.
- When working with thread, we should iterate over this list and call these methods. Maybe a util class in common could simplify this too.
Environment
- Powertools for AWS Lambda (Java) version used: 2.0.0-SNAPSHOT
Powertools for AWS Lambda (Java) should support multithreading properly, especially but not-only:
Note that these 2 components use
ThreadLocal.What were you trying to accomplish?
Expected Behavior
Current Behavior
Steps to Reproduce (for bugs)
Possible Solution
For logs, a temporary solution has been implemented (
MultiThreadMDC.We should think about a more "generic" or "universal" way of managing multithreading in Powertools to not only make logging and tracing work, but also enable users to use multithread more simply.
Initial thoughts:
ThreadAwareinterface in the common module and a static list ofThreadAwareclasses.ThreadAwareinterface should have at least 2 methods to act before the thread starts and within the thread execution, to be able to copy data frommainthread (or perform another task like getting the parent x-ray segment) and reuse it within the thread.Environment