aws-powertools / powertools-lambda-java

Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/java/
MIT No Attribution
286 stars 91 forks source link

Propagate traces and log context to non-main threads #1671

Open jeromevdl opened 3 months ago

jeromevdl commented 3 months ago

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)

  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:

Environment

scottgerring commented 3 months ago

x-ray specific #1670

humanzz commented 3 months ago

@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 commented 3 months ago

@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 commented 3 months ago

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.