优化
This commit is contained in:
@@ -33,6 +33,8 @@ class CorrelationIdFilterTest {
|
||||
.matches("00-4bf92f3577b34da6a3ce929d0e0e4736-[0-9a-f]{16}-01");
|
||||
assertThat(chain.traceId).isEqualTo("4bf92f3577b34da6a3ce929d0e0e4736");
|
||||
assertThat(chain.spanId).matches("[0-9a-f]{16}").isNotEqualTo("0000000000000000");
|
||||
assertThat(org.slf4j.MDC.get(CorrelationContext.TRACE_ID)).isNull();
|
||||
assertThat(org.slf4j.MDC.get(CorrelationContext.SPAN_ID)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baiyue.common.web;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class FeignTracePropagationAutoConfigurationTest {
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(FeignTracePropagationAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void registersTraceInterceptorWhenFeignIsPresent() {
|
||||
contextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(FeignTraceRequestInterceptor.class);
|
||||
assertThat(context.getBeansOfType(RequestInterceptor.class)).hasSize(1);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotLoadFeignConfigurationWhenFeignIsAbsent() {
|
||||
contextRunner.withClassLoader(new FilteredClassLoader(RequestInterceptor.class))
|
||||
.run(context -> {
|
||||
assertThat(context).hasNotFailed();
|
||||
assertThat(context).doesNotHaveBean("feignTraceRequestInterceptor");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void backsOffOnlyForAnApplicationProvidedTraceInterceptor() {
|
||||
contextRunner.withBean(FeignTraceRequestInterceptor.class, FeignTraceRequestInterceptor::new)
|
||||
.run(context -> assertThat(context).hasSingleBean(FeignTraceRequestInterceptor.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.baiyue.common.web;
|
||||
|
||||
import feign.RequestTemplate;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class FeignTraceRequestInterceptorTest {
|
||||
private final FeignTraceRequestInterceptor interceptor = new FeignTraceRequestInterceptor();
|
||||
|
||||
@AfterEach
|
||||
void clearMdc() {
|
||||
MDC.clear();
|
||||
}
|
||||
|
||||
@Test
|
||||
void runsLastSoTrustedContextHasFinalHeaderAuthority() {
|
||||
assertThat(interceptor.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void overwritesCallerHeadersFromTrustedMdcAndCreatesNewSampledSpan() {
|
||||
MDC.put(CorrelationContext.REQUEST_ID, "req-trusted-123");
|
||||
MDC.put(CorrelationContext.TRACE_ID, "4bf92f3577b34da6a3ce929d0e0e4736");
|
||||
RequestTemplate template = templateWithSpoofedHeaders();
|
||||
|
||||
interceptor.apply(template);
|
||||
|
||||
assertThat(template.headers().get(CorrelationContext.REQUEST_ID_HEADER))
|
||||
.containsExactly("req-trusted-123");
|
||||
assertThat(template.headers().get(CorrelationContext.TRACE_ID_HEADER))
|
||||
.containsExactly("4bf92f3577b34da6a3ce929d0e0e4736");
|
||||
assertThat(template.headers().get(CorrelationContext.TRACE_PARENT_HEADER))
|
||||
.singleElement().asString()
|
||||
.matches("00-4bf92f3577b34da6a3ce929d0e0e4736-[0-9a-f]{16}-01");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsDifferentNonZeroSpanForEachSynchronousInvocation() {
|
||||
MDC.put(CorrelationContext.TRACE_ID, "4bf92f3577b34da6a3ce929d0e0e4736");
|
||||
RequestTemplate first = new RequestTemplate();
|
||||
RequestTemplate second = new RequestTemplate();
|
||||
|
||||
interceptor.apply(first);
|
||||
interceptor.apply(second);
|
||||
|
||||
assertThat(singleHeader(first, CorrelationContext.TRACE_PARENT_HEADER))
|
||||
.isNotEqualTo(singleHeader(second, CorrelationContext.TRACE_PARENT_HEADER));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stripsManagedCallerHeadersWhenTrustedContextIsMissingOrInvalid() {
|
||||
MDC.put(CorrelationContext.REQUEST_ID, "invalid request id");
|
||||
MDC.put(CorrelationContext.TRACE_ID, "not-a-w3c-trace-id");
|
||||
RequestTemplate template = templateWithSpoofedHeaders();
|
||||
|
||||
interceptor.apply(template);
|
||||
|
||||
assertThat(template.headers()).doesNotContainKeys(
|
||||
CorrelationContext.REQUEST_ID_HEADER,
|
||||
CorrelationContext.TRACE_ID_HEADER,
|
||||
CorrelationContext.TRACE_PARENT_HEADER);
|
||||
}
|
||||
|
||||
private RequestTemplate templateWithSpoofedHeaders() {
|
||||
RequestTemplate template = new RequestTemplate();
|
||||
template.header(CorrelationContext.REQUEST_ID_HEADER, "req-business-parameter");
|
||||
template.header(CorrelationContext.TRACE_ID_HEADER, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
|
||||
template.header(CorrelationContext.TRACE_PARENT_HEADER,
|
||||
"00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-00");
|
||||
return template;
|
||||
}
|
||||
|
||||
private String singleHeader(RequestTemplate template, String name) {
|
||||
return template.headers().get(name).iterator().next();
|
||||
}
|
||||
}
|
||||
@@ -43,4 +43,12 @@ class TraceParentTest {
|
||||
assertThat(root.value()).matches(
|
||||
"00-abcdef0123456789abcdef0123456789-[0-9a-f]{16}-00");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsSampledOutboundChildForTrustedCurrentTrace() {
|
||||
TraceParent child = TraceParent.outboundChild("4BF92F3577B34DA6A3CE929D0E0E4736");
|
||||
|
||||
assertThat(child.value()).matches(
|
||||
"00-4bf92f3577b34da6a3ce929d0e0e4736-[0-9a-f]{16}-01");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user