Skip to content

Commit

Permalink
Adds preconditions to extract to another PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Cole committed Mar 16, 2020
1 parent 8844195 commit e55da25
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
4 changes: 2 additions & 2 deletions brave/src/main/java/brave/propagation/B3SingleFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ public static TraceContextOrSamplingFlags parseB3SingleFormat(CharSequence value
}

// Since we are using a hidden constructor, we need to validate here.
if (traceId == 0L || spanId == 0L) {
int field = traceId == 0L ? FIELD_TRACE_ID : FIELD_SPAN_ID;
if ((traceIdHigh == 0L && traceId == 0L) || spanId == 0L) {
int field = spanId == 0L ? FIELD_SPAN_ID : FIELD_TRACE_ID;
log(field, "Invalid input: read all zeros {0}");
return null;
}
Expand Down
18 changes: 15 additions & 3 deletions brave/src/main/java/brave/propagation/TraceContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 The OpenZipkin Authors
* Copyright 2013-2020 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -278,6 +278,17 @@ public static final class Builder {
extra = context.extra;
}

public final Builder clear() {
traceIdHigh = 0L;
traceId = 0L;
localRootId = 0L;
parentId = 0L;
spanId = 0L;
flags = 0;
extra = Collections.emptyList();
return this;
}

/** @see TraceContext#traceIdHigh() */
public Builder traceIdHigh(long traceIdHigh) {
this.traceIdHigh = traceIdHigh;
Expand Down Expand Up @@ -455,11 +466,12 @@ void maybeLogNotLowerHex(String notLowerHex) {
Platform.get().log("{0} is not a lower-hex string", notLowerHex, null);
}

/** @throws IllegalArgumentException if missing trace ID or span ID */
public final TraceContext build() {
String missing = "";
if (traceId == 0L) missing += " traceId";
if (traceIdHigh == 0L && traceId == 0L) missing += " traceId";
if (spanId == 0L) missing += " spanId";
if (!"".equals(missing)) throw new IllegalStateException("Missing: " + missing);
if (!"".equals(missing)) throw new IllegalArgumentException("Missing: " + missing);
return new TraceContext(
flags, traceIdHigh, traceId, localRootId, parentId, spanId, ensureImmutable(extra)
);
Expand Down
39 changes: 24 additions & 15 deletions brave/src/test/java/brave/propagation/B3SingleFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,35 @@ public class B3SingleFormatTest {

@Test public void parseB3SingleFormat_largest() {
assertThat(
parseB3SingleFormat(traceIdHigh + traceId + "-" + spanId + "-1-" + parentId)
).extracting(TraceContextOrSamplingFlags::context).isEqualToComparingFieldByField(
TraceContext.newBuilder()
.traceIdHigh(Long.parseUnsignedLong(traceIdHigh, 16))
.traceId(Long.parseUnsignedLong(traceId, 16))
.parentId(Long.parseUnsignedLong(parentId, 16))
.spanId(Long.parseUnsignedLong(spanId, 16))
.sampled(true).build()
parseB3SingleFormat(traceIdHigh + traceId + "-" + spanId + "-1-" + parentId).context()
).isEqualToComparingFieldByField(TraceContext.newBuilder()
.traceIdHigh(Long.parseUnsignedLong(traceIdHigh, 16))
.traceId(Long.parseUnsignedLong(traceId, 16))
.parentId(Long.parseUnsignedLong(parentId, 16))
.spanId(Long.parseUnsignedLong(spanId, 16))
.sampled(true).build()
);
}

@Test public void parseB3SingleFormat_padded() {
assertThat(
parseB3SingleFormat("0000000000000000" + traceId + "-" + spanId + "-1-" + parentId)
).extracting(TraceContextOrSamplingFlags::context).isEqualToComparingFieldByField(
TraceContext.newBuilder()
.traceId(Long.parseUnsignedLong(traceId, 16))
.parentId(Long.parseUnsignedLong(parentId, 16))
.spanId(Long.parseUnsignedLong(spanId, 16))
.sampled(true).build()
parseB3SingleFormat("0000000000000000" + traceId + "-" + spanId + "-1-" + parentId).context()
).isEqualToComparingFieldByField(TraceContext.newBuilder()
.traceId(Long.parseUnsignedLong(traceId, 16))
.parentId(Long.parseUnsignedLong(parentId, 16))
.spanId(Long.parseUnsignedLong(spanId, 16))
.sampled(true).build()
);
}

@Test public void parseTraceparentFormat_padded_right() {
assertThat(
parseB3SingleFormat(traceIdHigh + "0000000000000000-" + spanId + "-1-" + parentId).context()
).isEqualToComparingFieldByField(TraceContext.newBuilder()
.traceIdHigh(Long.parseUnsignedLong(traceIdHigh, 16))
.parentId(Long.parseUnsignedLong(parentId, 16))
.spanId(Long.parseUnsignedLong(spanId, 16))
.sampled(true).build()
);
}

Expand Down

0 comments on commit e55da25

Please sign in to comment.