From 52f38e48e2ac6cb65e28dcd97b4f7e9650357bba Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Thu, 9 Aug 2018 10:09:28 -0700 Subject: Add Tracestate into SpanContext. (#1359) * Add Tracestate into SpanContext. * Remove empty constructor from Tracestate.Builder * Add info in the changelog. --- .../main/java/io/opencensus/trace/SpanContext.java | 42 ++++++++++++++++++---- .../main/java/io/opencensus/trace/Tracestate.java | 6 +--- .../java/io/opencensus/trace/SpanContextTest.java | 20 ++++++++--- 3 files changed, 53 insertions(+), 15 deletions(-) (limited to 'api') diff --git a/api/src/main/java/io/opencensus/trace/SpanContext.java b/api/src/main/java/io/opencensus/trace/SpanContext.java index 66a6bfbc..49ed751b 100644 --- a/api/src/main/java/io/opencensus/trace/SpanContext.java +++ b/api/src/main/java/io/opencensus/trace/SpanContext.java @@ -30,9 +30,11 @@ import javax.annotation.concurrent.Immutable; */ @Immutable public final class SpanContext { + private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build(); private final TraceId traceId; private final SpanId spanId; private final TraceOptions traceOptions; + private final Tracestate tracestate; /** * The invalid {@code SpanContext}. @@ -40,7 +42,7 @@ public final class SpanContext { * @since 0.5 */ public static final SpanContext INVALID = - new SpanContext(TraceId.INVALID, SpanId.INVALID, TraceOptions.DEFAULT); + new SpanContext(TraceId.INVALID, SpanId.INVALID, TraceOptions.DEFAULT, TRACESTATE_DEFAULT); /** * Creates a new {@code SpanContext} with the given identifiers and options. @@ -49,10 +51,26 @@ public final class SpanContext { * @param spanId the span identifier of the span context. * @param traceOptions the trace options for the span context. * @return a new {@code SpanContext} with the given identifiers and options. - * @since 0.5 + * @deprecated use {@link #create(TraceId, SpanId, TraceOptions, Tracestate)}. */ + @Deprecated public static SpanContext create(TraceId traceId, SpanId spanId, TraceOptions traceOptions) { - return new SpanContext(traceId, spanId, traceOptions); + return create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT); + } + + /** + * Creates a new {@code SpanContext} 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 traceOptions 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.16 + */ + public static SpanContext create( + TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate) { + return new SpanContext(traceId, spanId, traceOptions, tracestate); } /** @@ -76,15 +94,25 @@ public final class SpanContext { } /** - * Returns the trace options associated with this {@code SpanContext}. + * Returns the {@code TraceOptions} associated with this {@code SpanContext}. * - * @return the trace options associated with this {@code SpanContext}. + * @return the {@code TraceOptions} associated with this {@code SpanContext}. * @since 0.5 */ public TraceOptions getTraceOptions() { return traceOptions; } + /** + * Returns the {@code Tracestate} associated with this {@code SpanContext}. + * + * @return the {@code Tracestate} associated with this {@code SpanContext}. + * @since 0.5 + */ + public Tracestate getTracestate() { + return tracestate; + } + /** * Returns true if this {@code SpanContext} is valid. * @@ -127,9 +155,11 @@ public final class SpanContext { + "}"; } - private SpanContext(TraceId traceId, SpanId spanId, TraceOptions traceOptions) { + private SpanContext( + TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate) { this.traceId = traceId; this.spanId = spanId; this.traceOptions = traceOptions; + this.tracestate = tracestate; } } diff --git a/api/src/main/java/io/opencensus/trace/Tracestate.java b/api/src/main/java/io/opencensus/trace/Tracestate.java index 5535a0ac..f88d3bd3 100644 --- a/api/src/main/java/io/opencensus/trace/Tracestate.java +++ b/api/src/main/java/io/opencensus/trace/Tracestate.java @@ -81,7 +81,7 @@ public abstract class Tracestate { * @since 0.16 */ public static Builder builder() { - return new Builder(); + return new Builder(Builder.EMPTY); } /** @@ -108,10 +108,6 @@ public abstract class Tracestate { // subclass (the auto-value generate class). private static final Tracestate EMPTY = create(Collections.emptyList()); - private Builder() { - this(EMPTY); - } - private Builder(Tracestate parent) { Utils.checkNotNull(parent, "parent"); this.parent = parent; diff --git a/api/src/test/java/io/opencensus/trace/SpanContextTest.java b/api/src/test/java/io/opencensus/trace/SpanContextTest.java index f5a9954f..54e188c8 100644 --- a/api/src/test/java/io/opencensus/trace/SpanContextTest.java +++ b/api/src/test/java/io/opencensus/trace/SpanContextTest.java @@ -32,16 +32,20 @@ public class SpanContextTest { new byte[] {0, 0, 0, 0, 0, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 0}; private static final byte[] firstSpanIdBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'}; private static final byte[] secondSpanIdBytes = new byte[] {'0', 0, 0, 0, 0, 0, 0, 0}; + private static final Tracestate firstTracestate = Tracestate.builder().set("foo", "bar").build(); + private static final Tracestate secondTracestate = Tracestate.builder().set("foo", "baz").build(); private static final SpanContext first = SpanContext.create( TraceId.fromBytes(firstTraceIdBytes), SpanId.fromBytes(firstSpanIdBytes), - TraceOptions.DEFAULT); + TraceOptions.DEFAULT, + firstTracestate); private static final SpanContext second = SpanContext.create( TraceId.fromBytes(secondTraceIdBytes), SpanId.fromBytes(secondSpanIdBytes), - TraceOptions.builder().setIsSampled(true).build()); + TraceOptions.builder().setIsSampled(true).build(), + secondTracestate); @Test public void invalidSpanContext() { @@ -86,6 +90,12 @@ public class SpanContextTest { .isEqualTo(TraceOptions.builder().setIsSampled(true).build()); } + @Test + public void getTracestate() { + assertThat(first.getTracestate()).isEqualTo(firstTracestate); + assertThat(second.getTracestate()).isEqualTo(secondTracestate); + } + @Test public void spanContext_EqualsAndHashCode() { EqualsTester tester = new EqualsTester(); @@ -98,13 +108,15 @@ public class SpanContextTest { SpanContext.create( TraceId.fromBytes(firstTraceIdBytes), SpanId.fromBytes(firstSpanIdBytes), - TraceOptions.builder().setIsSampled(false).build())); + TraceOptions.builder().setIsSampled(false).build(), + firstTracestate)); tester.addEqualityGroup( second, SpanContext.create( TraceId.fromBytes(secondTraceIdBytes), SpanId.fromBytes(secondSpanIdBytes), - TraceOptions.builder().setIsSampled(true).build())); + TraceOptions.builder().setIsSampled(true).build(), + secondTracestate)); tester.testEquals(); } -- cgit v1.2.3