Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 9da4642

Browse files
rlindoorensongy23
authored andcommitted
Fixed bug that made it impossible to use short span id's (#950) (#954)
(cherry picked from commit 765318b)
1 parent 83952ee commit 9da4642

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

contrib/http_util/src/main/java/io/opencensus/contrib/http/util/CloudTraceFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public <C> SpanContext extract(C carrier, Getter<C> getter) throws SpanContextPa
107107
checkArgument(headerStr.charAt(TRACE_ID_SIZE) == SPAN_ID_DELIMITER, "Invalid TRACE_ID size");
108108

109109
TraceId traceId = TraceId.fromLowerBase16(headerStr.subSequence(0, TRACE_ID_SIZE));
110-
int traceOptionsPos = headerStr.indexOf(TRACE_OPTION_DELIMITER, SPAN_ID_DELIMITER);
110+
int traceOptionsPos = headerStr.indexOf(TRACE_OPTION_DELIMITER, TRACE_ID_SIZE);
111111
CharSequence spanIdStr =
112112
headerStr.subSequence(
113113
SPAN_ID_START_POS, traceOptionsPos < 0 ? headerStr.length() : traceOptionsPos);

contrib/http_util/src/test/java/io/opencensus/contrib/http/util/CloudTraceFormatTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.opencensus.trace.propagation.SpanContextParseException;
3232
import io.opencensus.trace.propagation.TextFormat.Getter;
3333
import io.opencensus.trace.propagation.TextFormat.Setter;
34+
import java.nio.ByteBuffer;
3435
import java.util.HashMap;
3536
import java.util.Map;
3637
import javax.annotation.Nullable;
@@ -280,4 +281,15 @@ public void parseMissingSpanIdShouldFail() throws SpanContextParseException {
280281
public void fieldsShouldMatch() {
281282
assertThat(cloudTraceFormat.fields()).containsExactly(HEADER_NAME);
282283
}
284+
285+
@Test
286+
public void parseWithShortSpanIdAndSamplingShouldSucceed() throws SpanContextParseException {
287+
final String spanId = "1";
288+
ByteBuffer buffer = ByteBuffer.allocate(SpanId.SIZE);
289+
buffer.putLong(Long.valueOf(spanId));
290+
SpanId expectedSpanId = SpanId.fromBytes(buffer.array());
291+
parseSuccess(
292+
constructHeader(TRACE_ID_BASE16, spanId, SAMPLED),
293+
SpanContext.create(TRACE_ID, expectedSpanId, TRACE_OPTIONS_SAMPLED));
294+
}
283295
}

0 commit comments

Comments
 (0)