Skip to content

Improve multi-threading support across utilities #1671

Open
@jeromevdl

Description

@jeromevdl

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)

  1. Use the powertools-examples-batch with SQS
  2. See traces:
    image

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions