Skip to content

Conversation

@jj22ee
Copy link
Contributor

@jj22ee jj22ee commented Aug 15, 2025

Issue #, if available:
Follow pattern used by AWS SDK for Java:

Description of changes:

  • Lambda segment context will prioritize extracting lambdaTraceHeader from MDC first, then Env Var, then System Property. Before it would just prioritize Env Var then System Property.
  • Add unit tests.

Other Thoughts:

  • X-Ray SDK will only use the API for slf4j to extract from MDC, similarly to AWS SDK. If there is no implementation for logging (e.g. log4j-slf4j-impl, log4j-core), this extraction logic will not work (MDC.get(...) returns null) and fallback to Env Var or System Property. I'm not exactly what guarantees (if it is guaranteed) the logging implementation to exist in Lambda Environments.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@jj22ee jj22ee requested a review from a team as a code owner August 15, 2025 22:30
return TraceHeader.fromString(lambdaTraceHeaderKey != null && lambdaTraceHeaderKey.length() > 0
? lambdaTraceHeaderKey
: System.getProperty(LAMBDA_TRACE_HEADER_PROP));
String lambdaTraceHeaderKeyFromMdc = MDC.get(CONCURRENT_TRACE_ID_KEY);
Copy link
Contributor Author

@jj22ee jj22ee Aug 15, 2025

Choose a reason for hiding this comment

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

This slf4j API will return null if there is no implementation for logging (e.g. log4j-slf4j-impl, log4j-core).

I'm not exactly sure what guarantees (if it is guaranteed) the logging implementation to exist in Lambda Environments, but this is how AWS SDK is implementing this logic (which also only depends on slf4j API, not the implementation that is needed like in the unit test dependencies).

Choose a reason for hiding this comment

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

Hi there,

This is correct. For this to work one of the following has to provide this implementation:

  1. The Lambda runtime
  2. The AWS Java SDK v2 - not possible because it will break current customers who have their own implementation (logback etc)
  3. The Xray SDK
  4. The customer - probably not possible since this is supposed to work by default (?) and this would require customers to explicitly provide an implementation.


if (lambdaTraceHeaderKeyFromMdc != null && lambdaTraceHeaderKeyFromMdc.length() > 0) {
return TraceHeader.fromString(lambdaTraceHeaderKeyFromMdc);
} else if (lambdaTraceHeaderKeyFromEnvVar != null && lambdaTraceHeaderKeyFromEnvVar.length() > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: You don't need 'else' as you have 'return' in the previous block.

// See: https://github.com/aws/aws-xray-sdk-java/issues/251
private static final String LAMBDA_TRACE_HEADER_PROP = "com.amazonaws.xray.traceHeader";

public static TraceHeader getTraceHeaderFromEnvironment() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider rename this function getTraceHeaderFromEnvironment()

implementation("com.google.auto.value:auto-value-annotations:1.10.4")
implementation("com.google.auto.service:auto-service-annotations:1.1.1")
implementation("org.apache.httpcomponents:httpclient:4.5.14")
implementation("org.slf4j:slf4j-api:1.7.30")
Copy link
Contributor

Choose a reason for hiding this comment

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

May I know why using this old version? Q complains this version has performance issue. https://mvnrepository.com/artifact/org.slf4j/slf4j-api?

@jj22ee jj22ee requested a review from lukeina2z August 21, 2025 23:02
@jj22ee jj22ee marked this pull request as draft August 21, 2025 23:05
Copy link
Contributor

@lukeina2z lukeina2z left a comment

Choose a reason for hiding this comment

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

revert previous approval.

@jj22ee
Copy link
Contributor Author

jj22ee commented Sep 2, 2025

Closing PR as the solution will be changed - See aws/aws-sdk-java-v2#6366

@jj22ee jj22ee closed this Sep 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants