Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Avoid warning if traceparent not present #698

Merged
merged 2 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public JaegerSpanContext extract(TextMap carrier) {
traceState = entry.getValue();
}
}
if (traceParent == null) {
return null;
}
return extractContextFromTraceParent(traceParent, traceState);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,44 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import io.jaegertracing.internal.JaegerSpanContext;
import io.opentracing.propagation.TextMapAdapter;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

public class TraceContextCodecTest {

private static final JaegerSpanContext SPAN_CONTEXT =
new JaegerSpanContext(0, 1, 2, 3, (byte)0);
private static final String EXAMPLE_TRACE_PARENT = "00-00000000000000000000000000000001-0000000000000002-00";
private static PrintStream sysout;

private TraceContextCodec traceContextCodec = new TraceContextCodec.Builder().build();

@BeforeClass
public static void setup() {
sysout = System.out;
System.setOut(Mockito.spy(sysout));
}

@AfterClass
public static void cleanup() {
System.setOut(sysout);
}

@Test
public void support128BitTraceIdExtraction() {
String hex128Bits = "463ac35c9f6413ad48485a3953bb6124";
Expand Down Expand Up @@ -90,13 +113,15 @@ public void testInvalidTraceId() {
textMap.put(TRACE_PARENT, "00-00000000000000000000000000000000-0000000000000002-00");
JaegerSpanContext spanContext = traceContextCodec.extract(textMap);
assertNull(spanContext);
verifyWarningPresent();
}

@Test
public void testNoTraceHeader() {
TextMapAdapter textMap = new TextMapAdapter(new HashMap<>());
JaegerSpanContext spanContext = traceContextCodec.extract(textMap);
assertNull(spanContext);
verifyWarningNotPresent();
}

@Test
Expand All @@ -105,6 +130,7 @@ public void testInvalidParentId() {
textMap.put(TRACE_PARENT, "00-00000000000000000000000000000001-0000000000000000-00");
JaegerSpanContext spanContext = traceContextCodec.extract(textMap);
assertNull(spanContext);
verifyWarningPresent();
}

@Test
Expand Down Expand Up @@ -133,4 +159,27 @@ public void testEmptyTraceStateNotPropagated() {
assertEquals(1, injectCarrier.size());
assertEquals(EXAMPLE_TRACE_PARENT, injectCarrier.get(TRACE_PARENT));
}

private void verifyWarningPresent() {
ArgumentCaptor<byte[]> captor = ArgumentCaptor.forClass(byte[].class);
try {
verify(System.out).write(captor.capture());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
reset(System.out);
}
String message = new String(captor.getValue());
assertTrue(message.contains("Unparseable traceparent header."));
}

private void verifyWarningNotPresent() {
try {
verify(System.out, times(0)).write(any());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
reset(System.out);
}
}
}