-
Notifications
You must be signed in to change notification settings - Fork 936
Feature/master/utils lite lambda trace #6404
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
base: master
Are you sure you want to change the base?
Changes from all commits
d44e944
25b9687
27f1b5b
ee0c14d
93c2542
85642fe
dde4aa7
d863830
087fae8
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"description": "Add a utils-lite package that wraps threadlocal meant for internal shared usage across multiple applications across multiple applications" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,12 @@ | |
import software.amazon.awssdk.annotations.SdkProtectedApi; | ||
import software.amazon.awssdk.awscore.internal.interceptor.TracingSystemSetting; | ||
import software.amazon.awssdk.core.interceptor.Context; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttribute; | ||
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||
import software.amazon.awssdk.http.SdkHttpRequest; | ||
import software.amazon.awssdk.utils.SystemSetting; | ||
import software.amazon.awssdk.utilslite.SdkInternalThreadLocal; | ||
|
||
/** | ||
* The {@code TraceIdExecutionInterceptor} copies the trace details to the {@link #TRACE_ID_HEADER} header, assuming we seem to | ||
|
@@ -32,27 +34,57 @@ | |
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_TRACE_ID"; | ||
private static final ExecutionAttribute<String> TRACE_ID = new ExecutionAttribute<>("TraceId"); | ||
|
||
@Override | ||
public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) { | ||
String traceId = SdkInternalThreadLocal.get(CONCURRENT_TRACE_ID_KEY); | ||
if (traceId != null) { | ||
executionAttributes.putAttribute(TRACE_ID, traceId); | ||
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. Also I was thinking about this case where client is running on host where SdkInternalThreadLocal was not cleared/not-used
Now lets imagine the user is creating new request for every thread as below
Result: 1,000,000 HashMaps created ? Can we please confirm this will not cause any leak of empty hash maps ? 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. Great call out. I didnt think about this. I changed it so that the hashmap only gets initialized when |
||
} | ||
} | ||
|
||
@Override | ||
public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { | ||
Optional<String> traceIdHeader = traceIdHeader(context); | ||
if (!traceIdHeader.isPresent()) { | ||
Optional<String> lambdafunctionName = lambdaFunctionNameEnvironmentVariable(); | ||
Optional<String> traceId = traceId(); | ||
Optional<String> traceId = traceId(executionAttributes); | ||
|
||
if (lambdafunctionName.isPresent() && traceId.isPresent()) { | ||
return context.httpRequest().copy(r -> r.putHeader(TRACE_ID_HEADER, traceId.get())); | ||
} | ||
} | ||
|
||
return context.httpRequest(); | ||
} | ||
|
||
@Override | ||
public void afterExecution(Context.AfterExecution context, ExecutionAttributes executionAttributes) { | ||
saveTraceId(executionAttributes); | ||
} | ||
|
||
@Override | ||
public void onExecutionFailure(Context.FailedExecution context, ExecutionAttributes executionAttributes) { | ||
saveTraceId(executionAttributes); | ||
} | ||
|
||
private static void saveTraceId(ExecutionAttributes executionAttributes) { | ||
String traceId = executionAttributes.getAttribute(TRACE_ID); | ||
if (traceId != null) { | ||
SdkInternalThreadLocal.put(CONCURRENT_TRACE_ID_KEY, executionAttributes.getAttribute(TRACE_ID)); | ||
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. I see SdkInternalThreadLocal we do a put and we never call remove/clear thus if 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. If a thread goes back to thread pool, and a fresh request is being fired from the Lambda Runtime Client interface, it will contain a brand new traceID stored under the same key on the map so it will overwrite itself. If the thread is killed, then when a new thread is being spun up, a clean map is created. |
||
} | ||
} | ||
|
||
private Optional<String> traceIdHeader(Context.ModifyHttpRequest context) { | ||
return context.httpRequest().firstMatchingHeader(TRACE_ID_HEADER); | ||
} | ||
|
||
private Optional<String> traceId() { | ||
private Optional<String> traceId(ExecutionAttributes executionAttributes) { | ||
Optional<String> traceId = Optional.ofNullable(executionAttributes.getAttribute(TRACE_ID)); | ||
if (traceId.isPresent()) { | ||
return traceId; | ||
} | ||
return TracingSystemSetting._X_AMZN_TRACE_ID.getStringValue(); | ||
} | ||
|
||
|
@@ -61,4 +93,4 @@ private Optional<String> lambdaFunctionNameEnvironmentVariable() { | |
return SystemSetting.getStringValueFromEnvironmentVariable(LAMBDA_FUNCTION_NAME_ENVIRONMENT_VARIABLE); | ||
// CHECKSTYLE:ON | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.archtests; | ||
|
||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; | ||
|
||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import com.tngtech.archunit.core.importer.ClassFileImporter; | ||
import com.tngtech.archunit.lang.ArchRule; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Architecture tests for the utils-lite package to ensure it only contains allowed classes. | ||
*/ | ||
public class UtilsLitePackageTest { | ||
|
||
private static final JavaClasses CLASSES = new ClassFileImporter() | ||
.importPackages("software.amazon.awssdk.utilslite"); | ||
|
||
@Test | ||
public void utilsLitePackage_shouldOnlyContainAllowedClasses() { | ||
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. Is this a requirement ? 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 was following @zoewangg suggestion to add a manual check to make sure we don't add classes to this package unintentionally. The sole purpose of this package as of now is to add this wrapper. If we want to expand this in the future, this presents another checkpoint. |
||
ArchRule rule = classes() | ||
.that().resideInAPackage("software.amazon.awssdk.utilslite") | ||
.should().haveNameMatching(".*\\.(SdkInternalThreadLocal|SdkInternalThreadLocalTest)") | ||
.allowEmptyShould(true) | ||
.because("utils-lite package should only contain SdkInternalThreadLocal and its test"); | ||
|
||
rule.check(CLASSES); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
Since we are using ThreadLocal can we confirm if this can come across virtual thread ?
As in if customer makes call on a virtual thread , if the impact has been investigated ?