Skip to content

Improve multi-threading support across utilities #1671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jeromevdl opened this issue Jun 25, 2024 · 4 comments
Open

Improve multi-threading support across utilities #1671

jeromevdl opened this issue Jun 25, 2024 · 4 comments

Comments

@jeromevdl
Copy link
Contributor

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
@jeromevdl jeromevdl added bug Something isn't working RFC labels Jun 25, 2024
@scottgerring
Copy link
Contributor

x-ray specific #1670

@humanzz
Copy link
Contributor

humanzz commented Jun 26, 2024

@scottgerring on x-ray specifically, and independent of powertools, I do wonder if anything can be done in https://github.com/aws/aws-xray-sdk-java (don't really have thoughts/solutions on this) but I am just saying, that if u guys are thinking abt it, and if u find a nice solution, then maybe we should see if such thing can fit into the xray sdk itself

I know it's not a solution for now, but the moment I came across Scoped Values (https://openjdk.org/jeps/464), I felt like, why are we not in the future, and already logging context/xray is using that 😄

@scottgerring
Copy link
Contributor

@scottgerring on x-ray specifically, and independent of powertools, I do wonder if anything can be done in https://github.com/aws/aws-xray-sdk-java (don't really have thoughts/solutions on this) but I am just saying, that if u guys are thinking abt it, and if u find a nice solution, then maybe we should see if such thing can fit into the xray sdk itself

It looks like X-Ray used to use InheritableThreadLocal (see 1.0.6-beta) to store trace context, which may have just worked. Now it's suggesting providing a SegmentContext impl. Chasing after this I've stumbled across LambdaSegmentContext in the x-ray package itself. Because this is pulling the trace header from the environment, I wonder if providing this, or encouraging our users to use this, will be enough to jam all the subsegments under the current request together?

I know it's not a solution for now, but the moment I came across Scoped Values (https://openjdk.org/jeps/464), I felt like, why are we not in the future, and already logging context/xray is using that 😄

This is super jazzy, but probably not helpful for a few years yet sadly 😄

@jeromevdl
Copy link
Contributor Author

There's a great comment from @humanzz that outlines some of the issues that happen with thread pooling in general in a Java-Lambda environment.

@phipag phipag moved this from Triage to Backlog in Powertools for AWS Lambda (Java) Mar 11, 2025
@phipag phipag changed the title Propagate traces and log context to non-main threads Improve multi-threading support across utilities Apr 22, 2025
@phipag phipag added tracer and removed bug Something isn't working logger labels Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

No branches or pull requests

4 participants