Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isRemote to the SpanContext #626

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions api/src/main/java/io/opentelemetry/trace/SpanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public final class SpanContext {
TraceId.getInvalid(),
SpanId.getInvalid(),
TraceFlags.getDefault(),
Tracestate.getDefault());
Tracestate.getDefault(),
/* isRemote= */ false);

private final TraceId traceId;
private final SpanId spanId;
private final TraceFlags traceFlags;
private final Tracestate tracestate;
private final boolean isRemote;

/**
* Returns the invalid {@code SpanContext} that can be used for no-op operations.
Expand All @@ -64,7 +66,23 @@ static SpanContext getInvalid() {
*/
public static SpanContext create(
TraceId traceId, SpanId spanId, TraceFlags traceFlags, Tracestate tracestate) {
return new SpanContext(traceId, spanId, traceFlags, tracestate);
return new SpanContext(traceId, spanId, traceFlags, tracestate, /* isRemote= */ false);
}

/**
* Creates a new {@code SpanContext} that was propagated from a remote parent, with the given
* identifiers and options.
*
* @param traceId the trace identifier of the span context.
* @param spanId the span identifier of the span context.
* @param traceFlags the trace options for the span context.
* @param tracestate the trace state for the span context.
* @return a new {@code SpanContext} with the given identifiers and options.
* @since 0.1.0
*/
public static SpanContext createFromRemoteParent(
TraceId traceId, SpanId spanId, TraceFlags traceFlags, Tracestate tracestate) {
return new SpanContext(traceId, spanId, traceFlags, tracestate, /* isRemote= */ true);
}

/**
Expand Down Expand Up @@ -108,15 +126,25 @@ public Tracestate getTracestate() {
}

/**
* Returns true if this {@code SpanContext} is valid.
* Returns {@code true} if this {@code SpanContext} is valid.
*
* @return true if this {@code SpanContext} is valid.
* @return {@code true} if this {@code SpanContext} is valid.
* @since 0.1.0
*/
public boolean isValid() {
return traceId.isValid() && spanId.isValid();
}

/**
* Returns {@code true} if the {@code SpanContext} was propagated from a remote parent.
*
* @return {@code true} if the {@code SpanContext} was propagated from a remote parent.
* @since 0.1.0
*/
public boolean isRemote() {
return isRemote;
}

