Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 52f38e4

Browse files
author
Bogdan Drutu
authored
Add Tracestate into SpanContext. (#1359)
* Add Tracestate into SpanContext. * Remove empty constructor from Tracestate.Builder * Add info in the changelog.
1 parent 18aa279 commit 52f38e4

File tree

10 files changed

+78
-23
lines changed

10 files changed

+78
-23
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
on recording exemplars.
88
- Reduce the default limit on `Link`s per `Span` to 32 (was 128 before).
99
- Add Spring support for `@Traced` annotation and java.sql.PreparedStatements
10-
tracing
10+
tracing.
1111
- Allow custom prefix for Stackdriver metrics in `StackdriverStatsConfiguration`.
12+
- Add support to handle the Tracestate in the SpanContext.
1213

1314
## 0.15.0 - 2018-06-20
1415
- Expose the factory methods of MonitoredResource.

api/src/main/java/io/opencensus/trace/SpanContext.java

+36-6
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@
3030
*/
3131
@Immutable
3232
public final class SpanContext {
33+
private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build();
3334
private final TraceId traceId;
3435
private final SpanId spanId;
3536
private final TraceOptions traceOptions;
37+
private final Tracestate tracestate;
3638

3739
/**
3840
* The invalid {@code SpanContext}.
3941
*
4042
* @since 0.5
4143
*/
4244
public static final SpanContext INVALID =
43-
new SpanContext(TraceId.INVALID, SpanId.INVALID, TraceOptions.DEFAULT);
45+
new SpanContext(TraceId.INVALID, SpanId.INVALID, TraceOptions.DEFAULT, TRACESTATE_DEFAULT);
4446

4547
/**
4648
* Creates a new {@code SpanContext} with the given identifiers and options.
@@ -49,10 +51,26 @@ public final class SpanContext {
4951
* @param spanId the span identifier of the span context.
5052
* @param traceOptions the trace options for the span context.
5153
* @return a new {@code SpanContext} with the given identifiers and options.
52-
* @since 0.5
54+
* @deprecated use {@link #create(TraceId, SpanId, TraceOptions, Tracestate)}.
5355
*/
56+
@Deprecated
5457
public static SpanContext create(TraceId traceId, SpanId spanId, TraceOptions traceOptions) {
55-
return new SpanContext(traceId, spanId, traceOptions);
58+
return create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
59+
}
60+
61+
/**
62+
* Creates a new {@code SpanContext} with the given identifiers and options.
63+
*
64+
* @param traceId the trace identifier of the span context.
65+
* @param spanId the span identifier of the span context.
66+
* @param traceOptions the trace options for the span context.
67+
* @param tracestate the trace state for the span context.
68+
* @return a new {@code SpanContext} with the given identifiers and options.
69+
* @since 0.16
70+
*/
71+
public static SpanContext create(
72+
TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate) {
73+
return new SpanContext(traceId, spanId, traceOptions, tracestate);
5674
}
5775

5876
/**
@@ -76,15 +94,25 @@ public SpanId getSpanId() {
7694
}
7795

7896
/**
79-
* Returns the trace options associated with this {@code SpanContext}.
97+
* Returns the {@code TraceOptions} associated with this {@code SpanContext}.
8098
*
81-
* @return the trace options associated with this {@code SpanContext}.
99+
* @return the {@code TraceOptions} associated with this {@code SpanContext}.
82100
* @since 0.5
83101
*/
84102
public TraceOptions getTraceOptions() {
85103
return traceOptions;
86104
}
87105

106+
/**
107+
* Returns the {@code Tracestate} associated with this {@code SpanContext}.
108+
*
109+
* @return the {@code Tracestate} associated with this {@code SpanContext}.
110+
* @since 0.5
111+
*/
112+
public Tracestate getTracestate() {
113+
return tracestate;
114+
}
115+
88116
/**
89117
* Returns true if this {@code SpanContext} is valid.
90118
*
@@ -127,9 +155,11 @@ public String toString() {
127155
+ "}";
128156
}
129157

130-
private SpanContext(TraceId traceId, SpanId spanId, TraceOptions traceOptions) {
158+
private SpanContext(
159+
TraceId traceId, SpanId spanId, TraceOptions traceOptions, Tracestate tracestate) {
131160
this.traceId = traceId;
132161
this.spanId = spanId;
133162
this.traceOptions = traceOptions;
163+
this.tracestate = tracestate;
134164
}
135165
}

api/src/main/java/io/opencensus/trace/Tracestate.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public String get(String key) {
8181
* @since 0.16
8282
*/
8383
public static Builder builder() {
84-
return new Builder();
84+
return new Builder(Builder.EMPTY);
8585
}
8686

8787
/**
@@ -108,10 +108,6 @@ public static final class Builder {
108108
// subclass (the auto-value generate class).
109109
private static final Tracestate EMPTY = create(Collections.<Entry>emptyList());
110110

111-
private Builder() {
112-
this(EMPTY);
113-
}
114-
115111
private Builder(Tracestate parent) {
116112
Utils.checkNotNull(parent, "parent");
117113
this.parent = parent;

api/src/test/java/io/opencensus/trace/SpanContextTest.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ public class SpanContextTest {
3232
new byte[] {0, 0, 0, 0, 0, 0, 0, '0', 0, 0, 0, 0, 0, 0, 0, 0};
3333
private static final byte[] firstSpanIdBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'};
3434
private static final byte[] secondSpanIdBytes = new byte[] {'0', 0, 0, 0, 0, 0, 0, 0};
35+
private static final Tracestate firstTracestate = Tracestate.builder().set("foo", "bar").build();
36+
private static final Tracestate secondTracestate = Tracestate.builder().set("foo", "baz").build();
3537
private static final SpanContext first =
3638
SpanContext.create(
3739
TraceId.fromBytes(firstTraceIdBytes),
3840
SpanId.fromBytes(firstSpanIdBytes),
39-
TraceOptions.DEFAULT);
41+
TraceOptions.DEFAULT,
42+
firstTracestate);
4043
private static final SpanContext second =
4144
SpanContext.create(
4245
TraceId.fromBytes(secondTraceIdBytes),
4346
SpanId.fromBytes(secondSpanIdBytes),
44-
TraceOptions.builder().setIsSampled(true).build());
47+
TraceOptions.builder().setIsSampled(true).build(),
48+
secondTracestate);
4549

4650
@Test
4751
public void invalidSpanContext() {
@@ -86,6 +90,12 @@ public void getTraceOptions() {
8690
.isEqualTo(TraceOptions.builder().setIsSampled(true).build());
8791
}
8892

93+
@Test
94+
public void getTracestate() {
95+
assertThat(first.getTracestate()).isEqualTo(firstTracestate);
96+
assertThat(second.getTracestate()).isEqualTo(secondTracestate);
97+
}
98+
8999
@Test
90100
public void spanContext_EqualsAndHashCode() {
91101
EqualsTester tester = new EqualsTester();
@@ -98,13 +108,15 @@ public void spanContext_EqualsAndHashCode() {
98108
SpanContext.create(
99109
TraceId.fromBytes(firstTraceIdBytes),
100110
SpanId.fromBytes(firstSpanIdBytes),
101-
TraceOptions.builder().setIsSampled(false).build()));
111+
TraceOptions.builder().setIsSampled(false).build(),
112+
firstTracestate));
102113
tester.addEqualityGroup(
103114
second,
104115
SpanContext.create(
105116
TraceId.fromBytes(secondTraceIdBytes),
106117
SpanId.fromBytes(secondSpanIdBytes),
107-
TraceOptions.builder().setIsSampled(true).build()));
118+
TraceOptions.builder().setIsSampled(true).build(),
119+
secondTracestate));
108120
tester.testEquals();
109121
}
110122

benchmarks/src/jmh/java/io/opencensus/benchmarks/trace/propagation/BinaryPropagationImplBenchmark.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.opencensus.trace.SpanId;
2121
import io.opencensus.trace.TraceId;
2222
import io.opencensus.trace.TraceOptions;
23+
import io.opencensus.trace.Tracestate;
2324
import io.opencensus.trace.Tracing;
2425
import io.opencensus.trace.propagation.BinaryFormat;
2526
import io.opencensus.trace.propagation.SpanContextParseException;
@@ -41,7 +42,8 @@ public class BinaryPropagationImplBenchmark {
4142
private static final SpanId spanId = SpanId.fromBytes(spanIdBytes);
4243
private static final byte[] traceOptionsBytes = new byte[] {1};
4344
private static final TraceOptions traceOptions = TraceOptions.fromBytes(traceOptionsBytes);
44-
private static final SpanContext spanContext = SpanContext.create(traceId, spanId, traceOptions);
45+
private static final SpanContext spanContext =
46+
SpanContext.create(traceId, spanId, traceOptions, Tracestate.builder().build());
4547
private static final BinaryFormat binaryFormat =
4648
Tracing.getPropagationComponent().getBinaryFormat();
4749
private static final byte[] spanContextBinary = binaryFormat.toByteArray(spanContext);

contrib/appengine_standard_util/src/main/java/io/opencensus/contrib/appengine/standard/util/AppEngineCloudTraceContextUtils.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.opencensus.trace.SpanId;
2525
import io.opencensus.trace.TraceId;
2626
import io.opencensus.trace.TraceOptions;
27+
import io.opencensus.trace.Tracestate;
2728
import java.nio.ByteBuffer;
2829

2930
/**
@@ -37,6 +38,7 @@ public final class AppEngineCloudTraceContextUtils {
3738
TraceIdProto.newBuilder().setHi(0).setLo(0).build().toByteArray();
3839
private static final long INVALID_SPAN_ID = 0L;
3940
private static final long INVALID_TRACE_MASK = 0L;
41+
private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build();
4042

4143
@VisibleForTesting
4244
static final CloudTraceContext INVALID_CLOUD_TRACE_CONTEXT =
@@ -64,7 +66,8 @@ public static SpanContext fromCloudTraceContext(CloudTraceContext cloudTraceCont
6466
return SpanContext.create(
6567
TraceId.fromBytes(traceIdBuf.array()),
6668
SpanId.fromBytes(spanIdBuf.array()),
67-
TraceOptions.builder().setIsSampled(cloudTraceContext.isTraceEnabled()).build());
69+
TraceOptions.builder().setIsSampled(cloudTraceContext.isTraceEnabled()).build(),
70+
TRACESTATE_DEFAULT);
6871
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
6972
throw new RuntimeException(e);
7073
}

contrib/http_util/src/main/java/io/opencensus/contrib/http/util/CloudTraceFormat.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.opencensus.trace.SpanId;
2626
import io.opencensus.trace.TraceId;
2727
import io.opencensus.trace.TraceOptions;
28+
import io.opencensus.trace.Tracestate;
2829
import io.opencensus.trace.propagation.SpanContextParseException;
2930
import io.opencensus.trace.propagation.TextFormat;
3031
import java.nio.ByteBuffer;
@@ -77,6 +78,7 @@ final class CloudTraceFormat extends TextFormat {
7778
// 32-digit TRACE_ID + 1 digit SPAN_ID_DELIMITER + at least 1 digit SPAN_ID
7879
static final int MIN_HEADER_SIZE = SPAN_ID_START_POS + 1;
7980
static final int CLOUD_TRACE_IS_SAMPLED = 0x1;
81+
private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build();
8082

8183
@Override
8284
public List<String> fields() {
@@ -125,7 +127,7 @@ public List<String> fields() {
125127
traceOptions = OPTIONS_SAMPLED;
126128
}
127129
}
128-
return SpanContext.create(traceId, spanId, traceOptions);
130+
return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
129131
} catch (IllegalArgumentException e) {
130132
throw new SpanContextParseException("Invalid input", e);
131133
}

impl_core/src/main/java/io/opencensus/implcore/trace/SpanBuilderImpl.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.opencensus.trace.SpanId;
3232
import io.opencensus.trace.TraceId;
3333
import io.opencensus.trace.TraceOptions;
34+
import io.opencensus.trace.Tracestate;
3435
import io.opencensus.trace.config.TraceConfig;
3536
import io.opencensus.trace.config.TraceParams;
3637
import java.util.Collections;
@@ -41,8 +42,9 @@
4142

4243
/** Implementation of the {@link SpanBuilder}. */
4344
final class SpanBuilderImpl extends SpanBuilder {
44-
private final Options options;
45+
private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build();
4546

47+
private final Options options;
4648
private final String name;
4749
@Nullable private final Span parent;
4850
@Nullable private final SpanContext remoteParentSpanContext;
@@ -66,6 +68,8 @@ private SpanImpl startSpanInternal(
6668
SpanId spanId = SpanId.generateRandomId(random);
6769
SpanId parentSpanId = null;
6870
TraceOptions.Builder traceOptionsBuilder;
71+
// TODO(bdrutu): Handle tracestate correctly not just propagate.
72+
Tracestate tracestate = TRACESTATE_DEFAULT;
6973
if (parent == null || !parent.isValid()) {
7074
// New root span.
7175
traceId = TraceId.generateRandomId(random);
@@ -77,6 +81,7 @@ private SpanImpl startSpanInternal(
7781
traceId = parent.getTraceId();
7882
parentSpanId = parent.getSpanId();
7983
traceOptionsBuilder = TraceOptions.builder(parent.getTraceOptions());
84+
tracestate = parent.getTracestate();
8085
}
8186
traceOptionsBuilder.setIsSampled(
8287
makeSamplingDecision(
@@ -95,7 +100,7 @@ private SpanImpl startSpanInternal(
95100
}
96101
SpanImpl span =
97102
SpanImpl.startSpan(
98-
SpanContext.create(traceId, spanId, traceOptions),
103+
SpanContext.create(traceId, spanId, traceOptions, tracestate),
99104
spanOptions,
100105
name,
101106
kind,

impl_core/src/main/java/io/opencensus/implcore/trace/propagation/B3Format.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.opencensus.trace.SpanId;
2424
import io.opencensus.trace.TraceId;
2525
import io.opencensus.trace.TraceOptions;
26+
import io.opencensus.trace.Tracestate;
2627
import io.opencensus.trace.propagation.SpanContextParseException;
2728
import io.opencensus.trace.propagation.TextFormat;
2829
import java.util.Arrays;
@@ -38,6 +39,7 @@
3839
* href=https://github.com/openzipkin/b3-propagation>b3-propagation</a>.
3940
*/
4041
final class B3Format extends TextFormat {
42+
private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build();
4143
@VisibleForTesting static final String X_B3_TRACE_ID = "X-B3-TraceId";
4244
@VisibleForTesting static final String X_B3_SPAN_ID = "X-B3-SpanId";
4345
@VisibleForTesting static final String X_B3_PARENT_SPAN_ID = "X-B3-ParentSpanId";
@@ -103,7 +105,7 @@ public List<String> fields() {
103105
|| FLAGS_VALUE.equals(getter.get(carrier, X_B3_FLAGS))) {
104106
traceOptions = TraceOptions.builder().setIsSampled(true).build();
105107
}
106-
return SpanContext.create(traceId, spanId, traceOptions);
108+
return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
107109
} catch (IllegalArgumentException e) {
108110
throw new SpanContextParseException("Invalid input.", e);
109111
}

impl_core/src/main/java/io/opencensus/implcore/trace/propagation/BinaryFormatImpl.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.opencensus.trace.SpanId;
2424
import io.opencensus.trace.TraceId;
2525
import io.opencensus.trace.TraceOptions;
26+
import io.opencensus.trace.Tracestate;
2627
import io.opencensus.trace.propagation.BinaryFormat;
2728
import io.opencensus.trace.propagation.SpanContextParseException;
2829

@@ -61,6 +62,7 @@
6162
* </ul>
6263
*/
6364
final class BinaryFormatImpl extends BinaryFormat {
65+
private static final Tracestate TRACESTATE_DEFAULT = Tracestate.builder().build();
6466
private static final byte VERSION_ID = 0;
6567
private static final int VERSION_ID_OFFSET = 0;
6668
// The version_id/field_id size in bytes.
@@ -141,6 +143,6 @@ public SpanContext fromByteArray(byte[] bytes) throws SpanContextParseException
141143
}
142144
traceOptions = TraceOptions.fromBytes(bytes, pos + ID_SIZE);
143145
}
144-
return SpanContext.create(traceId, spanId, traceOptions);
146+
return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
145147
}
146148
}

0 commit comments

Comments
 (0)