-
Notifications
You must be signed in to change notification settings - Fork 719
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows extraction of just a trace ID (not span ID) from an incoming c…
…arrier
- Loading branch information
Adrian Cole
committed
Oct 6, 2017
1 parent
0be5039
commit 981257d
Showing
14 changed files
with
474 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package brave.propagation; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
import static brave.internal.HexCodec.writeHexLong; | ||
|
||
/** | ||
* Contains inbound trace ID and sampling flags, used when users control the root trace ID, but not | ||
* the span ID (ex Amazon X-Ray or other correlation). | ||
*/ | ||
@Immutable | ||
@AutoValue | ||
public abstract class TraceIdContext extends SamplingFlags { | ||
|
||
public static Builder newBuilder() { | ||
return new AutoValue_TraceIdContext.Builder().traceIdHigh(0L).debug(false); | ||
} | ||
|
||
/** When non-zero, the trace containing this span uses 128-bit trace identifiers. */ | ||
public abstract long traceIdHigh(); | ||
|
||
/** Unique 8-byte identifier for a trace, set on all spans within it. */ | ||
public abstract long traceId(); | ||
|
||
// override as auto-value can't currently read the super-class's nullable annotation. | ||
@Override @Nullable public abstract Boolean sampled(); | ||
|
||
public abstract Builder toBuilder(); | ||
|
||
/** Returns {@code $traceId} */ | ||
@Override | ||
public String toString() { | ||
boolean traceHi = traceIdHigh() != 0; | ||
char[] result = new char[traceHi ? 32 : 16]; | ||
int pos = 0; | ||
if (traceHi) { | ||
writeHexLong(result, pos, traceIdHigh()); | ||
pos += 16; | ||
} | ||
writeHexLong(result, pos, traceId()); | ||
return new String(result); | ||
} | ||
|
||
@AutoValue.Builder | ||
public static abstract class Builder { | ||
/** @see TraceIdContext#traceIdHigh() */ | ||
public abstract Builder traceIdHigh(long traceIdHigh); | ||
|
||
/** @see TraceIdContext#traceId() */ | ||
public abstract Builder traceId(long traceId); | ||
|
||
/** @see TraceIdContext#sampled */ | ||
public abstract Builder sampled(@Nullable Boolean nullableSampled); | ||
|
||
/** @see TraceIdContext#debug() */ | ||
public abstract Builder debug(boolean debug); | ||
|
||
public abstract TraceIdContext build(); | ||
|
||
Builder() { // no external implementations | ||
} | ||
} | ||
|
||
TraceIdContext() { // no external implementations | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
brave/src/test/java/brave/propagation/TraceIdContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package brave.propagation; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class TraceIdContextTest { | ||
TraceIdContext context = TraceIdContext.newBuilder().traceId(333L).build(); | ||
|
||
@Test public void compareUnequalIds() { | ||
assertThat(context) | ||
.isNotEqualTo(context.toBuilder().traceIdHigh(222L).build()); | ||
} | ||
|
||
@Test public void compareEqualIds() { | ||
assertThat(context) | ||
.isEqualTo(TraceIdContext.newBuilder().traceId(333L).build()); | ||
} | ||
|
||
@Test public void testToString_lo() { | ||
assertThat(context.toString()) | ||
.isEqualTo("000000000000014d"); | ||
} | ||
|
||
@Test public void testToString() { | ||
assertThat(context.toBuilder().traceIdHigh(222L).build().toString()) | ||
.isEqualTo("00000000000000de000000000000014d"); | ||
} | ||
} |
Oops, something went wrong.