From b6a824d0f6e54f246a1c5583e45536a1143c4c30 Mon Sep 17 00:00:00 2001 From: isaachier Date: Fri, 17 Aug 2018 13:16:24 -0400 Subject: [PATCH] Move contextFromString out of JaegerSpanContext into TextMapCodec (#517) Signed-off-by: Isaac Hier --- .../internal/JaegerSpanContext.java | 22 ----------------- .../internal/propagation/TextMapCodec.java | 24 ++++++++++++++++++- .../internal/JaegerSpanContextTest.java | 10 ++++---- .../internal/JaegerSpanTest.java | 3 ++- 4 files changed, 30 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 c6470435e..413e0bca2 100644 --- a/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java +++ b/jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java @@ -107,28 +107,6 @@ public String toString() { return contextAsString(); } - public static JaegerSpanContext contextFromString(String value) - throws MalformedTracerStateStringException, EmptyTracerStateStringException { - if (value == null || value.equals("")) { - throw new EmptyTracerStateStringException(); - } - - String[] parts = value.split(":"); - if (parts.length != 4) { - throw new MalformedTracerStateStringException(value); - } - - /* - oibe: because java doesn't like to convert large hex strings to longs - we should write this manually instead of using BigInteger. - */ - return new JaegerSpanContext( - new BigInteger(parts[0], 16).longValue(), - new BigInteger(parts[1], 16).longValue(), - new BigInteger(parts[2], 16).longValue(), - new BigInteger(parts[3], 16).byteValue()); - } - public JaegerSpanContext withBaggageItem(String key, String val) { Map newBaggage = new HashMap(this.baggage); if (val == null) { 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 739743772..7cc3a9352 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 @@ -16,9 +16,12 @@ import io.jaegertracing.internal.Constants; import io.jaegertracing.internal.JaegerSpanContext; +import io.jaegertracing.internal.exceptions.EmptyTracerStateStringException; +import io.jaegertracing.internal.exceptions.MalformedTracerStateStringException; import io.jaegertracing.spi.Codec; import io.opentracing.propagation.TextMap; import java.io.UnsupportedEncodingException; +import java.math.BigInteger; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.HashMap; @@ -54,6 +57,25 @@ private TextMapCodec(Builder builder) { this.baggagePrefix = builder.baggagePrefix; } + public static JaegerSpanContext contextFromString(String value) + throws MalformedTracerStateStringException, EmptyTracerStateStringException { + if (value == null || value.equals("")) { + throw new EmptyTracerStateStringException(); + } + + String[] parts = value.split(":"); + if (parts.length != 4) { + throw new MalformedTracerStateStringException(value); + } + + // TODO(isaachier): When we drop Java 1.6 support, use Long.parseUnsignedLong instead of using BigInteger. + return new JaegerSpanContext( + new BigInteger(parts[0], 16).longValue(), + new BigInteger(parts[1], 16).longValue(), + new BigInteger(parts[2], 16).longValue(), + new BigInteger(parts[3], 16).byteValue()); + } + @Override public void inject(JaegerSpanContext spanContext, TextMap carrier) { carrier.put(contextKey, encodedValue(spanContext.contextAsString())); @@ -71,7 +93,7 @@ public JaegerSpanContext extract(TextMap carrier) { // TODO there should be no lower-case here String key = entry.getKey().toLowerCase(Locale.ROOT); if (key.equals(contextKey)) { - context = JaegerSpanContext.contextFromString(decodedValue(entry.getValue())); + context = contextFromString(decodedValue(entry.getValue())); } else if (key.equals(Constants.DEBUG_ID_HEADER_KEY)) { debugId = decodedValue(entry.getValue()); } else if (key.startsWith(baggagePrefix)) { 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 8ebc3380e..895aa6bd7 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanContextTest.java @@ -16,26 +16,26 @@ import static org.junit.Assert.assertEquals; -import io.jaegertracing.internal.JaegerSpanContext; 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 { - JaegerSpanContext.contextFromString("ff:ff:ff"); + TextMapCodec.contextFromString("ff:ff:ff"); } @Test(expected = EmptyTracerStateStringException.class) public void testContextFromStringEmptyException() throws Exception { - JaegerSpanContext.contextFromString(""); + TextMapCodec.contextFromString(""); } @Test public void testContextFromString() throws Exception { - JaegerSpanContext context = JaegerSpanContext.contextFromString("ff:dd:cc:4"); + JaegerSpanContext context = TextMapCodec.contextFromString("ff:dd:cc:4"); assertEquals(context.getTraceId(), 255); assertEquals(context.getSpanId(), 221); assertEquals(context.getParentId(), 204); @@ -57,7 +57,7 @@ public void testToStringFormatsPositiveFields() { assertEquals( "fffffffffffffff6:fffffffffffffff6:fffffffffffffff6:81", context.contextAsString()); - JaegerSpanContext contextFromStr = JaegerSpanContext.contextFromString(context.contextAsString()); + JaegerSpanContext contextFromStr = TextMapCodec.contextFromString(context.contextAsString()); assertEquals(traceId, contextFromStr.getTraceId()); assertEquals(spanId, contextFromStr.getSpanId()); assertEquals(parentId, contextFromStr.getParentId()); 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 e672c6c04..8de0a3b0c 100644 --- a/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java +++ b/jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java @@ -27,6 +27,7 @@ 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; @@ -211,7 +212,7 @@ public void testWithoutTimestampsInaccurateClock() { public void testSpanToString() { JaegerSpan jaegerSpan = tracer.buildSpan("test-operation").start(); JaegerSpanContext expectedContext = jaegerSpan.context(); - JaegerSpanContext actualContext = JaegerSpanContext.contextFromString(expectedContext.contextAsString()); + JaegerSpanContext actualContext = TextMapCodec.contextFromString(expectedContext.contextAsString()); assertEquals(expectedContext.getTraceId(), actualContext.getTraceId()); assertEquals(expectedContext.getSpanId(), actualContext.getSpanId());