From 3ad52db24f230086cefd1de7ec04349011025d70 Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Fri, 17 Aug 2018 13:32:09 -0400 Subject: [PATCH 1/8] Reduce visibility of TextMapCodec.contextFromString to package scope Signed-off-by: Isaac Hier --- .../internal/propagation/TextMapCodec.java | 2 +- .../internal/JaegerSpanContextTest.java | 27 -------------- .../internal/JaegerSpanTest.java | 12 ------- .../propagation/TextMapCodecTest.java | 35 +++++++++++++++++++ 4 files changed, 36 insertions(+), 40 deletions(-) diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java b/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java index 7cc3a9352..4a057ba49 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java @@ -57,7 +57,7 @@ private TextMapCodec(Builder builder) { this.baggagePrefix = builder.baggagePrefix; } - public static JaegerSpanContext contextFromString(String value) + static JaegerSpanContext contextFromString(String value) throws MalformedTracerStateStringException, EmptyTracerStateStringException { if (value == null || value.equals("")) { throw new EmptyTracerStateStringException(); diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java index 895aa6bd7..5aa3bf86c 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java @@ -16,32 +16,10 @@ import static org.junit.Assert.assertEquals; -import io.jaegertracing.internal.exceptions.EmptyTracerStateStringException; -import io.jaegertracing.internal.exceptions.MalformedTracerStateStringException; -import io.jaegertracing.internal.propagation.TextMapCodec; import org.junit.Test; public class JaegerSpanContextTest { - @Test(expected = MalformedTracerStateStringException.class) - public void testContextFromStringMalformedException() throws Exception { - TextMapCodec.contextFromString("ff:ff:ff"); - } - - @Test(expected = EmptyTracerStateStringException.class) - public void testContextFromStringEmptyException() throws Exception { - TextMapCodec.contextFromString(""); - } - - @Test - public void testContextFromString() throws Exception { - JaegerSpanContext context = TextMapCodec.contextFromString("ff:dd:cc:4"); - assertEquals(context.getTraceId(), 255); - assertEquals(context.getSpanId(), 221); - assertEquals(context.getParentId(), 204); - assertEquals(context.getFlags(), 4); - } - @Test public void testToStringFormatsPositiveFields() { long traceId = -10L; @@ -57,10 +35,5 @@ public void testToStringFormatsPositiveFields() { assertEquals( "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.contextAsString()); - JaegerSpanContext contextFromStr = TextMapCodec.contextFromString(context.contextAsString()); - assertEquals(traceId, contextFromStr.getTraceId()); - assertEquals(spanId, contextFromStr.getSpanId()); - assertEquals(parentId, contextFromStr.getParentId()); - assertEquals(flags, contextFromStr.getFlags()); } } diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java index 8de0a3b0c..236a69d0e 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -208,18 +208,6 @@ public void testWithoutTimestampsInaccurateClock() { assertEquals(10, jaegerSpan.getDuration()); } - @Test - public void testSpanToString() { - JaegerSpan jaegerSpan = tracer.buildSpan("test-operation").start(); - JaegerSpanContext expectedContext = jaegerSpan.context(); - JaegerSpanContext actualContext = TextMapCodec.contextFromString(expectedContext.contextAsString()); - - assertEquals(expectedContext.getTraceId(), actualContext.getTraceId()); - assertEquals(expectedContext.getSpanId(), actualContext.getSpanId()); - assertEquals(expectedContext.getParentId(), actualContext.getParentId()); - assertEquals(expectedContext.getFlags(), actualContext.getFlags()); - } - @Test public void testOperationName() { String expectedOperation = "leela"; diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java index c0a64345e..b8a895c19 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java @@ -14,9 +14,15 @@ package io.jaegertracing.internal.propagation; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import io.jaegertracing.internal.JaegerSpan; +import io.jaegertracing.internal.JaegerSpanContext; +import io.jaegertracing.internal.exceptions.EmptyTracerStateStringException; +import io.jaegertracing.internal.exceptions.MalformedTracerStateStringException; +import java.util.Collections; import org.junit.Test; public class TextMapCodecTest { @@ -44,4 +50,33 @@ public void testWithoutBuilder() { assertTrue(str.contains("urlEncoding=false")); } + @Test + public void testSpanToString() { + JaegerSpanContext expectedContext = new JaegerSpanContext(1, 2, 3, (byte) 1); + JaegerSpanContext actualContext = TextMapCodec.contextFromString(expectedContext.contextAsString()); + + assertEquals(expectedContext.getTraceId(), actualContext.getTraceId()); + assertEquals(expectedContext.getSpanId(), actualContext.getSpanId()); + assertEquals(expectedContext.getParentId(), actualContext.getParentId()); + assertEquals(expectedContext.getFlags(), actualContext.getFlags()); + } + + @Test(expected = MalformedTracerStateStringException.class) + public void testContextFromStringMalformedException() throws Exception { + TextMapCodec.contextFromString("ff:ff:ff"); + } + + @Test(expected = EmptyTracerStateStringException.class) + public void testContextFromStringEmptyException() throws Exception { + TextMapCodec.contextFromString(""); + } + + @Test + public void testContextFromString() throws Exception { + JaegerSpanContext context = TextMapCodec.contextFromString("ff:dd:cc:4"); + assertEquals(context.getTraceId(), 255); + assertEquals(context.getSpanId(), 221); + assertEquals(context.getParentId(), 204); + assertEquals(context.getFlags(), 4); + } } From a1cb91144b3986e57cd7247176799f8f865c49b0 Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Fri, 17 Aug 2018 13:45:30 -0400 Subject: [PATCH 2/8] Get rid of JaegerSpanContextTest Signed-off-by: Isaac Hier --- .../internal/JaegerSpanContextTest.java | 39 ------------------- .../propagation/TextMapCodecTest.java | 22 +++++++++++ 2 files changed, 22 insertions(+), 39 deletions(-) delete mode 100644 jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java deleted file mode 100644 index 5aa3bf86c..000000000 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2016, Uber Technologies, Inc - * - * 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 io.jaegertracing.internal; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -public class JaegerSpanContextTest { - - @Test - public void testToStringFormatsPositiveFields() { - long traceId = -10L; - long spanId = -10L; - long parentId = -10L; - byte flags = (byte) 129; - - // I use MIN_VALUE because the most significant bit, and thats when - // we want to make sure the hex number is positive. - JaegerSpanContext context = new JaegerSpanContext(traceId, spanId, parentId, flags); - - context.contextAsString().split(":"); - - assertEquals( - "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.contextAsString()); - } -} diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java index b8a895c19..3df0730a4 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java @@ -79,4 +79,26 @@ public void testContextFromString() throws Exception { assertEquals(context.getParentId(), 204); assertEquals(context.getFlags(), 4); } + + @Test + public void testToStringFormatsPositiveFields() { + long traceId = -10L; + long spanId = -10L; + long parentId = -10L; + byte flags = (byte) 129; + + // I use MIN_VALUE because the most significant bit, and thats when + // we want to make sure the hex number is positive. + JaegerSpanContext context = new JaegerSpanContext(traceId, spanId, parentId, flags); + + context.contextAsString().split(":"); + + assertEquals( + "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.contextAsString()); + JaegerSpanContext contextFromStr = TextMapCodec.contextFromString(context.contextAsString()); + assertEquals(traceId, contextFromStr.getTraceId()); + assertEquals(spanId, contextFromStr.getSpanId()); + assertEquals(parentId, contextFromStr.getParentId()); + assertEquals(flags, contextFromStr.getFlags()); + } } From 843c9370c6361124ec15fb21ba40ebeb5602db84 Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Fri, 17 Aug 2018 13:54:09 -0400 Subject: [PATCH 3/8] Remove outdated comment Signed-off-by: Isaac Hier --- .../io/jaegertracing/internal/propagation/TextMapCodecTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java index 3df0730a4..ffad01961 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java @@ -87,8 +87,6 @@ public void testToStringFormatsPositiveFields() { long parentId = -10L; byte flags = (byte) 129; - // I use MIN_VALUE because the most significant bit, and thats when - // we want to make sure the hex number is positive. JaegerSpanContext context = new JaegerSpanContext(traceId, spanId, parentId, flags); context.contextAsString().split(":"); From 88470201fffdeeb7b2c3cadd09ee6041bf362bac Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Mon, 20 Aug 2018 11:30:38 -0400 Subject: [PATCH 4/8] Change contextAsString to package scope Signed-off-by: Isaac Hier --- .../internal/JaegerSpanContext.java | 2 +- .../internal/propagation/TextMapCodec.java | 2 +- .../internal/JaegerSpanTest.java | 36 ++++++++++++------- .../propagation/TextMapCodecTest.java | 17 ++------- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java index 413e0bca2..31b62619d 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java @@ -92,7 +92,7 @@ public boolean isDebug() { return (flags & flagDebug) == flagDebug; } - public String contextAsString() { + String contextAsString() { int intFlag = flags & 0xFF; return new StringBuilder() .append(Long.toHexString(traceId)).append(":") diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java b/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java index 4a057ba49..b5cae5416 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java @@ -78,7 +78,7 @@ static JaegerSpanContext contextFromString(String value) @Override public void inject(JaegerSpanContext spanContext, TextMap carrier) { - carrier.put(contextKey, encodedValue(spanContext.contextAsString())); + carrier.put(contextKey, encodedValue(spanContext.toString())); for (Map.Entry entry : spanContext.baggageItems()) { carrier.put(keys.prefixedKey(entry.getKey(), baggagePrefix), encodedValue(entry.getValue())); } diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java index 236a69d0e..889837fe3 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -14,20 +14,11 @@ package io.jaegertracing.internal; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - import io.jaegertracing.internal.baggage.DefaultBaggageRestrictionManager; import io.jaegertracing.internal.baggage.Restriction; import io.jaegertracing.internal.clock.Clock; import io.jaegertracing.internal.metrics.InMemoryMetricsFactory; import io.jaegertracing.internal.metrics.Metrics; -import io.jaegertracing.internal.propagation.TextMapCodec; import io.jaegertracing.internal.reporters.InMemoryReporter; import io.jaegertracing.internal.samplers.ConstSampler; import io.jaegertracing.spi.BaggageRestrictionManager; @@ -37,6 +28,10 @@ import io.opentracing.log.Fields; import io.opentracing.noop.NoopSpan; import io.opentracing.tag.Tags; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + import java.io.PrintWriter; import java.io.StringWriter; import java.util.Collections; @@ -45,9 +40,9 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; public class JaegerSpanTest { private Clock clock; @@ -200,7 +195,7 @@ public void testWithoutTimestampsInaccurateClock() { .thenThrow(new IllegalStateException("currentTimeMicros() called 2nd time")); when(clock.currentNanoTicks()).thenReturn(20000L).thenReturn(30000L); - JaegerSpan jaegerSpan = tracer.buildSpan("test-service-name").start(); + JaegerSpan jaegerSpan = tracer.buildSpan("test-operation").start(); jaegerSpan.finish(); assertEquals(1, reporter.getSpans().size()); @@ -208,6 +203,21 @@ public void testWithoutTimestampsInaccurateClock() { assertEquals(10, jaegerSpan.getDuration()); } + @Test + public void testSpanToString() { + String operation = "test-operation"; + JaegerSpan span = tracer.buildSpan(operation).start(); + String expectedString = String.format( + "%x:%x:%x:%x - %s", + span.context().getTraceId(), + span.context().getSpanId(), + span.context().getParentId(), + span.context().getFlags(), + operation); + assertEquals(expectedString, span.toString()); + span.finish(); + } + @Test public void testOperationName() { String expectedOperation = "leela"; diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java index ffad01961..80f57dbd2 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java @@ -50,17 +50,6 @@ public void testWithoutBuilder() { assertTrue(str.contains("urlEncoding=false")); } - @Test - public void testSpanToString() { - JaegerSpanContext expectedContext = new JaegerSpanContext(1, 2, 3, (byte) 1); - JaegerSpanContext actualContext = TextMapCodec.contextFromString(expectedContext.contextAsString()); - - assertEquals(expectedContext.getTraceId(), actualContext.getTraceId()); - assertEquals(expectedContext.getSpanId(), actualContext.getSpanId()); - assertEquals(expectedContext.getParentId(), actualContext.getParentId()); - assertEquals(expectedContext.getFlags(), actualContext.getFlags()); - } - @Test(expected = MalformedTracerStateStringException.class) public void testContextFromStringMalformedException() throws Exception { TextMapCodec.contextFromString("ff:ff:ff"); @@ -89,11 +78,11 @@ public void testToStringFormatsPositiveFields() { JaegerSpanContext context = new JaegerSpanContext(traceId, spanId, parentId, flags); - context.contextAsString().split(":"); + context.toString().split(":"); assertEquals( - "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.contextAsString()); - JaegerSpanContext contextFromStr = TextMapCodec.contextFromString(context.contextAsString()); + "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.toString()); + JaegerSpanContext contextFromStr = TextMapCodec.contextFromString(context.toString()); assertEquals(traceId, contextFromStr.getTraceId()); assertEquals(spanId, contextFromStr.getSpanId()); assertEquals(parentId, contextFromStr.getParentId()); From fa973eac6d95401586ee5ce124206295193ebece Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Mon, 20 Aug 2018 11:45:16 -0400 Subject: [PATCH 5/8] Style fixes Signed-off-by: Isaac Hier --- .../jaegertracing/internal/JaegerSpanTest.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java index 889837fe3..33d1fb09b 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -14,6 +14,14 @@ package io.jaegertracing.internal; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import io.jaegertracing.internal.baggage.DefaultBaggageRestrictionManager; import io.jaegertracing.internal.baggage.Restriction; import io.jaegertracing.internal.clock.Clock; @@ -28,10 +36,6 @@ import io.opentracing.log.Fields; import io.opentracing.noop.NoopSpan; import io.opentracing.tag.Tags; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - import java.io.PrintWriter; import java.io.StringWriter; import java.util.Collections; @@ -40,9 +44,9 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; public class JaegerSpanTest { private Clock clock; From 986986edaf5c90c70479d2cace0a527d46586bc3 Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Mon, 20 Aug 2018 15:39:47 -0400 Subject: [PATCH 6/8] Move contextAsString to public method of TextMapCodec Signed-off-by: Isaac Hier --- .../io/jaegertracing/internal/JaegerSpan.java | 2 +- .../internal/JaegerSpanContext.java | 16 ++-------------- .../internal/propagation/TextMapCodec.java | 17 ++++++++++++++++- .../jaegertracing/internal/JaegerSpanTest.java | 8 +------- .../internal/propagation/TextMapCodecTest.java | 9 ++------- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java index d7ab96d8a..5229bf23a 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpan.java @@ -147,7 +147,7 @@ public String getBaggageItem(String key) { @Override public String toString() { synchronized (this) { - return context.contextAsString() + " - " + operationName; + return context.toString() + " - " + operationName; } } diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java index 31b62619d..87d88aa24 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java @@ -14,10 +14,8 @@ package io.jaegertracing.internal; -import io.jaegertracing.internal.exceptions.EmptyTracerStateStringException; -import io.jaegertracing.internal.exceptions.MalformedTracerStateStringException; +import io.jaegertracing.internal.propagation.TextMapCodec; import io.opentracing.SpanContext; -import java.math.BigInteger; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -92,19 +90,9 @@ public boolean isDebug() { return (flags & flagDebug) == flagDebug; } - String contextAsString() { - int intFlag = flags & 0xFF; - return new StringBuilder() - .append(Long.toHexString(traceId)).append(":") - .append(Long.toHexString(spanId)).append(":") - .append(Long.toHexString(parentId)).append(":") - .append(Integer.toHexString(intFlag)) - .toString(); - } - @Override public String toString() { - return contextAsString(); + return TextMapCodec.contextAsString(this); } public JaegerSpanContext withBaggageItem(String key, String val) { diff --git a/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java b/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java index b5cae5416..d6d60df9a 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java @@ -76,9 +76,24 @@ static JaegerSpanContext contextFromString(String value) new BigInteger(parts[3], 16).byteValue()); } + /** + * Encode context into a string. + * @param context Span context to encode. + * @return Encoded string representing span context. + */ + public static String contextAsString(JaegerSpanContext context) { + int intFlag = context.getFlags() & 0xFF; + return new StringBuilder() + .append(Long.toHexString(context.getTraceId())).append(":") + .append(Long.toHexString(context.getSpanId())).append(":") + .append(Long.toHexString(context.getParentId())).append(":") + .append(Integer.toHexString(intFlag)) + .toString(); + } + @Override public void inject(JaegerSpanContext spanContext, TextMap carrier) { - carrier.put(contextKey, encodedValue(spanContext.toString())); + carrier.put(contextKey, encodedValue(contextAsString(spanContext))); for (Map.Entry entry : spanContext.baggageItems()) { carrier.put(keys.prefixedKey(entry.getKey(), baggagePrefix), encodedValue(entry.getValue())); } diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java index 33d1fb09b..78c560fd2 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -211,13 +211,7 @@ public void testWithoutTimestampsInaccurateClock() { public void testSpanToString() { String operation = "test-operation"; JaegerSpan span = tracer.buildSpan(operation).start(); - String expectedString = String.format( - "%x:%x:%x:%x - %s", - span.context().getTraceId(), - span.context().getSpanId(), - span.context().getParentId(), - span.context().getFlags(), - operation); + String expectedString = span.context().toString() + " - " + operation; assertEquals(expectedString, span.toString()); span.finish(); } diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java index 80f57dbd2..28874e909 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java @@ -18,11 +18,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import io.jaegertracing.internal.JaegerSpan; import io.jaegertracing.internal.JaegerSpanContext; import io.jaegertracing.internal.exceptions.EmptyTracerStateStringException; import io.jaegertracing.internal.exceptions.MalformedTracerStateStringException; -import java.util.Collections; import org.junit.Test; public class TextMapCodecTest { @@ -70,18 +68,15 @@ public void testContextFromString() throws Exception { } @Test - public void testToStringFormatsPositiveFields() { + public void testContextAsStringFormatsPositiveFields() { long traceId = -10L; long spanId = -10L; long parentId = -10L; byte flags = (byte) 129; JaegerSpanContext context = new JaegerSpanContext(traceId, spanId, parentId, flags); - - context.toString().split(":"); - assertEquals( - "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.toString()); + "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", TextMapCodec.contextAsString(context)); JaegerSpanContext contextFromStr = TextMapCodec.contextFromString(context.toString()); assertEquals(traceId, contextFromStr.getTraceId()); assertEquals(spanId, contextFromStr.getSpanId()); From 48112ed842703822e6c579306cdfe52c7e9adf83 Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Mon, 20 Aug 2018 16:32:28 -0400 Subject: [PATCH 7/8] Specify expected string output with string formatting Signed-off-by: Isaac Hier --- .../java/io/jaegertracing/internal/JaegerSpanTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java index 78c560fd2..75b392a60 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -211,7 +211,12 @@ public void testWithoutTimestampsInaccurateClock() { public void testSpanToString() { String operation = "test-operation"; JaegerSpan span = tracer.buildSpan(operation).start(); - String expectedString = span.context().toString() + " - " + operation; + String expectedString = String.format("%x:%x:%x:%x - %s", + span.context().getTraceId(), + span.context().getSpanId(), + span.context().getParentId(), + span.context().getFlags(), + operation); assertEquals(expectedString, span.toString()); span.finish(); } From 900524bec45860f376a83311a4a892e597036dce Mon Sep 17 00:00:00 2001 From: Isaac Hier Date: Mon, 20 Aug 2018 16:43:36 -0400 Subject: [PATCH 8/8] Use fixed IDs to avoid string formatting Signed-off-by: Isaac Hier --- .../internal/JaegerSpanTest.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java index 75b392a60..54739601e 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -210,14 +210,17 @@ public void testWithoutTimestampsInaccurateClock() { @Test public void testSpanToString() { String operation = "test-operation"; - JaegerSpan span = tracer.buildSpan(operation).start(); - String expectedString = String.format("%x:%x:%x:%x - %s", - span.context().getTraceId(), - span.context().getSpanId(), - span.context().getParentId(), - span.context().getFlags(), - operation); - assertEquals(expectedString, span.toString()); + JaegerSpan span = new JaegerSpan( + tracer, + operation, + new JaegerSpanContext( + 1, 2, 3, (byte) 4, Collections.emptyMap(), null /* debugId */), + 0, + 0, + false, + Collections.emptyMap(), + Collections.emptyList()); + assertEquals("1:2:3:4 - test-operation", span.toString()); span.finish(); }