|
40 | 40 | import org.mockito.junit.jupiter.MockitoExtension;
|
41 | 41 | import org.mockito.junit.jupiter.MockitoSettings;
|
42 | 42 | import org.mockito.quality.Strictness;
|
| 43 | +import software.amazon.awssdk.utilslite.SdkInternalThreadLocal; |
43 | 44 |
|
44 | 45 | @ExtendWith(MockitoExtension.class)
|
45 | 46 | @MockitoSettings(strictness = Strictness.LENIENT)
|
@@ -174,4 +175,118 @@ private static void testContextResultsInNoOpSegmentParent() {
|
174 | 175 | mockContext.endSubsegment(AWSXRay.getGlobalRecorder());
|
175 | 176 | assertThat(AWSXRay.getTraceEntity()).isNull();
|
176 | 177 | }
|
| 178 | + |
| 179 | + @Test |
| 180 | + @SetSystemProperty(key = "com.amazonaws.xray.traceHeader", value = TRACE_HEADER) |
| 181 | + void testSystemPropertyFallbackWithTraceValidation() { |
| 182 | + LambdaSegmentContext mockContext = new LambdaSegmentContext(); |
| 183 | + Subsegment subsegment = mockContext.beginSubsegment(AWSXRay.getGlobalRecorder(), "test"); |
| 184 | + FacadeSegment parent = (FacadeSegment) subsegment.getParent(); |
| 185 | + |
| 186 | + // Verify system property values are used correctly |
| 187 | + assertThat(parent.getTraceId().toString()).isEqualTo("1-57ff426a-80c11c39b0c928905eb0828d"); |
| 188 | + assertThat(parent.getId()).isEqualTo("1234abcd1234abcd"); |
| 189 | + assertThat(parent.isSampled()).isTrue(); |
| 190 | + |
| 191 | + mockContext.endSubsegment(AWSXRay.getGlobalRecorder()); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + @SetEnvironmentVariable(key = "_X_AMZN_TRACE_ID", value = TRACE_HEADER) |
| 196 | + void testEnvironmentVariableFallbackWithTraceValidation() { |
| 197 | + LambdaSegmentContext mockContext = new LambdaSegmentContext(); |
| 198 | + Subsegment subsegment = mockContext.beginSubsegment(AWSXRay.getGlobalRecorder(), "test"); |
| 199 | + FacadeSegment parent = (FacadeSegment) subsegment.getParent(); |
| 200 | + |
| 201 | + // Verify system property values are used correctly |
| 202 | + assertThat(parent.getTraceId().toString()).isEqualTo("1-57ff426a-80c11c39b0c928905eb0828d"); |
| 203 | + assertThat(parent.getId()).isEqualTo("1234abcd1234abcd"); |
| 204 | + assertThat(parent.isSampled()).isTrue(); |
| 205 | + |
| 206 | + mockContext.endSubsegment(AWSXRay.getGlobalRecorder()); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + @SetSystemProperty(key = "com.amazonaws.xray.traceHeader", value = TRACE_HEADER_2) |
| 211 | + void testSdkInternalThreadLocalTakesPriorityOverSystemProperty() { |
| 212 | + SdkInternalThreadLocal.put("AWS_LAMBDA_X_TRACE_ID", TRACE_HEADER); |
| 213 | + |
| 214 | + try { |
| 215 | + LambdaSegmentContext mockContext = new LambdaSegmentContext(); |
| 216 | + Subsegment subsegment = mockContext.beginSubsegment(AWSXRay.getGlobalRecorder(), "test"); |
| 217 | + FacadeSegment parent = (FacadeSegment) subsegment.getParent(); |
| 218 | + |
| 219 | + // Verify SdkInternalThreadLocal values are used (TRACE_HEADER), not system property values (TRACE_HEADER_2) |
| 220 | + assertThat(parent.getTraceId().toString()).isEqualTo("1-57ff426a-80c11c39b0c928905eb0828d"); |
| 221 | + assertThat(parent.getId()).isEqualTo("1234abcd1234abcd"); |
| 222 | + assertThat(parent.isSampled()).isTrue(); |
| 223 | + |
| 224 | + mockContext.endSubsegment(AWSXRay.getGlobalRecorder()); |
| 225 | + } finally { |
| 226 | + SdkInternalThreadLocal.remove("AWS_LAMBDA_X_TRACE_ID"); |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + @SetSystemProperty(key = "com.amazonaws.xray.traceHeader", value = TRACE_HEADER) |
| 232 | + void testSdkInternalThreadLocalWithEmptyStringFallsBackToSystemProperty() { |
| 233 | + SdkInternalThreadLocal.put("AWS_LAMBDA_X_TRACE_ID", ""); |
| 234 | + |
| 235 | + try { |
| 236 | + LambdaSegmentContext mockContext = new LambdaSegmentContext(); |
| 237 | + Subsegment subsegment = mockContext.beginSubsegment(AWSXRay.getGlobalRecorder(), "test"); |
| 238 | + FacadeSegment parent = (FacadeSegment) subsegment.getParent(); |
| 239 | + |
| 240 | + // Verify system property values are used as fallback when SdkInternalThreadLocal returns empty string |
| 241 | + assertThat(parent.getTraceId().toString()).isEqualTo("1-57ff426a-80c11c39b0c928905eb0828d"); |
| 242 | + assertThat(parent.getId()).isEqualTo("1234abcd1234abcd"); |
| 243 | + assertThat(parent.isSampled()).isTrue(); |
| 244 | + |
| 245 | + mockContext.endSubsegment(AWSXRay.getGlobalRecorder()); |
| 246 | + } finally { |
| 247 | + SdkInternalThreadLocal.remove("AWS_LAMBDA_X_TRACE_ID"); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + @SetEnvironmentVariable(key = "_X_AMZN_TRACE_ID", value = TRACE_HEADER_2) |
| 253 | + void testSdkInternalThreadLocalTakesPriorityOverEnvironmentVariable() { |
| 254 | + SdkInternalThreadLocal.put("AWS_LAMBDA_X_TRACE_ID", TRACE_HEADER); |
| 255 | + |
| 256 | + try { |
| 257 | + LambdaSegmentContext mockContext = new LambdaSegmentContext(); |
| 258 | + Subsegment subsegment = mockContext.beginSubsegment(AWSXRay.getGlobalRecorder(), "test"); |
| 259 | + FacadeSegment parent = (FacadeSegment) subsegment.getParent(); |
| 260 | + |
| 261 | + // Verify SdkInternalThreadLocal values are used (TRACE_HEADER), not environment variable values (TRACE_HEADER_2) |
| 262 | + assertThat(parent.getTraceId().toString()).isEqualTo("1-57ff426a-80c11c39b0c928905eb0828d"); |
| 263 | + assertThat(parent.getId()).isEqualTo("1234abcd1234abcd"); |
| 264 | + assertThat(parent.isSampled()).isTrue(); |
| 265 | + |
| 266 | + mockContext.endSubsegment(AWSXRay.getGlobalRecorder()); |
| 267 | + } finally { |
| 268 | + SdkInternalThreadLocal.remove("AWS_LAMBDA_X_TRACE_ID"); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + @Test |
| 273 | + @SetEnvironmentVariable(key = "_X_AMZN_TRACE_ID", value = TRACE_HEADER) |
| 274 | + void testSdkInternalThreadLocalWithEmptyStringFallsBackToEnvironmentVariable() { |
| 275 | + SdkInternalThreadLocal.put("AWS_LAMBDA_X_TRACE_ID", ""); |
| 276 | + |
| 277 | + try { |
| 278 | + LambdaSegmentContext mockContext = new LambdaSegmentContext(); |
| 279 | + Subsegment subsegment = mockContext.beginSubsegment(AWSXRay.getGlobalRecorder(), "test"); |
| 280 | + FacadeSegment parent = (FacadeSegment) subsegment.getParent(); |
| 281 | + |
| 282 | + // Verify environment variable values are used as fallback when SdkInternalThreadLocal returns empty string |
| 283 | + assertThat(parent.getTraceId().toString()).isEqualTo("1-57ff426a-80c11c39b0c928905eb0828d"); |
| 284 | + assertThat(parent.getId()).isEqualTo("1234abcd1234abcd"); |
| 285 | + assertThat(parent.isSampled()).isTrue(); |
| 286 | + |
| 287 | + mockContext.endSubsegment(AWSXRay.getGlobalRecorder()); |
| 288 | + } finally { |
| 289 | + SdkInternalThreadLocal.remove("AWS_LAMBDA_X_TRACE_ID"); |
| 290 | + } |
| 291 | + } |
177 | 292 | }
|
0 commit comments