Skip to content

Commit 673d8cb

Browse files
author
Dave Syer
committed
Add integration sample and vanilla non-zipkin sample
1 parent 405e07f commit 673d8cb

File tree

22 files changed

+689
-60
lines changed

22 files changed

+689
-60
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<modules>
2929
<module>spring-cloud-sleuth-core</module>
3030
<module>spring-cloud-sleuth-zipkin</module>
31-
<module>spring-cloud-sleuth-sample</module>
31+
<module>spring-cloud-sleuth-samples</module>
3232
<module>docs</module>
3333
</modules>
3434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.sleuth.instrument.integration;
18+
19+
import static org.springframework.cloud.sleuth.Trace.PARENT_ID_NAME;
20+
import static org.springframework.cloud.sleuth.Trace.PROCESS_ID_NAME;
21+
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
22+
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
23+
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
24+
25+
import java.util.HashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
import org.springframework.cloud.sleuth.Span;
30+
import org.springframework.integration.support.MessageBuilder;
31+
import org.springframework.messaging.Message;
32+
33+
/**
34+
* @author Dave Syer
35+
*
36+
*/
37+
public class SpanMessageHeaders {
38+
39+
public static Message<?> addSpanHeaders(Message<?> message, Span span) {
40+
if (span==null) {
41+
return message;
42+
}
43+
Map<String, String> headers = new HashMap<String, String>();
44+
addHeader(headers, TRACE_ID_NAME, span.getTraceId());
45+
addHeader(headers, SPAN_ID_NAME, span.getSpanId());
46+
addHeader(headers, PARENT_ID_NAME, getFirst(span.getParents()));
47+
addHeader(headers, SPAN_NAME_NAME, span.getName());
48+
addHeader(headers, PROCESS_ID_NAME, span.getProcessId());
49+
return MessageBuilder.fromMessage(message).copyHeaders(headers).build();
50+
}
51+
52+
private static void addHeader(Map<String, String> headers, String name, String value) {
53+
if (value!=null) {
54+
headers.put(name, value);
55+
}
56+
}
57+
58+
private static String getFirst(List<String> parents) {
59+
return parents==null || parents.isEmpty() ? null : parents.get(0);
60+
}
61+
62+
63+
}

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/integration/TraceChannelInterceptor.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,12 @@
2323
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
2424
import static org.springframework.util.StringUtils.hasText;
2525

