优化
This commit is contained in:
+9
-1
@@ -7,11 +7,13 @@ import com.baiyue.eventbridge.domain.BridgeInbox;
|
||||
import com.baiyue.eventbridge.mapper.BridgeDeliveryAttemptMapper;
|
||||
import com.baiyue.eventbridge.mapper.BridgeInboxMapper;
|
||||
import com.baiyue.eventbridge.metrics.EventBridgeMetrics;
|
||||
import com.baiyue.common.web.CorrelationContext;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
@@ -63,7 +65,12 @@ class OverseasEventDispatcherTest {
|
||||
|
||||
@Test
|
||||
void successfulDeliveryIsConfirmedAndAudited() throws Exception {
|
||||
when(eventClient.deliver(event)).thenReturn("200");
|
||||
event.setTraceId("550e8400-e29b-41d4-a716-446655440000");
|
||||
when(eventClient.deliver(event)).thenAnswer(invocation -> {
|
||||
assertThat(MDC.get(CorrelationContext.TRACE_ID))
|
||||
.isEqualTo("550e8400e29b41d4a716446655440000");
|
||||
return "200";
|
||||
});
|
||||
when(inboxMapper.markProcessed(eq("event-1"), anyString())).thenReturn(1);
|
||||
|
||||
dispatcher.dispatchOne("event-1");
|
||||
@@ -74,6 +81,7 @@ class OverseasEventDispatcherTest {
|
||||
assertThat(updatedAttempt().getStatus()).isEqualTo("DELIVERED");
|
||||
assertThat(transportCount("DELIVERED")).isEqualTo(1.0);
|
||||
assertThat(registry.get(EventBridgeMetrics.DELIVERY_LATENCY_METRIC).timer().count()).isEqualTo(1);
|
||||
assertThat(MDC.get(CorrelationContext.TRACE_ID)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+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