Skip to content

Commit ae8ce37

Browse files
Merge pull request #59 from ExpediaDotCom/parentid-bug-fix
set parentSpanId as null in place of initializing it to UUID.zero
2 parents 37d51ae + 2107d0f commit ae8ce37

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed

core/src/main/java/com/expedia/www/haystack/client/Tracer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ public Scope startActive(boolean finishSpanOnClose) {
267267

268268
protected SpanContext createNewContext() {
269269
UUID randomId = UUID.randomUUID();
270-
UUID zero = new UUID(0l, 0l);
271-
return createContext(randomId, randomId, zero, Collections.<String, String>emptyMap());
270+
return createContext(randomId, randomId, null, Collections.emptyMap());
272271
}
273272

274273
protected SpanContext createContext(UUID traceId, UUID spanId, UUID parentId, Map<String, String> baggage) {

core/src/main/java/com/expedia/www/haystack/client/dispatchers/formats/ProtoBufFormat.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@
1616
*/
1717
package com.expedia.www.haystack.client.dispatchers.formats;
1818

19-
import java.io.ByteArrayOutputStream;
20-
import java.io.IOException;
21-
import java.io.ObjectOutputStream;
22-
import java.util.stream.Collectors;
23-
24-
import org.slf4j.Logger;
25-
import org.slf4j.LoggerFactory;
26-
2719
import com.expedia.open.tracing.Log;
2820
import com.expedia.open.tracing.Tag;
2921
import com.expedia.open.tracing.Tag.TagType;
3022
import com.expedia.www.haystack.client.LogData;
3123
import com.expedia.www.haystack.client.Span;
3224
import com.expedia.www.haystack.client.SpanContext;
3325
import com.google.protobuf.ByteString;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.IOException;
31+
import java.io.ObjectOutputStream;
32+
import java.util.stream.Collectors;
3433

3534
public class ProtoBufFormat implements Format<com.expedia.open.tracing.Span> {
3635
private static final Logger LOGGER = LoggerFactory.getLogger(ProtoBufFormat.class);
@@ -41,36 +40,35 @@ public com.expedia.open.tracing.Span format(Span span) {
4140

4241
SpanContext context = span.context();
4342
builder.setTraceId(context.getTraceId().toString())
44-
.setSpanId(context.getSpanId().toString())
45-
.setParentSpanId(context.getParentId().toString());
43+
.setSpanId(context.getSpanId().toString());
4644

47-
builder.setServiceName(span.getServiceName())
48-
.setOperationName(span.getOperatioName());
45+
if (context.getParentId() != null) {
46+
builder.setParentSpanId(context.getParentId().toString());
47+
}
4948

50-
builder.setStartTime(span.getStartTime());
49+
builder.setServiceName(span.getServiceName())
50+
.setOperationName(span.getOperatioName())
51+
.setStartTime(span.getStartTime());
5152

5253
if (span.getDuration() != null) {
5354
builder.setDuration(span.getDuration());
5455
}
5556

5657
builder.addAllLogs(span.getLogs().stream()
57-
.map(l -> buildLog(l))
58-
.collect(Collectors.toList()));
59-
60-
builder.addAllTags(span.getTags().entrySet().stream()
58+
.map(this::buildLog)
59+
.collect(Collectors.toList()))
60+
.addAllTags(span.getTags().entrySet().stream()
6161
.map(e -> buildTag(e.getKey(), e.getValue()))
62-
.collect(Collectors.toList()));
63-
64-
// add the baggage items as tags for now
65-
builder.addAllTags(context.getBaggage().entrySet().stream()
62+
.collect(Collectors.toList()))
63+
.addAllTags(context.getBaggage().entrySet().stream() // add the baggage items as tags for now
6664
.map(e -> buildTag(e.getKey(), e.getValue()))
6765
.collect(Collectors.toList()));
6866

6967
return builder.build();
7068
}
7169

72-
protected Log buildLog(LogData log) {
73-
Log.Builder builder = Log.newBuilder()
70+
protected Log buildLog(final LogData log) {
71+
final Log.Builder builder = Log.newBuilder()
7472
.setTimestamp(log.getTimestamp());
7573

7674
if (log.getFields() != null ) {
@@ -87,7 +85,7 @@ protected Tag buildTag(String key, Object value) {
8785
if (value == null) {
8886
// just a message collected; adding an empty payload
8987
builder.setType(TagType.STRING);
90-
builder.setVStr(new String());
88+
builder.setVStr("");
9189
} else if (value instanceof String) {
9290
builder.setType(TagType.STRING);
9391
builder.setVStr((String) value);
@@ -103,8 +101,8 @@ protected Tag buildTag(String key, Object value) {
103101
} else {
104102
builder.setType(TagType.BINARY);
105103

106-
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
107-
try (ObjectOutputStream os = new ObjectOutputStream(out)) {
104+
try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) {
105+
try (final ObjectOutputStream os = new ObjectOutputStream(out)) {
108106
os.writeObject(value);
109107
builder.setVBytes(ByteString.copyFrom(out.toByteArray()));
110108
}

core/src/main/java/com/expedia/www/haystack/client/propagation/TextMapCodex.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public class TextMapCodex implements Codex<String,Object> {
2323

2424
@Override
2525
public String encode(Object value) {
26-
return value.toString();
26+
return value == null ? null : value.toString();
2727
}
2828

2929
@Override
3030
public String decode(Object value) {
31-
return value.toString();
31+
return value == null ? null : value.toString();
3232
}
3333
}

core/src/main/java/com/expedia/www/haystack/client/propagation/TextMapPropagator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public SpanContext extract(TextMap carrier) {
106106

107107
SpanContext context = new SpanContext(UUID.fromString(traceId),
108108
UUID.fromString(spanId),
109-
UUID.fromString(parentId));
109+
parentId == null ? null : UUID.fromString(parentId));
110110
return context.addBaggage(baggage);
111111
}
112112

core/src/test/java/com/expedia/www/haystack/client/SpanPropagationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testActiveSpanIgnorePropagation() {
9090
Assert.assertEquals(0, secondSpan.getReferences().size());
9191
Assert.assertEquals(0, firstSpan.getReferences().size());
9292
Assert.assertNotEquals(firstSpan.context().getTraceId(), secondSpan.context().getTraceId());
93-
Assert.assertEquals(new UUID(0l, 0l), secondSpan.context().getParentId());
93+
Assert.assertNull(secondSpan.context().getParentId());
9494
}
9595

9696
@Test
@@ -114,9 +114,9 @@ public void testActiveSpanPreSeeded() {
114114
Assert.assertEquals(parent.context().getSpanId(), child.context().getParentId());
115115

116116
Assert.assertTrue(parent.getReferences().isEmpty());
117-
Assert.assertEquals(new UUID(0l, 0l), parent.context().getParentId());
117+
Assert.assertNull(parent.context().getParentId());
118118
Assert.assertTrue(active.getReferences().isEmpty());
119-
Assert.assertEquals(new UUID(0l, 0l), active.context().getParentId());
119+
Assert.assertNull(active.context().getParentId());
120120
}
121121

122122
@Test

core/src/test/java/com/expedia/www/haystack/client/dispatchers/formats/ProtBufFormatTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,19 @@
1616
*/
1717
package com.expedia.www.haystack.client.dispatchers.formats;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotNull;
21-
22-
import org.junit.Assert;
23-
import org.junit.Before;
24-
import org.junit.Test;
25-
2619
import com.expedia.open.tracing.Log;
2720
import com.expedia.open.tracing.Tag;
2821
import com.expedia.www.haystack.client.Span;
2922
import com.expedia.www.haystack.client.Tracer;
3023
import com.expedia.www.haystack.client.dispatchers.InMemoryDispatcher;
3124
import com.expedia.www.haystack.client.metrics.NoopMetricsRegistry;
3225
import com.google.common.collect.ImmutableMap;
26+
import org.junit.Assert;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertNotNull;
3332

3433
public class ProtBufFormatTest {
3534
private Tracer tracer;
@@ -156,5 +155,4 @@ public void testSpanConversion() {
156155
// Tags + Baggage for now.
157156
assertEquals(2, protoSpan.getTagsCount());
158157
}
159-
160158
}

0 commit comments

Comments
 (0)