aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorBogdan Drutu <bdrutu@google.com>2018-08-09 10:09:28 -0700
committerGitHub <noreply@github.com>2018-08-09 10:09:28 -0700
commit52f38e48e2ac6cb65e28dcd97b4f7e9650357bba (patch)
treeb5dc40e7e3805ece5011f1d253622e69bccc15f6 /api
parent18aa2793facbb3d7e252ac32e6bb4d09f08a37ac (diff)
downloadopencensus-java-52f38e48e2ac6cb65e28dcd97b4f7e9650357bba.tar.gz
Add Tracestate into SpanContext. (#1359)
* Add Tracestate into SpanContext. * Remove empty constructor from Tracestate.Builder * Add info in the changelog.
Diffstat (limited to 'api')
-rw-r--r--api/src/main/java/io/opencensus/trace/SpanContext.java42
-rw-r--r--api/src/main/java/io/opencensus/trace/Tracestate.java6
-rw-r--r--api/src/test/java/io/opencensus/trace/SpanContextTest.java20
3 files changed, 53 insertions, 15 deletions
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,9 +94,9 @@ 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() {
@@ -86,6 +104,16 @@ public final class SpanContext {
}
/**
+ * 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.
*
* @return 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.<Entry>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() {
@@ -87,6 +91,12 @@ public class SpanContextTest {
}
@Test
+ public void getTracestate() {
+ assertThat(first.getTracestate()).isEqualTo(firstTracestate);
+ assertThat(second.getTracestate()).isEqualTo(secondTracestate);
+ }
+
+ @Test
public void spanContext_EqualsAndHashCode() {
EqualsTester tester = new EqualsTester();
tester.addEqualityGroup(
@@ -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();
}