优化
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
package com.baiyue.eventbridge.trace;
|
||||
|
||||
import com.baiyue.common.web.CorrelationContext;
|
||||
import com.baiyue.eventbridge.domain.BridgeInbox;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class EventBridgeTracePropagationTest {
|
||||
|
||||
@AfterEach
|
||||
void clearMdc() {
|
||||
MDC.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void envelopeUuidFlowsThroughMdcAndRawHttpAsAChildSpan() {
|
||||
BridgeInbox event = new BridgeInbox();
|
||||
event.setTraceId("550e8400-e29b-41d4-a716-446655440000");
|
||||
event.setCorrelationId("ORDER:customer-99:booking-123");
|
||||
event.setEventId("event-business-id");
|
||||
|
||||
try (EventBridgeTraceScope ignored = EventBridgeTraceScope.open(event)) {
|
||||
String localSpan = MDC.get(CorrelationContext.SPAN_ID);
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create("http://127.0.0.1/internal"))
|
||||
.header(CorrelationContext.REQUEST_ID_HEADER, event.getEventId())
|
||||
.header(CorrelationContext.TRACE_ID_HEADER, event.getCorrelationId())
|
||||
.header(CorrelationContext.TRACE_PARENT_HEADER,
|
||||
"00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-00");
|
||||
HttpRequest request = new EventBridgeHttpTracePropagation().apply(builder).GET().build();
|
||||
|
||||
assertThat(MDC.get(CorrelationContext.TRACE_ID))
|
||||
.isEqualTo("550e8400e29b41d4a716446655440000");
|
||||
assertThat(request.headers().firstValue(CorrelationContext.TRACE_ID_HEADER))
|
||||
.contains("550e8400e29b41d4a716446655440000");
|
||||
assertThat(request.headers().firstValue(CorrelationContext.REQUEST_ID_HEADER).orElseThrow())
|
||||
.matches("[0-9a-f-]{36}").isNotEqualTo(event.getEventId());
|
||||
assertThat(request.headers().firstValue(CorrelationContext.TRACE_PARENT_HEADER).orElseThrow())
|
||||
.matches("00-550e8400e29b41d4a716446655440000-[0-9a-f]{16}-01")
|
||||
.doesNotContain(localSpan);
|
||||
}
|
||||
|
||||
assertThat(MDC.get(CorrelationContext.TRACE_ID)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidLegacyEnvelopeTraceIsSafelyRebuiltAndPreviousMdcIsRestored() {
|
||||
MDC.put("existing", "preserved");
|
||||
BridgeInbox event = new BridgeInbox();
|
||||
event.setTraceId("JWT-or-business-id-value");
|
||||
|
||||
try (EventBridgeTraceScope ignored = EventBridgeTraceScope.open(event)) {
|
||||
assertThat(MDC.get(CorrelationContext.TRACE_ID)).matches("[0-9a-f]{32}")
|
||||
.isNotEqualTo(event.getTraceId());
|
||||
}
|
||||
|
||||
assertThat(MDC.get("existing")).isEqualTo("preserved");
|
||||
assertThat(MDC.get(CorrelationContext.TRACE_ID)).isNull();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user