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

Move contextFromString out of JaegerSpanContext into TextMapCodec #517

Merged
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 @@ -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
Original file line number Diff line number Diff line change
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's used only in tests, can't we make this private and test the inject operation instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the point of the internal package to imply anything here may change without warning? Public makes this easily accessible in a number of test modules without compromising our external API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been blocking the other PR, so I would rather not cause major refactoring of the tests. But going for minimum visibility is always a good goal. In this case, wouldn't all the tests work just as well if this was package level viz?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it and sadly no. propagation is its own package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. Ok then.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can probably fix the other tests. Don't merge yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops too late. Put another one if a simple fix.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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