Skip to content

Commit 8462a02

Browse files
committed
Modify threadLocal to InheritableThreadLocal
1 parent 9f1350c commit 8462a02

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/TraceIdTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,38 @@ public void beforeMarshalling(Context.BeforeMarshalling context, ExecutionAttrib
241241
}
242242
});
243243
}
244+
245+
@Test
246+
public void traceIdInterceptorWithNewThreadInheritsTraceId() throws Exception {
247+
EnvironmentVariableHelper.run(env -> {
248+
env.set("AWS_LAMBDA_FUNCTION_NAME", "foo");
249+
250+
SdkInternalThreadLocal.put("AWS_LAMBDA_X_TRACE_ID", "SdkInternalThreadLocal-trace-123");
251+
252+
try (MockSyncHttpClient mockHttpClient = new MockSyncHttpClient();
253+
ProtocolRestJsonClient client = ProtocolRestJsonClient.builder()
254+
.region(Region.US_WEST_2)
255+
.credentialsProvider(AnonymousCredentialsProvider.create())
256+
.httpClient(mockHttpClient)
257+
.build()) {
258+
259+
mockHttpClient.stubNextResponse(HttpExecuteResponse.builder()
260+
.response(SdkHttpResponse.builder().statusCode(200).build())
261+
.responseBody(AbortableInputStream.create(new StringInputStream("{}")))
262+
.build());
263+
264+
Thread childThread = new Thread(client::allTypes);
265+
childThread.start();
266+
childThread.join();
267+
268+
List<SdkHttpRequest> requests = mockHttpClient.getRequests();
269+
assertThat(requests.get(0).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("SdkInternalThreadLocal-trace-123");
270+
271+
} catch (InterruptedException e) {
272+
throw new RuntimeException(e);
273+
} finally {
274+
SdkInternalThreadLocal.clear();
275+
}
276+
});
277+
}
244278
}

utils-lite/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<parent>
2222
<groupId>software.amazon.awssdk</groupId>
2323
<artifactId>aws-sdk-java-pom</artifactId>
24-
<version>2.33.8-SNAPSHOT</version>
24+
<version>2.33.9-SNAPSHOT</version>
2525
</parent>
2626
<artifactId>utils-lite</artifactId>
2727
<name>AWS Java SDK :: Utils Lite</name>

utils-lite/src/main/java/software/amazon/awssdk/utilslite/SdkInternalThreadLocal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
@ThreadSafe
2727
@SdkProtectedApi
2828
public final class SdkInternalThreadLocal {
29-
private static final ThreadLocal<Map<String, String>> STORAGE = new ThreadLocal<>();
29+
private static final ThreadLocal<Map<String, String>> STORAGE = new InheritableThreadLocal<>();
3030

3131
private SdkInternalThreadLocal() {
3232
}

0 commit comments

Comments
 (0)