下面列出了org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration#org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
void defaultsToV2Endpoint() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.base-url",
this.server.url("/").toString());
this.context.register(ZipkinAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, TraceAutoConfiguration.class,
Config.class);
this.context.refresh();
Span span = this.context.getBean(Tracing.class).tracer().nextSpan().name("foo")
.tag("foo", "bar").start();
span.finish();
Awaitility.await().untilAsserted(
() -> then(this.server.getRequestCount()).isGreaterThan(1));
// first request is for health check
this.server.takeRequest();
// second request is the span one
RecordedRequest request = this.server.takeRequest();
then(request.getPath()).isEqualTo("/api/v2/spans");
then(request.getBody().readUtf8()).contains("localEndpoint");
}
@Test
public void encoderDirectsEndpoint() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.base-url",
this.server.url("/").toString());
environment().setProperty("spring.zipkin.encoder", "JSON_V1");
this.context.register(ZipkinAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, TraceAutoConfiguration.class,
Config.class);
this.context.refresh();
Span span = this.context.getBean(Tracing.class).tracer().nextSpan().name("foo")
.tag("foo", "bar").start();
span.finish();
Awaitility.await().untilAsserted(
() -> then(this.server.getRequestCount()).isGreaterThan(0));
// first request is for health check
this.server.takeRequest();
// second request is the span one
RecordedRequest request = this.server.takeRequest();
then(request.getPath()).isEqualTo("/api/v1/spans");
then(request.getBody().readUtf8()).contains("binaryAnnotations");
}
@Test
public void overrideActiveMqQueue() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.jms.cache.enabled", "false");
environment().setProperty("spring.zipkin.activemq.queue", "zipkin2");
environment().setProperty("spring.zipkin.activemq.message-max-bytes", "50");
environment().setProperty("spring.zipkin.sender.type", "activemq");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
ActiveMQAutoConfiguration.class, ZipkinAutoConfiguration.class,
TraceAutoConfiguration.class);
this.context.refresh();
then(this.context.getBean(Sender.class)).isInstanceOf(ActiveMQSender.class);
this.context.close();
}
@Test
public void testWithSleuth() {
this.contextRunner
.withConfiguration(AutoConfigurations.of(StackdriverTraceAutoConfiguration.class,
TraceAutoConfiguration.class))
.withUserConfiguration(Configuration.class)
.withPropertyValues("spring.cloud.gcp.project-id=pop-1")
.run((context) -> assertThat(context
.getBeansOfType(TraceIdLoggingWebMvcInterceptor.class).size())
.isEqualTo(0));
}
@Test
public void overrideRabbitMQQueue() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.rabbitmq.queue", "zipkin2");
environment().setProperty("spring.zipkin.sender.type", "rabbit");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
RabbitAutoConfiguration.class, ZipkinAutoConfiguration.class,
TraceAutoConfiguration.class);
this.context.refresh();
then(this.context.getBean(Sender.class)).isInstanceOf(RabbitMQSender.class);
this.context.close();
}
@Test
public void overrideKafkaTopic() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.kafka.topic", "zipkin2");
environment().setProperty("spring.zipkin.sender.type", "kafka");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
KafkaAutoConfiguration.class, ZipkinAutoConfiguration.class,
TraceAutoConfiguration.class);
this.context.refresh();
then(this.context.getBean(Sender.class)).isInstanceOf(KafkaSender.class);
this.context.close();
}
@Test
public void canOverrideBySender() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.sender.type", "web");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
RabbitAutoConfiguration.class, KafkaAutoConfiguration.class,
ZipkinAutoConfiguration.class, TraceAutoConfiguration.class);
this.context.refresh();
then(this.context.getBean(Sender.class).getClass().getName())
.contains("RestTemplateSender");
this.context.close();
}
@Test
public void canOverrideBySenderAndIsCaseInsensitive() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.sender.type", "WEB");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
RabbitAutoConfiguration.class, KafkaAutoConfiguration.class,
ZipkinAutoConfiguration.class, TraceAutoConfiguration.class);
this.context.refresh();
then(this.context.getBean(Sender.class).getClass().getName())
.contains("RestTemplateSender");
this.context.close();
}
@Test
public void rabbitWinsWhenKafkaPresent() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.sender.type", "rabbit");
this.context.register(PropertyPlaceholderAutoConfiguration.class,
RabbitAutoConfiguration.class, KafkaAutoConfiguration.class,
ZipkinAutoConfiguration.class, TraceAutoConfiguration.class);
this.context.refresh();
then(this.context.getBean(Sender.class)).isInstanceOf(RabbitMQSender.class);
this.context.close();
}
@Test
public void supportsMultipleReporters() throws Exception {
this.context = new AnnotationConfigApplicationContext();
environment().setProperty("spring.zipkin.base-url",
this.server.url("/").toString());
this.context.register(ZipkinAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, TraceAutoConfiguration.class,
Config.class, MultipleReportersConfig.class);
this.context.refresh();
then(this.context.getBeansOfType(Sender.class)).hasSize(2);
then(this.context.getBeansOfType(Sender.class))
.containsKeys(ZipkinAutoConfiguration.SENDER_BEAN_NAME, "otherSender");
then(this.context.getBeansOfType(Reporter.class)).hasSize(2);
then(this.context.getBeansOfType(Reporter.class)).containsKeys(
ZipkinAutoConfiguration.REPORTER_BEAN_NAME, "otherReporter");
Span span = this.context.getBean(Tracing.class).tracer().nextSpan().name("foo")
.tag("foo", "bar").start();
span.finish();
Awaitility.await().untilAsserted(
() -> then(this.server.getRequestCount()).isGreaterThan(1));
// first request is for health check
this.server.takeRequest();
// second request is the span one
RecordedRequest request = this.server.takeRequest();
then(request.getPath()).isEqualTo("/api/v2/spans");
then(request.getBody().readUtf8()).contains("localEndpoint");
MultipleReportersConfig.OtherSender sender = this.context
.getBean(MultipleReportersConfig.OtherSender.class);
Awaitility.await().untilAsserted(() -> then(sender.isSpanSent()).isTrue());
}
@Test
public void shouldOverrideDefaultBeans() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(ZipkinAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, TraceAutoConfiguration.class,
Config.class, MyConfig.class);
this.context.refresh();
then(this.context.getBeansOfType(Sender.class)).hasSize(1);
then(this.context.getBeansOfType(Sender.class))
.containsKeys(ZipkinAutoConfiguration.SENDER_BEAN_NAME);
then(this.context.getBeansOfType(Reporter.class)).hasSize(1);
then(this.context.getBeansOfType(Reporter.class))
.containsKeys(ZipkinAutoConfiguration.REPORTER_BEAN_NAME);
Span span = this.context.getBean(Tracing.class).tracer().nextSpan().name("foo")
.tag("foo", "bar").start();
span.finish();
Awaitility.await()
.untilAsserted(() -> then(this.server.getRequestCount()).isEqualTo(0));
MyConfig.MySender sender = this.context.getBean(MyConfig.MySender.class);
Awaitility.await().untilAsserted(() -> then(sender.isSpanSent()).isTrue());
}
private ApplicationContextRunner contextRunner(String... propertyValues) {
return new ApplicationContextRunner().withPropertyValues(propertyValues)
.withConfiguration(AutoConfigurations.of(TraceAutoConfiguration.class,
TraceHttpAutoConfiguration.class,
SkipPatternConfiguration.class));
}
private ApplicationContextRunner contextRunner(String... propertyValues) {
return new ApplicationContextRunner().withPropertyValues(propertyValues)
.withConfiguration(AutoConfigurations.of(TraceAutoConfiguration.class,
TraceRpcAutoConfiguration.class));
}
private ApplicationContextRunner contextRunner(String... propertyValues) {
return new ApplicationContextRunner().withPropertyValues(propertyValues)
.withConfiguration(AutoConfigurations.of(TraceAutoConfiguration.class,
TraceMessagingAutoConfiguration.class));
}