-
Notifications
You must be signed in to change notification settings - Fork 100
Support extracting Trace Header via MDC in Lambda Segment Context #431
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,20 +30,28 @@ | |
| import java.util.Objects; | ||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.slf4j.MDC; | ||
|
|
||
| public class LambdaSegmentContext implements SegmentContext { | ||
| private static final Log logger = LogFactory.getLog(LambdaSegmentContext.class); | ||
|
|
||
| private static final String LAMBDA_TRACE_HEADER_KEY = "_X_AMZN_TRACE_ID"; | ||
|
|
||
| private static final String CONCURRENT_TRACE_ID_KEY = "AWS_LAMBDA_X_TRACE_ID"; | ||
|
|
||
| // 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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider rename this function getTraceHeaderFromEnvironment() |
||
| String lambdaTraceHeaderKey = System.getenv(LAMBDA_TRACE_HEADER_KEY); | ||
| return TraceHeader.fromString(lambdaTraceHeaderKey != null && lambdaTraceHeaderKey.length() > 0 | ||
| ? lambdaTraceHeaderKey | ||
| : System.getProperty(LAMBDA_TRACE_HEADER_PROP)); | ||
| String lambdaTraceHeaderKeyFromMdc = MDC.get(CONCURRENT_TRACE_ID_KEY); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
| String lambdaTraceHeaderKeyFromEnvVar = System.getenv(LAMBDA_TRACE_HEADER_KEY); | ||
|
|
||
| if (lambdaTraceHeaderKeyFromMdc != null && lambdaTraceHeaderKeyFromMdc.length() > 0) { | ||
| return TraceHeader.fromString(lambdaTraceHeaderKeyFromMdc); | ||
| } else if (lambdaTraceHeaderKeyFromEnvVar != null && lambdaTraceHeaderKeyFromEnvVar.length() > 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| return TraceHeader.fromString(lambdaTraceHeaderKeyFromEnvVar); | ||
| } else { | ||
| return TraceHeader.fromString(System.getProperty(LAMBDA_TRACE_HEADER_PROP)); | ||
| } | ||
| } | ||
|
|
||
| // SuppressWarnings is needed for passing Root TraceId to noOp segment | ||
|
|
||
There was a problem hiding this comment.
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?