Skip to content

Add support for lambda traces via threadLocal #6295

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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

RanVaknin
Copy link
Contributor

@RanVaknin RanVaknin commented Jul 23, 2025

Motivation and Context

This implementation uses SLF4J MDC (which uses ThreadLocal under the hood) to read the trace ID value stored in AWS_LAMBDA_X_TraceId that would be set by the Lambda runtime (this would be added later, probably here).

@RanVaknin RanVaknin added the changelog-not-required Indicate changelog entry is not required for a specific PR label Jul 23, 2025
@RanVaknin RanVaknin requested a review from a team as a code owner July 23, 2025 23:17
Copy link
Contributor

@millems millems left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will async clients be addressed in a separate PR? This won't work for async retries (which are delegated to a retries thread pool), calls that happen within future chaining (which are delegated to a future completion executor thread pool), etc.

We'll want to find where calls might switch threads (i.e. anywhere we're using futures) and then preserve the thread local setting across that thread change.

@@ -32,6 +33,7 @@
public class TraceIdExecutionInterceptor implements ExecutionInterceptor {
private static final String TRACE_ID_HEADER = "X-Amzn-Trace-Id";
private static final String LAMBDA_FUNCTION_NAME_ENVIRONMENT_VARIABLE = "AWS_LAMBDA_FUNCTION_NAME";
private static final String CONCURRENT_TRACE_ID_KEY = "AWS_LAMBDA_X_TraceId";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This capitalization is weird. Was this specific format chosen for a reason?

Copy link
Contributor Author

@RanVaknin RanVaknin Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have context about why it was chosen, but according to the Lambda team ,this is the format they are going to use.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to follow AWS SDK standards - We can tweak our string accordingly

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we agree on a string? ex. AWS_LAMBDA_X_TRACEID?

Copy link
Contributor

@millems millems Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWS_LAMBDA_X_TRACEID or AWS_LAMBDA_X_TRACE_ID sounds good to me!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with AWS_LAMBDA_X_TRACE_ID🤝🏻

@RanVaknin RanVaknin changed the title Add support for lambda traces for concurrent environments Add support for lambda traces via threadLocal Jul 25, 2025
Copy link

@@ -32,27 +34,40 @@
public class TraceIdExecutionInterceptor implements ExecutionInterceptor {
private static final String TRACE_ID_HEADER = "X-Amzn-Trace-Id";
private static final String LAMBDA_FUNCTION_NAME_ENVIRONMENT_VARIABLE = "AWS_LAMBDA_FUNCTION_NAME";
private static final String CONCURRENT_TRACE_ID_KEY = "AWS_LAMBDA_X_TraceId";
private static final ExecutionAttribute<String> CACHED_TRACE_ID = new ExecutionAttribute<>("CachedTraceId");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just TRACE_ID? This is cached in the sense that anything stored in memory is cached. Probably not the right term here.

@@ -133,5 +206,6 @@ private SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context) {
private void resetRelevantEnvVars(EnvironmentVariableHelper env) {
env.remove("AWS_LAMBDA_FUNCTION_NAME");
env.remove("_X_AMZN_TRACE_ID");
env.remove("AWS_LAMBDA_MAX_CONCURRENCY");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer used

.responseBody(AbortableInputStream.create(new StringInputStream("{}")))
.build());

client.allTypes().join();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add a test sending a request in the result future:

client.allTypes()
      .thenRun(() -> client.allTypes() /* Did this request have the right trace ID? */)

I suspect we'll need to plumb the thread local context to there as well (and clear it, so that reuses of that thread don't have it set).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog-not-required Indicate changelog entry is not required for a specific PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants