@@ -16,9 +16,12 @@
|
||||
|
||||
package org.springframework.cloud.sleuth.trace;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.cloud.sleuth.Sampler;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.SpanNamer;
|
||||
@@ -38,6 +41,8 @@ import org.springframework.cloud.sleuth.util.SpanNameUtil;
|
||||
*/
|
||||
public class DefaultTracer implements Tracer {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass());
|
||||
|
||||
private static final int MAX_CHARS_IN_SPAN_NAME = 50;
|
||||
|
||||
private final Sampler defaultSampler;
|
||||
@@ -102,17 +107,18 @@ public class DefaultTracer implements Tracer {
|
||||
return continueSpan(span);
|
||||
}
|
||||
|
||||
private String shortenNameIfNecessary(String name) {
|
||||
int maxLength = name.length() > MAX_CHARS_IN_SPAN_NAME ? MAX_CHARS_IN_SPAN_NAME : name.length();
|
||||
return name.substring(0, maxLength);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Span detach(Span span) {
|
||||
if (span == null) {
|
||||
return null;
|
||||
}
|
||||
Span cur = SpanContextHolder.getCurrentSpan();
|
||||
if (cur == null) {
|
||||
if (log.isTraceEnabled()) {
|
||||
log.trace("Span in the context is null so something has already detached the span. Won't do anything about it");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (!span.equals(cur)) {
|
||||
ExceptionUtils.warn("Tried to detach trace span but " + "it is not the current span: " + span
|
||||
+ ". You may have forgotten to close or detach " + cur);
|
||||
|
||||
@@ -22,9 +22,11 @@ import java.util.Random;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.test.rule.OutputCapture;
|
||||
import org.springframework.cloud.sleuth.DefaultSpanNamer;
|
||||
import org.springframework.cloud.sleuth.Span;
|
||||
import org.springframework.cloud.sleuth.SpanNamer;
|
||||
@@ -54,6 +56,7 @@ public class DefaultTracerTests {
|
||||
private SpanNamer spanNamer = new DefaultSpanNamer();
|
||||
private SpanLogger spanLogger = Mockito.mock(SpanLogger.class);
|
||||
private SpanReporter spanReporter = Mockito.mock(SpanReporter.class);
|
||||
@Rule public OutputCapture capture = new OutputCapture();
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@@ -206,6 +209,18 @@ public class DefaultTracerTests {
|
||||
then(child.getName().length()).isEqualTo(50);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldNotProduceAWarningMessageWhenThereIsNoSpanInContextAndWeDetachASpan() {
|
||||
DefaultTracer tracer = new DefaultTracer(new AlwaysSampler(), new Random(),
|
||||
this.spanNamer, this.spanLogger, this.spanReporter);
|
||||
Span span = Span.builder().name("foo").traceId(1L).spanId(1L).build();
|
||||
|
||||
Span child = tracer.detach(span);
|
||||
|
||||
then(child).isNull();
|
||||
then(this.capture.toString()).doesNotContain("Tried to detach trace span");
|
||||
}
|
||||
|
||||
private String bigName() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 60; i++) {
|
||||
|
||||
Reference in New Issue
Block a user