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

Commit

Permalink
Move contextFromString out of JaegerSpanContext into TextMapCodec (#517)
Browse files Browse the repository at this point in the history
Signed-off-by: Isaac Hier <ihier@uber.com>
  • Loading branch information
isaachier authored and yurishkuro committed Aug 17, 2018
1 parent 26869a9 commit b6a824d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
Expand Up @@ -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<String, String> newBaggage = new HashMap<String, String>(this.baggage);
if (val == null) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand All @@ -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)) {
Expand Down
Expand Up @@ -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);
Expand All @@ -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());
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit b6a824d

Please sign in to comment.