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

Commit 640fa35

Browse files
authored
Do not strip leading zeros from trace IDs (#746)
* Do not strip leading zeros from trace IDs Signed-off-by: Yuri Shkuro <[email protected]> * Reuse util function Signed-off-by: Yuri Shkuro <[email protected]> * fix Signed-off-by: Yuri Shkuro <[email protected]>
1 parent 3296585 commit 640fa35

File tree

7 files changed

+35
-21
lines changed

7 files changed

+35
-21
lines changed

jaeger-core/src/main/java/io/jaegertracing/internal/JaegerSpanContext.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package io.jaegertracing.internal;
1717

1818
import io.jaegertracing.internal.propagation.TextMapCodec;
19+
import io.jaegertracing.internal.utils.Utils;
1920
import io.opentracing.SpanContext;
2021

2122
import java.util.Collections;
@@ -71,7 +72,7 @@ protected JaegerSpanContext(
7172
this.debugId = debugId;
7273
this.objectFactory = objectFactory;
7374
this.traceIdAsString = convertTraceId();
74-
this.spanIdAsString = Long.toHexString(spanId);
75+
this.spanIdAsString = Utils.to16HexString(spanId);
7576
}
7677

7778
@Override
@@ -89,16 +90,10 @@ Map<String, String> baggage() {
8990

9091
private String convertTraceId() {
9192
if (traceIdHigh == 0L) {
92-
return Long.toHexString(traceIdLow);
93-
}
94-
final String hexStringHigh = Long.toHexString(traceIdHigh);
95-
final String hexStringLow = Long.toHexString(traceIdLow);
96-
if (hexStringLow.length() < 16) {
97-
// left pad low trace id with '0'.
98-
// In theory, only 12.5% of all possible long values will be padded.
99-
// In practice, using Random.nextLong(), only 6% will need padding
100-
return hexStringHigh + "0000000000000000".substring(hexStringLow.length()) + hexStringLow;
93+
return Utils.to16HexString(traceIdLow);
10194
}
95+
final String hexStringHigh = Utils.to16HexString(traceIdHigh);
96+
final String hexStringLow = Utils.to16HexString(traceIdLow);
10297
return hexStringHigh + hexStringLow;
10398
}
10499

jaeger-core/src/main/java/io/jaegertracing/internal/propagation/TextMapCodec.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.jaegertracing.internal.exceptions.EmptyTracerStateStringException;
2222
import io.jaegertracing.internal.exceptions.MalformedTracerStateStringException;
2323
import io.jaegertracing.internal.exceptions.TraceIdOutOfBoundException;
24+
import io.jaegertracing.internal.utils.Utils;
2425
import io.jaegertracing.spi.Codec;
2526
import io.opentracing.propagation.TextMap;
2627

@@ -118,8 +119,9 @@ public static String contextAsString(JaegerSpanContext context) {
118119
int intFlag = context.getFlags() & 0xFF;
119120
return new StringBuilder()
120121
.append(context.getTraceId()).append(":")
121-
.append(Long.toHexString(context.getSpanId())).append(":")
122-
.append(Long.toHexString(context.getParentId())).append(":")
122+
.append(Utils.to16HexString(context.getSpanId())).append(":")
123+
// parent=0 is special, no need to encode as full 16 characters, and more readable this way
124+
.append(context.getParentId() == 0 ? "0" : Utils.to16HexString(context.getParentId())).append(":")
123125
.append(Integer.toHexString(intFlag))
124126
.toString();
125127
}

jaeger-core/src/main/java/io/jaegertracing/internal/utils/Utils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,13 @@ public static boolean equals(Object a, Object b) {
6060
return (a == b) || (a != null && a.equals(b));
6161
}
6262

63+
public static String to16HexString(long id) {
64+
final String hex = Long.toHexString(id);
65+
if (hex.length() == 16) {
66+
return hex;
67+
}
68+
return "0000000000000000".substring(hex.length()) + hex;
69+
}
70+
6371
private Utils() {}
6472
}

jaeger-core/src/test/java/io/jaegertracing/internal/JaegerSpanTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.jaegertracing.internal.metrics.Metrics;
3232
import io.jaegertracing.internal.reporters.InMemoryReporter;
3333
import io.jaegertracing.internal.samplers.ConstSampler;
34+
import io.jaegertracing.internal.utils.Utils;
3435
import io.jaegertracing.spi.BaggageRestrictionManager;
3536
import io.opentracing.References;
3637
import io.opentracing.Span;
@@ -190,7 +191,7 @@ public void testToTraceId() {
190191

191192
@Test
192193
public void testToSpanId() {
193-
assertEquals(Long.toHexString(jaegerSpan.context().getSpanId()), jaegerSpan.context().toSpanId());
194+
assertEquals(Utils.to16HexString(jaegerSpan.context().getSpanId()), jaegerSpan.context().toSpanId());
194195
}
195196

196197
@Test
@@ -295,7 +296,9 @@ public void testSpanToString() {
295296
false,
296297
Collections.emptyMap(),
297298
Collections.emptyList());
298-
assertEquals("1:2:3:4 - test-operation", span.toString());
299+
assertEquals(
300+
"0000000000000001:0000000000000002:0000000000000003:4 - test-operation",
301+
span.toString());
299302
span.finish();
300303
}
301304

@@ -317,7 +320,9 @@ public void testSpanToStringWith128BitTraceId() {
317320
false,
318321
Collections.emptyMap(),
319322
Collections.emptyList());
320-
assertEquals("20000000000000001:3:4:4 - test-operation", span.toString());
323+
assertEquals(
324+
"00000000000000020000000000000001:0000000000000003:0000000000000004:4 - test-operation",
325+
span.toString());
321326
span.finish();
322327
}
323328

jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TextMapCodecTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public void testContextAsStringWith128BitTraceId() {
104104

105105
JaegerSpanContext context = new JaegerSpanContext(traceIdHigh, traceIdLow, spanId, parentId, flags);
106106
assertEquals(
107-
"20000000000000001:3:4:5", TextMapCodec.contextAsString(context));
107+
"00000000000000020000000000000001:0000000000000003:0000000000000004:5",
108+
TextMapCodec.contextAsString(context));
108109
}
109110

110111
@Test
@@ -169,7 +170,7 @@ public void testInjectDoNotEncodeSpanContext() {
169170
codec.inject(new JaegerSpanContext(0L, traceIdLow, spanId, parentId, (byte)1), new TextMapAdapter(headers));
170171

171172
String traceId = headers.get("uber-trace-id");
172-
assertEquals("2a:1:0:1", traceId);
173+
assertEquals("000000000000002a:0000000000000001:0:1", traceId);
173174
}
174175

175176
@Test

jaeger-core/src/test/java/io/jaegertracing/internal/propagation/TraceContextCodecTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void testInjectWith64bit() {
113113
String traceParent = carrier.get(TRACE_PARENT);
114114
assertEquals(EXAMPLE_TRACE_PARENT, traceParent);
115115
JaegerSpanContext extractedContext = traceContextCodec.extract(textMap);
116-
assertEquals("1:2:0:0", extractedContext.toString());
116+
assertEquals("0000000000000001:0000000000000002:0:0", extractedContext.toString());
117117
}
118118

119119
@Test
@@ -123,7 +123,7 @@ public void testExtractWithCapitalizedTraceHeaders() {
123123
textMap.put("Traceparent", EXAMPLE_TRACE_PARENT);
124124
textMap.put("Tracestate", "whatever");
125125
JaegerSpanContext spanContext = traceContextCodec.extract(textMap);
126-
assertEquals("1:2:0:0", spanContext.toString());
126+
assertEquals("0000000000000001:0000000000000002:0:0", spanContext.toString());
127127
assertEquals("whatever", spanContext.getTraceState());
128128
}
129129

@@ -201,7 +201,7 @@ public void testDebugIdWithTraceHeader() {
201201
textMap.put(Constants.DEBUG_ID_HEADER_KEY, EXAMPLE_DEBUG_ID);
202202
JaegerSpanContext spanContext = traceContextCodec.extract(textMap);
203203
JaegerTracer tracer = new JaegerTracer.Builder("service").withReporter(new InMemoryReporter()).build();
204-
assertEquals("1", spanContext.getTraceId());
204+
assertEquals("0000000000000001", spanContext.getTraceId());
205205
JaegerSpan child = tracer.buildSpan("span").asChildOf(spanContext).start();
206206
assertFalse(child.context().isDebug());
207207
child.finish();

jaeger-zipkin/src/test/java/io/jaegertracing/zipkin/internal/V2SpanConverterTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ public void testSpanLogsCreateAnnotations() {
303303

304304
@Test
305305
public void testConvertSpanWith128BitTraceId() {
306-
JaegerSpan span = tracer128.buildSpan("operation-name").start();
306+
// zipkinSpan.traceId() always returns full length ID (padded with 0s).
307+
// To make the test stable, use a short idHigh portion.
308+
JaegerSpanContext c = new JaegerSpanContext(1L, 1L, 1L, 0L, (byte)0x01);
309+
JaegerSpan span = tracer128.buildSpan("operation-name").asChildOf(c).start();
307310

308311
zipkin2.Span zipkinSpan = V2SpanConverter.convertSpan(span);
309312
assertNotEquals(0, span.context().getTraceIdHigh());

0 commit comments

Comments
 (0)