Add integration sample and vanilla non-zipkin sample
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://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.integration;
|
||||
|
||||
import static org.springframework.cloud.sleuth.Trace.PARENT_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.PROCESS_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SpanMessageHeaders {
|
||||
|
||||
public static Message<?> addSpanHeaders(Message<?> message, Span span) {
|
||||
if (span==null) {
|
||||
return message;
|
||||
}
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
addHeader(headers, TRACE_ID_NAME, span.getTraceId());
|
||||
addHeader(headers, SPAN_ID_NAME, span.getSpanId());
|
||||
addHeader(headers, PARENT_ID_NAME, getFirst(span.getParents()));
|
||||
addHeader(headers, SPAN_NAME_NAME, span.getName());
|
||||
addHeader(headers, PROCESS_ID_NAME, span.getProcessId());
|
||||
return MessageBuilder.fromMessage(message).copyHeaders(headers).build();
|
||||
}
|
||||
|
||||
private static void addHeader(Map<String, String> headers, String name, String value) {
|
||||
if (value!=null) {
|
||||
headers.put(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getFirst(List<String> parents) {
|
||||
return parents==null || parents.isEmpty() ? null : parents.get(0);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -23,18 +23,12 @@ import static org.springframework.cloud.sleuth.Trace.SPAN_NAME_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
|
||||
import static org.springframework.util.StringUtils.hasText;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.cloud.sleuth.MilliSpan;
|
||||
import org.springframework.cloud.sleuth.MilliSpan.MilliSpanBuilder;
|
||||
import org.springframework.cloud.sleuth.NullScope;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceContextHolder;
|
||||
import org.springframework.cloud.sleuth.TraceScope;
|
||||
import org.springframework.integration.context.IntegrationObjectSupport;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.support.ChannelInterceptorAdapter;
|
||||
@@ -54,7 +48,7 @@ public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
@Override
|
||||
public Message<?> preSend(Message<?> message, MessageChannel channel) {
|
||||
if (TraceContextHolder.isTracing()) {
|
||||
return message;
|
||||
return SpanMessageHeaders.addSpanHeaders(message, TraceContextHolder.getCurrentSpan());
|
||||
}
|
||||
String spanId = getHeader(message, SPAN_ID_NAME);
|
||||
String traceId = getHeader(message, TRACE_ID_NAME);
|
||||
@@ -67,9 +61,9 @@ public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
MilliSpanBuilder span = MilliSpan.builder().traceId(traceId).spanId(spanId);
|
||||
String parentId = getHeader(message, PARENT_ID_NAME);
|
||||
String processId = getHeader(message, PROCESS_ID_NAME);
|
||||
String parentName = getHeader(message, SPAN_NAME_NAME);
|
||||
if (parentName != null) {
|
||||
span.name(parentName);
|
||||
String spanName = getHeader(message, SPAN_NAME_NAME);
|
||||
if (spanName != null) {
|
||||
span.name(spanName);
|
||||
}
|
||||
if (processId != null) {
|
||||
span.processId(processId);
|
||||
@@ -85,27 +79,7 @@ public class TraceChannelInterceptor extends ChannelInterceptorAdapter {
|
||||
else {
|
||||
traceScope = this.trace.startSpan(name);
|
||||
}
|
||||
if (traceScope == NullScope.INSTANCE) {
|
||||
return message;
|
||||
} else {
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
addHeader(headers, TRACE_ID_NAME, traceScope.getSpan().getTraceId());
|
||||
addHeader(headers, SPAN_ID_NAME, traceScope.getSpan().getSpanId());
|
||||
addHeader(headers, PARENT_ID_NAME, getFirst(traceScope.getSpan().getParents()));
|
||||
addHeader(headers, SPAN_NAME_NAME, traceScope.getSpan().getName());
|
||||
addHeader(headers, PROCESS_ID_NAME, traceScope.getSpan().getProcessId());
|
||||
return MessageBuilder.fromMessage(message).copyHeaders(headers).build();
|
||||
}
|
||||
}
|
||||
|
||||
private void addHeader(Map<String, String> headers, String name, String value) {
|
||||
if (value!=null) {
|
||||
headers.put(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
private String getFirst(List<String> parents) {
|
||||
return parents==null || parents.isEmpty() ? null : parents.get(0);
|
||||
return SpanMessageHeaders.addSpanHeaders(message, traceScope.getSpan());
|
||||
}
|
||||
|
||||
private String getHeader(Message<?> message, String name) {
|
||||
|
||||
@@ -174,7 +174,7 @@ implements ExecutorChannelInterceptor {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MessageWithThreadState{" + "message=" + this.message + ", span="
|
||||
return "MessageWithSpan{" + "message=" + this.message + ", span="
|
||||
+ this.span + ", messageHeaders=" + this.messageHeaders + '}';
|
||||
}
|
||||
|
||||
|
||||
@@ -21,26 +21,26 @@ import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.cloud.sleuth.Trace;
|
||||
import org.springframework.cloud.sleuth.TraceContextHolder;
|
||||
import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
|
||||
import org.springframework.cloud.sleuth.TraceScope;
|
||||
import org.springframework.cloud.sleuth.instrument.integration.TraceChannelInterceptorTests.App;
|
||||
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.annotation.MessageEndpoint;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.config.EnableIntegration;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -51,43 +51,66 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@SpringApplicationConfiguration(classes=App.class)
|
||||
@IntegrationTest
|
||||
@DirtiesContext
|
||||
public class TraceChannelInterceptorTests {
|
||||
public class TraceChannelInterceptorTests implements MessageHandler {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("channel")
|
||||
private PollableChannel channel;
|
||||
private DirectChannel channel;
|
||||
|
||||
@Autowired
|
||||
private Trace trace;
|
||||
|
||||
private Message<?> message;
|
||||
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.channel.subscribe(this);
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
TraceContextHolder.setCurrentSpan(null);
|
||||
this.channel.unsubscribe(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpanCreation() {
|
||||
|
||||
this.channel.send(MessageBuilder.withPayload("hi").build());
|
||||
assertNotNull("message was null", this.message);
|
||||
|
||||
Message<?> message = this.channel.receive(0);
|
||||
|
||||
assertNotNull("message was null", message);
|
||||
|
||||
String spanId = message.getHeaders().get(SPAN_ID_NAME, String.class);
|
||||
String spanId = this.message.getHeaders().get(SPAN_ID_NAME, String.class);
|
||||
assertNotNull("spanId was null", spanId);
|
||||
|
||||
String traceId = message.getHeaders().get(TRACE_ID_NAME, String.class);
|
||||
String traceId = this.message.getHeaders().get(TRACE_ID_NAME, String.class);
|
||||
assertNotNull("traceId was null", traceId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderCreation() {
|
||||
TraceScope traceScope = this.trace.startSpan("testSendMessage", new AlwaysSampler(), null);
|
||||
this.channel.send(MessageBuilder.withPayload("hi").build());
|
||||
traceScope.close();
|
||||
assertNotNull("message was null", this.message);
|
||||
|
||||
String spanId = this.message.getHeaders().get(SPAN_ID_NAME, String.class);
|
||||
assertNotNull("spanId was null", spanId);
|
||||
|
||||
String traceId = this.message.getHeaders().get(TRACE_ID_NAME, String.class);
|
||||
assertNotNull("traceId was null", traceId);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@MessageEndpoint
|
||||
@EnableIntegration
|
||||
@ImportAutoConfiguration({TraceSpringIntegrationAutoConfiguration.class, TraceAutoConfiguration.class})
|
||||
static class App {
|
||||
|
||||
@Bean
|
||||
public QueueChannel channel() {
|
||||
return new QueueChannel();
|
||||
public DirectChannel channel() {
|
||||
return new DirectChannel();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -36,9 +36,7 @@ import org.springframework.cloud.sleuth.instrument.integration.TraceContextPropa
|
||||
import org.springframework.cloud.sleuth.sampler.AlwaysSampler;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.annotation.MessageEndpoint;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.config.EnableIntegration;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.PollableChannel;
|
||||
@@ -87,8 +85,6 @@ public class TraceContextPropagationChannelInterceptorTests {
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@MessageEndpoint
|
||||
@EnableIntegration
|
||||
static class App {
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user