@Override
public boolean equals(@Nullable Object obj) {
if (obj == this) {
Expand All @@ -130,7 +158,8 @@ public boolean equals(@Nullable Object obj) {
SpanContext that = (SpanContext) obj;
return traceId.equals(that.traceId)
&& spanId.equals(that.spanId)
&& traceFlags.equals(that.traceFlags);
&& traceFlags.equals(that.traceFlags)
&& isRemote == that.isRemote;
}

@Override
Expand All @@ -146,14 +175,21 @@ public String toString() {
+ spanId
+ ", traceFlags="
+ traceFlags
+ ", isRemote="
+ isRemote
bogdandrutu marked this conversation as resolved.
Show resolved Hide resolved
+ "}";
}

private SpanContext(
TraceId traceId, SpanId spanId, TraceFlags traceFlags, Tracestate tracestate) {
TraceId traceId,
SpanId spanId,
TraceFlags traceFlags,
Tracestate tracestate,
boolean isRemote) {
this.traceId = traceId;
this.spanId = spanId;
this.traceFlags = traceFlags;
this.tracestate = tracestate;
this.isRemote = isRemote;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,6 @@ public SpanContext fromByteArray(byte[] bytes) {
}
traceFlags = TraceFlags.fromByte(bytes[pos + ID_SIZE]);
}
return SpanContext.create(traceId, spanId, traceFlags, TRACESTATE_DEFAULT);
return SpanContext.createFromRemoteParent(traceId, spanId, traceFlags, TRACESTATE_DEFAULT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public <C> void inject(SpanContext spanContext, C carrier, Setter<C> setter) {
String tracestate = getter.get(carrier, TRACESTATE);
try {
if (tracestate == null || tracestate.isEmpty()) {
return SpanContext.create(traceId, spanId, traceFlags, TRACESTATE_DEFAULT);
return SpanContext.createFromRemoteParent(traceId, spanId, traceFlags, TRACESTATE_DEFAULT);
}
Tracestate.Builder tracestateBuilder = Tracestate.builder();
String[] listMembers = TRACESTATE_ENTRY_DELIMITER_SPLIT_PATTERN.split(tracestate);
Expand All @@ -149,7 +149,8 @@ public <C> void inject(SpanContext spanContext, C carrier, Setter<C> setter) {
tracestateBuilder.set(
listMember.substring(0, index), listMember.substring(index + 1, listMember.length()));
}
return SpanContext.create(traceId, spanId, traceFlags, tracestateBuilder.build());
return SpanContext.createFromRemoteParent(
traceId, spanId, traceFlags, tracestateBuilder.build());
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid tracestate: " + tracestate, e);
}
Expand Down
20 changes: 20 additions & 0 deletions api/src/test/java/io/opentelemetry/trace/SpanContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public class SpanContextTest {
SpanId.fromBytes(secondSpanIdBytes, 0),
TraceFlags.builder().setIsSampled(true).build(),
secondTracestate);
private static final SpanContext remote =
SpanContext.createFromRemoteParent(
TraceId.fromBytes(secondTraceIdBytes, 0),
SpanId.fromBytes(secondSpanIdBytes, 0),
TraceFlags.builder().setIsSampled(true).build(),
emptyTracestate);

@Test
public void invalidSpanContext() {
Expand Down Expand Up @@ -102,6 +108,13 @@ public void getTracestate() {
assertThat(second.getTracestate()).isEqualTo(secondTracestate);
}

@Test
public void isRemote() {
assertThat(first.isRemote()).isFalse();
assertThat(second.isRemote()).isFalse();
assertThat(remote.isRemote()).isTrue();
}

@Test
public void spanContext_EqualsAndHashCode() {
EqualsTester tester = new EqualsTester();
Expand All @@ -124,6 +137,13 @@ public void spanContext_EqualsAndHashCode() {
SpanId.fromBytes(secondSpanIdBytes, 0),
TraceFlags.builder().setIsSampled(true).build(),
secondTracestate));
tester.addEqualityGroup(
remote,
SpanContext.createFromRemoteParent(
TraceId.fromBytes(secondTraceIdBytes, 0),
SpanId.fromBytes(secondSpanIdBytes, 0),
TraceFlags.builder().setIsSampled(true).build(),
emptyTracestate));
tester.testEquals();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.opentelemetry.trace.propagation;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import io.opentelemetry.context.propagation.BinaryFormat;
import io.opentelemetry.trace.DefaultSpan;
Expand Down Expand Up @@ -48,8 +47,6 @@ public class BinaryTraceContextTest {
0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100,
101, 102, 103, 104, 2, 1
};
private static final SpanContext EXAMPLE_SPAN_CONTEXT =
SpanContext.create(TRACE_ID, SPAN_ID, TRACE_OPTIONS, Tracestate.getDefault());
private static final SpanContext INVALID_SPAN_CONTEXT = DefaultSpan.getInvalid().getContext();
@Rule public ExpectedException expectedException = ExpectedException.none();
private final BinaryFormat<SpanContext> binaryFormat = new BinaryTraceContext();
Expand All @@ -58,9 +55,9 @@ private void testSpanContextConversion(SpanContext spanContext) {
SpanContext propagatedBinarySpanContext =
binaryFormat.fromByteArray(binaryFormat.toByteArray(spanContext));

assertWithMessage("BinaryFormat propagated context is not equal with the initial context.")
.that(propagatedBinarySpanContext)
.isEqualTo(spanContext);
assertThat(propagatedBinarySpanContext.getTraceId()).isEqualTo(spanContext.getTraceId());
assertThat(propagatedBinarySpanContext.getSpanId()).isEqualTo(spanContext.getSpanId());
assertThat(propagatedBinarySpanContext.getTraceFlags()).isEqualTo(spanContext.getTraceFlags());
}

@Test
Expand Down Expand Up @@ -95,7 +92,10 @@ public void toBinaryValue_InvalidSpanContext() {

@Test
public void fromBinaryValue_BinaryExampleValue() {
assertThat(binaryFormat.fromByteArray(EXAMPLE_BYTES)).isEqualTo(EXAMPLE_SPAN_CONTEXT);
assertThat(binaryFormat.fromByteArray(EXAMPLE_BYTES))
.isEqualTo(
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, TRACE_OPTIONS, Tracestate.getDefault()));
}

@Test(expected = NullPointerException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public void extract_SampledContext() {
carrier.put(TRACEPARENT, TRACEPARENT_HEADER_SAMPLED);
assertThat(httpTraceContext.extract(carrier, getter))
.isEqualTo(
SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT));
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_DEFAULT));
}

@Test
Expand All @@ -137,7 +138,8 @@ public void extract_NotSampledContext() {
carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED);
assertThat(httpTraceContext.extract(carrier, getter))
.isEqualTo(
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT));
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT));
}

@Test
Expand All @@ -147,46 +149,51 @@ public void extract_SampledContext_WithTraceState() {
carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING);
assertThat(httpTraceContext.extract(carrier, getter))
.isEqualTo(
SpanContext.create(TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT));
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, SAMPLED_TRACE_OPTIONS, TRACESTATE_NOT_DEFAULT));
}

@Test
public void extract_NotSampledContext_WithTraceState() {
Map<String, String> carrier = new LinkedHashMap<String, String>();
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED);
carrier.put(TRACESTATE, TRACESTATE_NOT_DEFAULT_ENCODING);
assertThat(httpTraceContext.extract(carrier, getter))
.isEqualTo(
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT));
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT));
}

@Test
public void extract_NotSampledContext_NextVersion() {
Map<String, String> carrier = new LinkedHashMap<String, String>();
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(TRACEPARENT, "01-" + TRACE_ID_BASE16 + "-" + SPAN_ID_BASE16 + "-00-02");
assertThat(httpTraceContext.extract(carrier, getter))
.isEqualTo(
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT));
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT));
}

@Test
public void extract_NotSampledContext_EmptyTraceState() {
Map<String, String> carrier = new LinkedHashMap<String, String>();
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED);
carrier.put(TRACESTATE, "");
assertThat(httpTraceContext.extract(carrier, getter))
.isEqualTo(
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT));
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_DEFAULT));
}

@Test
public void extract_NotSampledContext_TraceStateWithSpaces() {
Map<String, String> carrier = new LinkedHashMap<String, String>();
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(TRACEPARENT, TRACEPARENT_HEADER_NOT_SAMPLED);
carrier.put(TRACESTATE, "foo=bar , bar=baz");
assertThat(httpTraceContext.extract(carrier, getter))
.isEqualTo(
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT));
SpanContext.createFromRemoteParent(
TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TRACESTATE_NOT_DEFAULT));
}

@Test
Expand Down