diff --git a/core/aws-core/src/main/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptor.java b/core/aws-core/src/main/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptor.java index 95224228cfb4..ee69fe660db2 100644 --- a/core/aws-core/src/main/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptor.java +++ b/core/aws-core/src/main/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptor.java @@ -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.utils.ThreadStorage; /** * 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 TRACE_ID = new ExecutionAttribute<>("TraceId"); + + @Override + public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) { + String traceId = ThreadStorage.get(CONCURRENT_TRACE_ID_KEY); + if (traceId != null) { + executionAttributes.putAttribute(TRACE_ID, traceId); + } + } @Override public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { Optional traceIdHeader = traceIdHeader(context); if (!traceIdHeader.isPresent()) { Optional lambdafunctionName = lambdaFunctionNameEnvironmentVariable(); - Optional traceId = traceId(); + Optional 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) { + ThreadStorage.put(CONCURRENT_TRACE_ID_KEY, executionAttributes.getAttribute(TRACE_ID)); + } + } + private Optional traceIdHeader(Context.ModifyHttpRequest context) { return context.httpRequest().firstMatchingHeader(TRACE_ID_HEADER); } - private Optional traceId() { + private Optional traceId(ExecutionAttributes executionAttributes) { + Optional traceId = Optional.ofNullable(executionAttributes.getAttribute(TRACE_ID)); + if (traceId.isPresent()) { + return traceId; + } return TracingSystemSetting._X_AMZN_TRACE_ID.getStringValue(); } diff --git a/core/aws-core/src/test/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptorTest.java b/core/aws-core/src/test/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptorTest.java index b3f965a490fc..0f4f4194601f 100644 --- a/core/aws-core/src/test/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptorTest.java +++ b/core/aws-core/src/test/java/software/amazon/awssdk/awscore/interceptor/TraceIdExecutionInterceptorTest.java @@ -28,6 +28,7 @@ import software.amazon.awssdk.http.SdkHttpMethod; import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.testutils.EnvironmentVariableHelper; +import software.amazon.awssdk.utils.ThreadStorage; public class TraceIdExecutionInterceptorTest { @Test @@ -111,6 +112,78 @@ public void headerNotAddedIfNoTraceIdEnvVar() { }); } + @Test + public void modifyHttpRequest_whenMultiConcurrencyModeWithThreadStorage_shouldAddTraceIdHeader() { + EnvironmentVariableHelper.run(env -> { + resetRelevantEnvVars(env); + env.set("AWS_LAMBDA_FUNCTION_NAME", "foo"); + ThreadStorage.put("AWS_LAMBDA_X_TRACE_ID", "ThreadStorage-trace-123"); + + try { + TraceIdExecutionInterceptor interceptor = new TraceIdExecutionInterceptor(); + ExecutionAttributes executionAttributes = new ExecutionAttributes(); + + interceptor.beforeExecution(null, executionAttributes); + Context.ModifyHttpRequest context = context(); + + SdkHttpRequest request = interceptor.modifyHttpRequest(context, executionAttributes); + assertThat(request.firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + } finally { + ThreadStorage.remove("AWS_LAMBDA_X_TRACE_ID"); + } + }); + } + + @Test + public void modifyHttpRequest_whenMultiConcurrencyModeWithBothThreadStorageAndSystemProperty_shouldUseThreadStorageValue() { + EnvironmentVariableHelper.run(env -> { + resetRelevantEnvVars(env); + env.set("AWS_LAMBDA_FUNCTION_NAME", "foo"); + + ThreadStorage.put("AWS_LAMBDA_X_TRACE_ID", "ThreadStorage-trace-123"); + Properties props = System.getProperties(); + props.setProperty("com.amazonaws.xray.traceHeader", "sys-prop-345"); + + try { + TraceIdExecutionInterceptor interceptor = new TraceIdExecutionInterceptor(); + ExecutionAttributes executionAttributes = new ExecutionAttributes(); + + interceptor.beforeExecution(null, executionAttributes); + + Context.ModifyHttpRequest context = context(); + SdkHttpRequest request = interceptor.modifyHttpRequest(context, executionAttributes); + + assertThat(request.firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + } finally { + ThreadStorage.remove("AWS_LAMBDA_X_TRACE_ID"); + props.remove("com.amazonaws.xray.traceHeader"); + } + }); + } + + @Test + public void modifyHttpRequest_whenNotInLambdaEnvironmentWithThreadStorage_shouldNotAddHeader() { + EnvironmentVariableHelper.run(env -> { + resetRelevantEnvVars(env); + + ThreadStorage.put("AWS_LAMBDA_X_TRACE_ID", "should-be-ignored"); + + try { + TraceIdExecutionInterceptor interceptor = new TraceIdExecutionInterceptor(); + ExecutionAttributes executionAttributes = new ExecutionAttributes(); + + interceptor.beforeExecution(null, executionAttributes); + + Context.ModifyHttpRequest context = context(); + SdkHttpRequest request = interceptor.modifyHttpRequest(context, executionAttributes); + + assertThat(request.firstMatchingHeader("X-Amzn-Trace-Id")).isEmpty(); + } finally { + ThreadStorage.remove("AWS_LAMBDA_X_TRACE_ID"); + } + }); + } + private Context.ModifyHttpRequest context() { return context(SdkHttpRequest.builder() .uri(URI.create("https://localhost")) diff --git a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/TraceIdTest.java b/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/TraceIdTest.java index 3299e26ef876..18971743729a 100644 --- a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/TraceIdTest.java +++ b/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/TraceIdTest.java @@ -17,17 +17,25 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.List; import org.junit.jupiter.api.Test; import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; import software.amazon.awssdk.awscore.interceptor.TraceIdExecutionInterceptor; +import software.amazon.awssdk.core.interceptor.Context; +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; import software.amazon.awssdk.http.AbortableInputStream; import software.amazon.awssdk.http.HttpExecuteResponse; +import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.SdkHttpResponse; import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonAsyncClient; import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; import software.amazon.awssdk.testutils.EnvironmentVariableHelper; +import software.amazon.awssdk.testutils.service.http.MockAsyncHttpClient; import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; import software.amazon.awssdk.utils.StringInputStream; +import software.amazon.awssdk.utils.ThreadStorage; /** * Verifies that the {@link TraceIdExecutionInterceptor} is actually wired up for AWS services. @@ -56,4 +64,182 @@ public void traceIdInterceptorIsEnabled() { } }); } + + @Test + public void traceIdInterceptorPreservesTraceIdAcrossRetries() { + EnvironmentVariableHelper.run(env -> { + env.set("AWS_LAMBDA_FUNCTION_NAME", "foo"); + ThreadStorage.put("AWS_LAMBDA_X_TRACE_ID", "ThreadStorage-trace-123"); + + try (MockAsyncHttpClient mockHttpClient = new MockAsyncHttpClient(); + ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() + .region(Region.US_WEST_2) + .credentialsProvider(AnonymousCredentialsProvider.create()) + .httpClient(mockHttpClient) + .build()) { + + mockHttpClient.stubResponses( + HttpExecuteResponse.builder() + .response(SdkHttpResponse.builder().statusCode(500).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build(), + HttpExecuteResponse.builder() + .response(SdkHttpResponse.builder().statusCode(500).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build(), + HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(200).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build()); + + client.allTypes().join(); + + List requests = mockHttpClient.getRequests(); + assertThat(requests).hasSize(3); + + assertThat(requests.get(0).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + assertThat(requests.get(1).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + assertThat(requests.get(2).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + + } finally { + ThreadStorage.clear(); + } + }); + } + + @Test + public void traceIdInterceptorPreservesTraceIdAcrossChainedFutures() { + EnvironmentVariableHelper.run(env -> { + env.set("AWS_LAMBDA_FUNCTION_NAME", "foo"); + ThreadStorage.put("AWS_LAMBDA_X_TRACE_ID", "ThreadStorage-trace-123"); + + try (MockAsyncHttpClient mockHttpClient = new MockAsyncHttpClient(); + ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() + .region(Region.US_WEST_2) + .credentialsProvider(AnonymousCredentialsProvider.create()) + .httpClient(mockHttpClient) + .build()) { + + mockHttpClient.stubResponses( + HttpExecuteResponse.builder() + .response(SdkHttpResponse.builder().statusCode(200).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build(), + HttpExecuteResponse.builder() + .response(SdkHttpResponse.builder().statusCode(200).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build() + ); + + client.allTypes() + .thenRun(() -> { + client.allTypes().join(); + }) + .join(); + + List requests = mockHttpClient.getRequests(); + + assertThat(requests).hasSize(2); + + assertThat(requests.get(0).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + assertThat(requests.get(1).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + + } finally { + ThreadStorage.clear(); + } + }); + } + + @Test + public void traceIdInterceptorPreservesTraceIdAcrossExceptionallyCompletedFutures() { + EnvironmentVariableHelper.run(env -> { + env.set("AWS_LAMBDA_FUNCTION_NAME", "foo"); + ThreadStorage.put("AWS_LAMBDA_X_TRACE_ID", "ThreadStorage-trace-123"); + + try (MockAsyncHttpClient mockHttpClient = new MockAsyncHttpClient(); + ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() + .region(Region.US_WEST_2) + .credentialsProvider(AnonymousCredentialsProvider.create()) + .httpClient(mockHttpClient) + .build()) { + + mockHttpClient.stubResponses( + HttpExecuteResponse.builder() + .response(SdkHttpResponse.builder().statusCode(400).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build(), + HttpExecuteResponse.builder() + .response(SdkHttpResponse.builder().statusCode(200).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build() + ); + + client.allTypes() + .exceptionally(throwable -> { + client.allTypes().join(); + return null; + }).join(); + + List requests = mockHttpClient.getRequests(); + + assertThat(requests).hasSize(2); + + assertThat(requests.get(0).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + assertThat(requests.get(1).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + + } finally { + ThreadStorage.clear(); + } + }); + } + + @Test + public void traceIdInterceptorPreservesTraceIdAcrossExceptionallyCompletedFuturesThrownInPreExecution() { + EnvironmentVariableHelper.run(env -> { + env.set("AWS_LAMBDA_FUNCTION_NAME", "foo"); + ThreadStorage.put("AWS_LAMBDA_X_TRACE_ID", "ThreadStorage-trace-123"); + + ExecutionInterceptor throwingInterceptor = new ExecutionInterceptor() { + private boolean hasThrown = false; + + @Override + public void beforeMarshalling(Context.BeforeMarshalling context, ExecutionAttributes executionAttributes) { + if (!hasThrown) { + hasThrown = true; + throw new RuntimeException("failing in pre execution"); + } + } + }; + + try (MockAsyncHttpClient mockHttpClient = new MockAsyncHttpClient(); + ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() + .region(Region.US_WEST_2) + .credentialsProvider(AnonymousCredentialsProvider.create()) + .overrideConfiguration(o -> o.addExecutionInterceptor(throwingInterceptor)) + .httpClient(mockHttpClient) + .build()) { + + mockHttpClient.stubResponses( + HttpExecuteResponse.builder() + .response(SdkHttpResponse.builder().statusCode(200).build()) + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) + .build() + ); + + client.allTypes() + .exceptionally(throwable -> { + client.allTypes().join(); + return null; + }).join(); + + List requests = mockHttpClient.getRequests(); + + assertThat(requests).hasSize(1); + assertThat(requests.get(0).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("ThreadStorage-trace-123"); + + } finally { + ThreadStorage.clear(); + } + }); + } } + diff --git a/utils/src/main/java/software/amazon/awssdk/utils/ThreadStorage.java b/utils/src/main/java/software/amazon/awssdk/utils/ThreadStorage.java new file mode 100644 index 000000000000..54e3d225c274 --- /dev/null +++ b/utils/src/main/java/software/amazon/awssdk/utils/ThreadStorage.java @@ -0,0 +1,51 @@ +/* + * 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.utils; + +import java.util.HashMap; +import java.util.Map; +import software.amazon.awssdk.annotations.SdkProtectedApi; + +/** + * Utility for thread-local context storage. + */ +@SdkProtectedApi +public final class ThreadStorage { + private static final ThreadLocal> STORAGE = ThreadLocal.withInitial(HashMap::new); + + private ThreadStorage() { + } + + public static void put(String key, String value) { + STORAGE.get().put(key, value); + } + + public static String get(String key) { + return STORAGE.get().get(key); + } + + public static String remove(String key) { + return STORAGE.get().remove(key); + } + + public static void clear() { + STORAGE.get().clear(); + } + + public static boolean containsKey(String key) { + return STORAGE.get().containsKey(key); + } +} \ No newline at end of file diff --git a/utils/src/test/java/software/amazon/awssdk/utils/ThreadStorageTest.java b/utils/src/test/java/software/amazon/awssdk/utils/ThreadStorageTest.java new file mode 100644 index 000000000000..ffcfe8167705 --- /dev/null +++ b/utils/src/test/java/software/amazon/awssdk/utils/ThreadStorageTest.java @@ -0,0 +1,82 @@ +/* + * 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.utils; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ThreadStorageTest { + + @AfterEach + void cleanup() { + ThreadStorage.clear(); + } + + @Test + void put_withValidKeyValue_shouldStoreValue() { + ThreadStorage.put("test-key", "test-value"); + + assertThat(ThreadStorage.get("test-key")).isEqualTo("test-value"); + } + + @Test + void get_withNonExistentKey_shouldReturnNull() { + assertThat(ThreadStorage.get("non-existent")).isNull(); + } + + @Test + void remove_withExistingKey_shouldRemoveAndReturnValue() { + ThreadStorage.put("test-key", "test-value"); + + String removed = ThreadStorage.remove("test-key"); + + assertThat(removed).isEqualTo("test-value"); + assertThat(ThreadStorage.get("test-key")).isNull(); + } + + @Test + void put_withNullValue_shouldRemoveKey() { + ThreadStorage.put("test-key", "test-value"); + ThreadStorage.put("test-key", null); + + assertThat(ThreadStorage.get("test-key")).isNull(); + } + + @Test + void clear_withMultipleValues_shouldRemoveAllValues() { + ThreadStorage.put("key1", "value1"); + ThreadStorage.put("key2", "value2"); + + ThreadStorage.clear(); + + assertThat(ThreadStorage.get("key1")).isNull(); + assertThat(ThreadStorage.get("key2")).isNull(); + } + + @Test + void containsKey_withExistingKey_shouldReturnTrue() { + ThreadStorage.put("test-key", "test-value"); + + assertThat(ThreadStorage.containsKey("test-key")).isTrue(); + } + + @Test + void containsKey_withNonExistentKey_shouldReturnFalse() { + assertThat(ThreadStorage.containsKey("non-existent")).isFalse(); + } +} \ No newline at end of file