|
18 | 18 | import static org.assertj.core.api.Assertions.assertThat;
|
19 | 19 |
|
20 | 20 | import java.util.List;
|
| 21 | +import java.util.concurrent.atomic.AtomicReference; |
21 | 22 | import org.junit.jupiter.api.Test;
|
22 | 23 | import org.slf4j.MDC;
|
23 | 24 | import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
|
24 | 25 | import software.amazon.awssdk.awscore.interceptor.TraceIdExecutionInterceptor;
|
| 26 | +import software.amazon.awssdk.core.interceptor.Context; |
| 27 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 28 | +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; |
25 | 29 | import software.amazon.awssdk.http.AbortableInputStream;
|
26 | 30 | import software.amazon.awssdk.http.HttpExecuteResponse;
|
27 | 31 | import software.amazon.awssdk.http.SdkHttpRequest;
|
@@ -188,5 +192,55 @@ public void traceIdInterceptorPreservesTraceIdAcrossExceptionallyCompletedFuture
|
188 | 192 | }
|
189 | 193 | });
|
190 | 194 | }
|
| 195 | + |
| 196 | + @Test |
| 197 | + public void traceIdInterceptorPreservesTraceIdAcrossExceptionallyCompletedFuturesThrownInPreExecution() { |
| 198 | + EnvironmentVariableHelper.run(env -> { |
| 199 | + env.set("AWS_LAMBDA_FUNCTION_NAME", "foo"); |
| 200 | + MDC.put("AWS_LAMBDA_X_TRACE_ID", "mdc-trace-123"); |
| 201 | + |
| 202 | + ExecutionInterceptor throwingInterceptor = new ExecutionInterceptor() { |
| 203 | + private boolean hasThrown = false; |
| 204 | + |
| 205 | + @Override |
| 206 | + public void beforeMarshalling(Context.BeforeMarshalling context, ExecutionAttributes executionAttributes) { |
| 207 | + if (!hasThrown) { |
| 208 | + hasThrown = true; |
| 209 | + throw new RuntimeException("failing in pre execution"); |
| 210 | + } |
| 211 | + } |
| 212 | + }; |
| 213 | + |
| 214 | + try (MockAsyncHttpClient mockHttpClient = new MockAsyncHttpClient(); |
| 215 | + ProtocolRestJsonAsyncClient client = ProtocolRestJsonAsyncClient.builder() |
| 216 | + .region(Region.US_WEST_2) |
| 217 | + .credentialsProvider(AnonymousCredentialsProvider.create()) |
| 218 | + .overrideConfiguration(o -> o.addExecutionInterceptor(throwingInterceptor)) |
| 219 | + .httpClient(mockHttpClient) |
| 220 | + .build()) { |
| 221 | + |
| 222 | + mockHttpClient.stubResponses( |
| 223 | + HttpExecuteResponse.builder() |
| 224 | + .response(SdkHttpResponse.builder().statusCode(200).build()) |
| 225 | + .responseBody(AbortableInputStream.create(new StringInputStream("{}"))) |
| 226 | + .build() |
| 227 | + ); |
| 228 | + |
| 229 | + client.allTypes() |
| 230 | + .exceptionally(throwable -> { |
| 231 | + client.allTypes().join(); |
| 232 | + return null; |
| 233 | + }).join(); |
| 234 | + |
| 235 | + List<SdkHttpRequest> requests = mockHttpClient.getRequests(); |
| 236 | + |
| 237 | + assertThat(requests).hasSize(1); |
| 238 | + assertThat(requests.get(0).firstMatchingHeader("X-Amzn-Trace-Id")).hasValue("mdc-trace-123"); |
| 239 | + |
| 240 | + } finally { |
| 241 | + MDC.clear(); |
| 242 | + } |
| 243 | + }); |
| 244 | + } |
191 | 245 | }
|
192 | 246 |
|
0 commit comments