26-
import java.util.HashMap;
27-
import java.util.List;
28-
import java.util.Map;
29-
3026
import org.springframework.cloud.sleuth.MilliSpan;
3127
import org.springframework.cloud.sleuth.MilliSpan.MilliSpanBuilder;
32-
import org.springframework.cloud.sleuth.NullScope;
3328
import org.springframework.cloud.sleuth.Trace;
3429
import org.springframework.cloud.sleuth.TraceContextHolder;
3530
import org.springframework.cloud.sleuth.TraceScope;
3631
import org.springframework.integration.context.IntegrationObjectSupport;
37-
import org.springframework.integration.support.MessageBuilder;
3832
import org.springframework.messaging.Message;
3933
import org.springframework.messaging.MessageChannel;
4034
import org.springframework.messaging.support.ChannelInterceptorAdapter;
@@ -54,7 +48,7 @@ public TraceChannelInterceptor(Trace trace) {
5448
@Override
5549
public Message<?> preSend(Message<?> message, MessageChannel channel) {
5650
if (TraceContextHolder.isTracing()) {
57-
return message;
51+
return SpanMessageHeaders.addSpanHeaders(message, TraceContextHolder.getCurrentSpan());
5852
}
5953
String spanId = getHeader(message, SPAN_ID_NAME);
6054
String traceId = getHeader(message, TRACE_ID_NAME);
@@ -67,9 +61,9 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
6761
MilliSpanBuilder span = MilliSpan.builder().traceId(traceId).spanId(spanId);
6862
String parentId = getHeader(message, PARENT_ID_NAME);
6963
String processId = getHeader(message, PROCESS_ID_NAME);
70-
String parentName = getHeader(message, SPAN_NAME_NAME);
71-
if (parentName != null) {
72-
span.name(parentName);
64+
String spanName = getHeader(message, SPAN_NAME_NAME);
65+
if (spanName != null) {
66+
span.name(spanName);
7367
}
7468
if (processId != null) {
7569
span.processId(processId);
@@ -85,27 +79,7 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
8579
else {
8680
traceScope = this.trace.startSpan(name);
8781
}
88-
if (traceScope == NullScope.INSTANCE) {
89-
return message;
90-
} else {
91-
Map<String, String> headers = new HashMap<String, String>();
92-
addHeader(headers, TRACE_ID_NAME, traceScope.getSpan().getTraceId());
93-
addHeader(headers, SPAN_ID_NAME, traceScope.getSpan().getSpanId());
94-
addHeader(headers, PARENT_ID_NAME, getFirst(traceScope.getSpan().getParents()));
95-
addHeader(headers, SPAN_NAME_NAME, traceScope.getSpan().getName());
96-
addHeader(headers, PROCESS_ID_NAME, traceScope.getSpan().getProcessId());
97-
return MessageBuilder.fromMessage(message).copyHeaders(headers).build();
98-
}
99-
}
100-
101-
private void addHeader(Map<String, String> headers, String name, String value) {
102-
if (value!=null) {
103-
headers.put(name, value);
104-
}
105-
}
106-
107-
private String getFirst(List<String> parents) {
108-
return parents==null || parents.isEmpty() ? null : parents.get(0);
82+
return SpanMessageHeaders.addSpanHeaders(message, traceScope.getSpan());
10983
}
11084

11185
private String getHeader(Message<?> message, String name) {

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/integration/TraceContextPropagationChannelInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public MessageHeaders getHeaders() {
174174

175175
@Override
176176
public String toString() {
177-
return "MessageWithThreadState{" + "message=" + this.message + ", span="
177+
return "MessageWithSpan{" + "message=" + this.message + ", span="
178178
+ this.span + ", messageHeaders=" + this.messageHeaders + '}';
179179
}
180180

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/integration/TraceChannelInterceptorTests.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,26 @@
2121
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
2222

2323
import org.junit.After;
24+
import org.junit.Before;
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.beans.factory.annotation.Qualifier;
2829
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
29-
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
3030
import org.springframework.boot.test.IntegrationTest;
3131
import org.springframework.boot.test.SpringApplicationConfiguration;
32+
import org.springframework.cloud.sleuth.Trace;
3233
import org.springframework.cloud.sleuth.TraceContextHolder;
33-
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
34+
import org.springframework.cloud.sleuth.TraceScope;
3435
import org.springframework.cloud.sleuth.instrument.integration.TraceChannelInterceptorTests.App;
3536
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
3637
import org.springframework.context.annotation.Bean;
3738
import org.springframework.context.annotation.Configuration;
38-
import org.springframework.integration.annotation.MessageEndpoint;
39-
import org.springframework.integration.channel.QueueChannel;
40-
import org.springframework.integration.config.EnableIntegration;
39+
import org.springframework.integration.channel.DirectChannel;
4140
import org.springframework.integration.support.MessageBuilder;
4241
import org.springframework.messaging.Message;
43-
import org.springframework.messaging.PollableChannel;
42+
import org.springframework.messaging.MessageHandler;
43+
import org.springframework.messaging.MessagingException;
4444
import org.springframework.test.annotation.DirtiesContext;
4545
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
4646

@@ -51,43 +51,66 @@
5151
@SpringApplicationConfiguration(classes=App.class)
5252
@IntegrationTest
5353
@DirtiesContext
54-
public class TraceChannelInterceptorTests {
54+
public class TraceChannelInterceptorTests implements MessageHandler {
5555

5656
@Autowired
5757
@Qualifier("channel")
58-
private PollableChannel channel;
58+
private DirectChannel channel;
59+
60+
@Autowired
61+
private Trace trace;
62+
63+
private Message<?> message;
64+
65+
@Override
66+
public void handleMessage(Message<?> message) throws MessagingException {
67+
this.message = message;
68+
}
69+
70+
@Before
71+
public void init() {
72+
this.channel.subscribe(this);
73+
}
5974

6075
@After
6176
public void close() {
6277
TraceContextHolder.setCurrentSpan(null);
78+
this.channel.unsubscribe(this);
6379
}
6480

6581
@Test
6682
public void testSpanCreation() {
67-
6883
this.channel.send(MessageBuilder.withPayload("hi").build());
84+
assertNotNull("message was null", this.message);
6985

70-
Message<?> message = this.channel.receive(0);
86+
String spanId = this.message.getHeaders().get(SPAN_ID_NAME, String.class);
87+
assertNotNull("spanId was null", spanId);
7188

72-
assertNotNull("message was null", message);
89+
String traceId = this.message.getHeaders().get(TRACE_ID_NAME, String.class);
90+
assertNotNull("traceId was null", traceId);
91+
}
92+
93+
@Test
94+
public void testHeaderCreation() {
95+
TraceScope traceScope = this.trace.startSpan("testSendMessage", new AlwaysSampler(), null);
96+
this.channel.send(MessageBuilder.withPayload("hi").build());
97+
traceScope.close();
98+
assertNotNull("message was null", this.message);
7399

74-
String spanId = message.getHeaders().get(SPAN_ID_NAME, String.class);
100+
String spanId = this.message.getHeaders().get(SPAN_ID_NAME, String.class);
75101
assertNotNull("spanId was null", spanId);
76102

77-
String traceId = message.getHeaders().get(TRACE_ID_NAME, String.class);
103+
String traceId = this.message.getHeaders().get(TRACE_ID_NAME, String.class);
78104
assertNotNull("traceId was null", traceId);
79105
}
80106

81107
@Configuration
82108
@EnableAutoConfiguration
83-
@MessageEndpoint
84-
@EnableIntegration
85-
@ImportAutoConfiguration({TraceSpringIntegrationAutoConfiguration.class, TraceAutoConfiguration.class})
86109
static class App {
87110

88111
@Bean
89-
public QueueChannel channel() {
90-
return new QueueChannel();
112+
public DirectChannel channel() {
113+
return new DirectChannel();
91114
}
92115

93116
@Bean

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/integration/TraceContextPropagationChannelInterceptorTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
3737
import org.springframework.context.annotation.Bean;
3838
import org.springframework.context.annotation.Configuration;
39-
import org.springframework.integration.annotation.MessageEndpoint;
4039
import org.springframework.integration.channel.QueueChannel;
41-
import org.springframework.integration.config.EnableIntegration;
4240
import org.springframework.integration.support.MessageBuilder;
4341
import org.springframework.messaging.Message;
4442
import org.springframework.messaging.PollableChannel;
@@ -87,8 +85,6 @@ public void testSpanPropagation() {
8785

8886
@Configuration
8987
@EnableAutoConfiguration
90-
@MessageEndpoint
91-
@EnableIntegration
9288
static class App {
9389

9490
@Bean

spring-cloud-sleuth-samples/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>spring-cloud-sleuth-samples</artifactId>
8+
<packaging>pom</packaging>
9+
<name>Spring Cloud Sleuth Samples</name>
10+
<description>Spring Cloud Sleuth Samples</description>
11+
12+
<parent>
13+
<groupId>org.springframework.cloud</groupId>
14+
<artifactId>spring-cloud-sleuth</artifactId>
15+
<version>1.1.0.BUILD-SNAPSHOT</version>
16+
</parent>
17+
18+
<modules>
19+
<module>spring-cloud-sleuth-sample</module>
20+
<module>spring-cloud-sleuth-sample-messaging</module>
21+
<module>spring-cloud-sleuth-sample-zipkin</module>
22+
</modules>
23+
24+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>spring-cloud-sleuth-sample-messaging</artifactId>
8+
<packaging>jar</packaging>
9+
<name>spring-cloud-sleuth-sample-messaging</name>
10+
<description>Spring Cloud Sleuth Sample</description>
11+
12+
<parent>
13+
<groupId>org.springframework.cloud</groupId>
14+
<artifactId>spring-cloud-sleuth</artifactId>
15+
<version>1.0.0.BUILD-SNAPSHOT</version>
16+
<relativePath>..</relativePath>
17+
</parent>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-maven-plugin</artifactId>
24+
<executions>
25+
<execution>
26+
<goals>
27+
<goal>repackage</goal>
28+
</goals>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
<plugin>
33+
<!--skip deploy -->
34+
<artifactId>maven-deploy-plugin</artifactId>
35+
<configuration>
36+
<skip>true</skip>
37+
</configuration>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
42+
<dependencies>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-integration</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.cloud</groupId>
53+
<artifactId>spring-cloud-sleuth-core</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework.cloud</groupId>
57+
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-starter-aop</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-actuator</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.projectlombok</groupId>
69+
<artifactId>lombok</artifactId>
70+
</dependency>
71+
</dependencies>
72+
73+
</project>

0 commit comments

Comments
 (0)