Open
Description
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
- Using the new feature (feat(v2): parallel batch processing #1620), we can see in X-Ray that thread sub-segments don't have the good parent (handler).
- MDC keys are lost between threads
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 ofThreadAware
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 frommain
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
Metadata
Metadata
Assignees
Type
Projects
Status
Backlog