Merge branch '2.2.x'

This commit is contained in:
Marcin Grzejszczak
2020-03-03 19:44:56 +01:00
22 changed files with 679 additions and 125 deletions

View File

@@ -60,6 +60,18 @@ https://www.w3.org/2001/XMLSchema-instance ">
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<type>test-jar</type>
<scope>test</scope>
<classifier>test-binder</classifier>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -65,6 +65,7 @@ public class TraceContextPropagationChannelInterceptorTests {
public void testSpanPropagation() {
Span span = this.tracing.tracer().nextSpan().name("http:testSendMessage").start();
String expectedSpanId = SpanUtil.idToHex(span.context().spanId());
try (Tracer.SpanInScope ws = this.tracing.tracer().withSpanInScope(span)) {
this.channel.send(MessageBuilder.withPayload("hi").build());
}
@@ -72,6 +73,10 @@ public class TraceContextPropagationChannelInterceptorTests {
span.finish();
}
assertThatNewSpanIdWasSetOnMessage(expectedSpanId);
}
private void assertThatNewSpanIdWasSetOnMessage(String expectedSpanId) {
Message<?> message = this.channel.receive(0);
assertThat(message).as("message was null").isNotNull();
@@ -88,7 +93,6 @@ public class TraceContextPropagationChannelInterceptorTests {
String.class);
assertThat(parentId).as("parentId was not equal to parent's id")
.isEqualTo(this.reporter.getSpans().get(0).id());
}
@Configuration

View File

@@ -0,0 +1,120 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.instrument.messaging;
import brave.Span;
import brave.Tracer;
import brave.Tracing;
import brave.sampler.Sampler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.sleuth.instrument.util.SpanUtil;
import org.springframework.cloud.sleuth.util.ArrayListSpanReporter;
import org.springframework.cloud.stream.binder.test.OutputDestination;
import org.springframework.cloud.stream.binder.test.TestChannelBinderConfiguration;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.test.annotation.DirtiesContext;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Spencer Gibb
*/
@SpringBootTest(classes = TraceStreamChannelInterceptorTests.App.class,
properties = "spring.cloud.stream.source=testSupplier")
@DirtiesContext
public class TraceStreamChannelInterceptorTests {
@Autowired
private OutputDestination channel;
@Autowired
private Tracing tracing;
@Autowired
private StreamBridge streamBridge;
@Autowired
private ArrayListSpanReporter reporter;
@AfterEach
public void close() {
this.reporter.clear();
}
@Test
public void testSpanPropagationViaBridge() {
Span span = this.tracing.tracer().nextSpan().name("http:testSendMessage").start();
String expectedSpanId = SpanUtil.idToHex(span.context().spanId());
try (Tracer.SpanInScope ws = this.tracing.tracer().withSpanInScope(span)) {
this.streamBridge.send("testSupplier-out-0", "hi");
}
finally {
span.finish();
}
assertThatNewSpanIdWasSetOnMessage(expectedSpanId);
}
private void assertThatNewSpanIdWasSetOnMessage(String expectedSpanId) {
Message<?> message = this.channel.receive(0);
assertThat(message).as("message was null").isNotNull();
String spanId = message.getHeaders().get(TraceMessageHeaders.SPAN_ID_NAME,
String.class);
assertThat(spanId).as("spanId was equal to parent's id")
.isNotEqualTo(expectedSpanId);
String traceId = message.getHeaders().get(TraceMessageHeaders.TRACE_ID_NAME,
String.class);
assertThat(traceId).as("traceId was null").isNotNull();
String parentId = message.getHeaders().get(TraceMessageHeaders.PARENT_ID_NAME,
String.class);
// [0] - producer
// [1] - http:testsendmessage
assertThat(parentId).as("parentId was not equal to parent's id")
.isEqualTo(this.reporter.getSpans().get(1).id());
}
@Configuration
@EnableAutoConfiguration
@ImportAutoConfiguration(TestChannelBinderConfiguration.class)
static class App {
@Bean
Sampler testSampler() {
return Sampler.ALWAYS_SAMPLE;
}
@Bean
ArrayListSpanReporter reporter() {
return new ArrayListSpanReporter();
}
}
}

View File

@@ -19,7 +19,6 @@ package org.springframework.cloud.sleuth.instrument.messaging.issues.issue_943;
import brave.sampler.Sampler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
@@ -30,8 +29,7 @@ import org.springframework.integration.config.EnableIntegration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class })
@ImportResource("classpath:beans/applicationContext.xml")
@EnableIntegration

View File

@@ -24,6 +24,7 @@ import java.util.stream.Collectors;
import brave.Tracing;
import brave.http.HttpRequest;
import brave.http.HttpRequestParser;
import brave.sampler.Sampler;
import brave.sampler.SamplerFunction;
import org.assertj.core.api.BDDAssertions;
@@ -66,9 +67,6 @@ public class TraceFilterWebIntegrationTests {
@Rule
public OutputCaptureRule capture = new OutputCaptureRule();
@Autowired
Tracing tracer;
@Autowired
ArrayListSpanReporter accumulator;
@@ -85,6 +83,16 @@ public class TraceFilterWebIntegrationTests {
this.accumulator.clear();
}
@Test
public void should_tag_url() {
new RestTemplate().getForObject("http://localhost:" + port() + "/good",
String.class);
then(Tracing.current().tracer().currentSpan()).isNull();
then(this.accumulator.getSpans()).hasSize(1);
then(this.accumulator.getSpans().get(0).tags()).containsKey("http.url");
}
@Test
public void should_not_create_a_span_for_error_controller() {
try {
@@ -146,7 +154,12 @@ public class TraceFilterWebIntegrationTests {
public static class Config {
@Bean
ExceptionThrowingController controller() {
GoodController goodController() {
return new GoodController();
}
@Bean
ExceptionThrowingController badController() {
return new ExceptionThrowingController();
}
@@ -160,6 +173,19 @@ public class TraceFilterWebIntegrationTests {
return Sampler.ALWAYS_SAMPLE;
}
// tag::custom_parser[]
@Bean(name = { HttpClientRequestParser.NAME, HttpServerRequestParser.NAME })
HttpRequestParser sleuthHttpServerRequestParser() {
return (req, context, span) -> {
HttpRequestParser.DEFAULT.parse(req, context, span);
String url = req.url();
if (url != null) {
span.tag("http.url", url);
}
};
}
// end::custom_parser[]
// tag::custom_server_sampler[]
@Bean(name = HttpServerSampler.NAME)
SamplerFunction<HttpRequest> myHttpSampler(SkipPatternProvider provider) {
@@ -188,6 +214,16 @@ public class TraceFilterWebIntegrationTests {
}
@RestController
public static class GoodController {
@RequestMapping("/good")
public String beGood() {
return "good";
}
}
@RestController
public static class ExceptionThrowingController {

View File

@@ -28,6 +28,7 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
@@ -118,9 +119,13 @@ public class WebClientBraveTests
}
@Bean
WebClient.Builder webClientBuilder(HttpClient httpClient) {
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(httpClient));
ClientHttpConnector clientHttpConnector(HttpClient httpClient) {
return new ReactorClientHttpConnector(httpClient);
}
@Bean
WebClient.Builder webClientBuilder(ClientHttpConnector clientHttpConnector) {
return WebClient.builder().clientConnector(clientHttpConnector);
}
}