Skip to content

Commit 8901e4e

Browse files
author
Bogdan Drutu
authored
Add get/from{Byte} methods on TraceOptions and deprecate get/from{Bytes}. (census-instrumentation#1392)
* Add get/from{Byte} methods on TraceOptions and deprecate get/from{Bytes}. * Update changelog.
1 parent 217a1e8 commit 8901e4e

9 files changed

Lines changed: 74 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Allow custom prefix for Stackdriver metrics in `StackdriverStatsConfiguration`.
1212
- Add support to handle the Tracestate in the SpanContext.
1313
- Remove global synchronization from the get current stats state.
14+
- Add get/from{Byte} methods on TraceOptions and deprecate get/from{Bytes}.
1415

1516
## 0.15.0 - 2018-06-20
1617
- Expose the factory methods of MonitoredResource.

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class TraceOptions {
4848
*
4949
* @since 0.5
5050
*/
51-
public static final TraceOptions DEFAULT = new TraceOptions(DEFAULT_OPTIONS);
51+
public static final TraceOptions DEFAULT = fromByte(DEFAULT_OPTIONS);
5252

5353
// The set of enabled features is determined by all the enabled bits.
5454
private final byte options;
@@ -72,13 +72,15 @@ private TraceOptions(byte options) {
7272
* @throws NullPointerException if {@code buffer} is null.
7373
* @throws IllegalArgumentException if {@code buffer.length} is not {@link TraceOptions#SIZE}.
7474
* @since 0.5
75+
* @deprecated use {@link #fromByte(byte)}.
7576
*/
77+
@Deprecated
7678
public static TraceOptions fromBytes(byte[] buffer) {
7779
Utils.checkNotNull(buffer, "buffer");
7880
Utils.checkArgument(
7981
buffer.length == SIZE,
8082
String.format("Invalid size: expected %s, got %s", SIZE, buffer.length));
81-
return new TraceOptions(buffer[0]);
83+
return fromByte(buffer[0]);
8284
}
8385

8486
/**
@@ -93,18 +95,44 @@ public static TraceOptions fromBytes(byte[] buffer) {
9395
* @throws IndexOutOfBoundsException if {@code srcOffset+TraceOptions.SIZE} is greater than {@code
9496
* src.length}.
9597
* @since 0.5
98+
* @deprecated use {@link #fromByte(byte)}.
9699
*/
100+
@Deprecated
97101
public static TraceOptions fromBytes(byte[] src, int srcOffset) {
98102
Utils.checkIndex(srcOffset, src.length);
99-
return new TraceOptions(src[srcOffset]);
103+
return fromByte(src[srcOffset]);
104+
}
105+
106+
/**
107+
* Returns a {@code TraceOptions} whose representation is {@code src}.
108+
*
109+
* @param src the byte representation of the {@code TraceOptions}.
110+
* @return a {@code TraceOptions} whose representation is {@code src}.
111+
* @since 0.16
112+
*/
113+
public static TraceOptions fromByte(byte src) {
114+
// TODO(bdrutu): OPTIMIZATION: Cache all the 256 possible objects and return from the cache.
115+
return new TraceOptions(src);
116+
}
117+
118+
/**
119+
* Returns the one byte representation of the {@code TraceOptions}.
120+
*
121+
* @return the one byte representation of the {@code TraceOptions}.
122+
* @since 0.16
123+
*/
124+
public byte getByte() {
125+
return options;
100126
}
101127

102128
/**
103129
* Returns the 1-byte array representation of the {@code TraceOptions}.
104130
*
105131
* @return the 1-byte array representation of the {@code TraceOptions}.
106132
* @since 0.5
133+
* @deprecated use {@link #getByte()}.
107134
*/
135+
@Deprecated
108136
public byte[] getBytes() {
109137
byte[] bytes = new byte[SIZE];
110138
bytes[0] = options;
@@ -237,7 +265,7 @@ public Builder setIsSampled(boolean isSampled) {
237265
* @since 0.5
238266
*/
239267
public TraceOptions build() {
240-
return new TraceOptions(options);
268+
return fromByte(options);
241269
}
242270
}
243271

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
/** Unit tests for {@link TraceOptions}. */
2727
@RunWith(JUnit4.class)
2828
public class TraceOptionsTest {
29-
private static final byte[] firstBytes = {(byte) 0xff};
30-
private static final byte[] secondBytes = {1};
31-
private static final byte[] thirdBytes = {6};
29+
private static final byte FIRST_BYTE = (byte) 0xff;
30+
private static final byte SECOND_BYTE = 1;
31+
private static final byte THIRD_BYTE = 6;
3232

3333
@Test
3434
public void getOptions() {
@@ -37,9 +37,9 @@ public void getOptions() {
3737
assertThat(TraceOptions.builder().setIsSampled(true).build().getOptions()).isEqualTo(1);
3838
assertThat(TraceOptions.builder().setIsSampled(true).setIsSampled(false).build().getOptions())
3939
.isEqualTo(0);
40-
assertThat(TraceOptions.fromBytes(firstBytes).getOptions()).isEqualTo(-1);
41-
assertThat(TraceOptions.fromBytes(secondBytes).getOptions()).isEqualTo(1);
42-
assertThat(TraceOptions.fromBytes(thirdBytes).getOptions()).isEqualTo(6);
40+
assertThat(TraceOptions.fromByte(FIRST_BYTE).getOptions()).isEqualTo(-1);
41+
assertThat(TraceOptions.fromByte(SECOND_BYTE).getOptions()).isEqualTo(1);
42+
assertThat(TraceOptions.fromByte(THIRD_BYTE).getOptions()).isEqualTo(6);
4343
}
4444

4545
@Test
@@ -49,16 +49,30 @@ public void isSampled() {
4949
}
5050

5151
@Test
52-
public void toFromBytes() {
53-
assertThat(TraceOptions.fromBytes(firstBytes).getBytes()).isEqualTo(firstBytes);
54-
assertThat(TraceOptions.fromBytes(secondBytes).getBytes()).isEqualTo(secondBytes);
55-
assertThat(TraceOptions.fromBytes(thirdBytes).getBytes()).isEqualTo(thirdBytes);
52+
public void toFromByte() {
53+
assertThat(TraceOptions.fromByte(FIRST_BYTE).getByte()).isEqualTo(FIRST_BYTE);
54+
assertThat(TraceOptions.fromByte(SECOND_BYTE).getByte()).isEqualTo(SECOND_BYTE);
55+
assertThat(TraceOptions.fromByte(THIRD_BYTE).getByte()).isEqualTo(THIRD_BYTE);
56+
}
57+
58+
@Test
59+
@SuppressWarnings("deprecation")
60+
public void deprecated_fromBytes() {
61+
assertThat(TraceOptions.fromBytes(new byte[] {FIRST_BYTE}).getByte()).isEqualTo(FIRST_BYTE);
62+
assertThat(TraceOptions.fromBytes(new byte[] {1, FIRST_BYTE}, 1).getByte())
63+
.isEqualTo(FIRST_BYTE);
64+
}
65+
66+
@Test
67+
@SuppressWarnings("deprecation")
68+
public void deprecated_getBytes() {
69+
assertThat(TraceOptions.fromByte(FIRST_BYTE).getBytes()).isEqualTo(new byte[] {FIRST_BYTE});
5670
}
5771

5872
@Test
5973
public void builder_FromOptions() {
6074
assertThat(
61-
TraceOptions.builder(TraceOptions.fromBytes(thirdBytes))
75+
TraceOptions.builder(TraceOptions.fromByte(THIRD_BYTE))
6276
.setIsSampled(true)
6377
.build()
6478
.getOptions())
@@ -70,8 +84,8 @@ public void traceOptions_EqualsAndHashCode() {
7084
EqualsTester tester = new EqualsTester();
7185
tester.addEqualityGroup(TraceOptions.DEFAULT);
7286
tester.addEqualityGroup(
73-
TraceOptions.fromBytes(secondBytes), TraceOptions.builder().setIsSampled(true).build());
74-
tester.addEqualityGroup(TraceOptions.fromBytes(firstBytes));
87+
TraceOptions.fromByte(SECOND_BYTE), TraceOptions.builder().setIsSampled(true).build());
88+
tester.addEqualityGroup(TraceOptions.fromByte(FIRST_BYTE));
7589
tester.testEquals();
7690
}
7791

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public class BinaryPropagationImplBenchmark {
4040
private static final TraceId traceId = TraceId.fromBytes(traceIdBytes);
4141
private static final byte[] spanIdBytes = new byte[] {(byte) 0xFF, 0, 0, 0, 0, 0, 0, 0};
4242
private static final SpanId spanId = SpanId.fromBytes(spanIdBytes);
43-
private static final byte[] traceOptionsBytes = new byte[] {1};
44-
private static final TraceOptions traceOptions = TraceOptions.fromBytes(traceOptionsBytes);
43+
private static final byte TRACE_OPTIONS_BYTE = 1;
44+
private static final TraceOptions traceOptions = TraceOptions.fromByte(TRACE_OPTIONS_BYTE);
4545
private static final SpanContext spanContext =
4646
SpanContext.create(traceId, spanId, traceOptions, Tracestate.builder().build());
4747
private static final BinaryFormat binaryFormat =

exporters/trace/instana/src/test/java/io/opencensus/exporter/trace/instana/InstanaExporterHandlerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void generateSpan_NoKindAndRemoteParent() {
6969
SpanContext.create(
7070
TraceId.fromLowerBase16(TRACE_ID),
7171
SpanId.fromLowerBase16(SPAN_ID),
72-
TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
72+
TraceOptions.builder().setIsSampled(true).build()),
7373
SpanId.fromLowerBase16(PARENT_SPAN_ID),
7474
true, /* hasRemoteParent */
7575
"SpanName", /* name */
@@ -107,7 +107,7 @@ public void generateSpan_ServerKind() {
107107
SpanContext.create(
108108
TraceId.fromLowerBase16(TRACE_ID),
109109
SpanId.fromLowerBase16(SPAN_ID),
110-
TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
110+
TraceOptions.builder().setIsSampled(true).build()),
111111
SpanId.fromLowerBase16(PARENT_SPAN_ID),
112112
true, /* hasRemoteParent */
113113
"SpanName", /* name */
@@ -145,7 +145,7 @@ public void generateSpan_ClientKind() {
145145
SpanContext.create(
146146
TraceId.fromLowerBase16(TRACE_ID),
147147
SpanId.fromLowerBase16(SPAN_ID),
148-
TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
148+
TraceOptions.builder().setIsSampled(true).build()),
149149
SpanId.fromLowerBase16(PARENT_SPAN_ID),
150150
true, /* hasRemoteParent */
151151
"SpanName", /* name */

exporters/trace/zipkin/src/test/java/io/opencensus/exporter/trace/zipkin/ZipkinExporterHandlerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void generateSpan_NoKindAndRemoteParent() {
7272
SpanContext.create(
7373
TraceId.fromLowerBase16(TRACE_ID),
7474
SpanId.fromLowerBase16(SPAN_ID),
75-
TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
75+
TraceOptions.builder().setIsSampled(true).build()),
7676
// TODO SpanId.fromLowerBase16
7777
SpanId.fromLowerBase16(PARENT_SPAN_ID),
7878
true, /* hasRemoteParent */
@@ -113,7 +113,7 @@ public void generateSpan_ServerKind() {
113113
SpanContext.create(
114114
TraceId.fromLowerBase16(TRACE_ID),
115115
SpanId.fromLowerBase16(SPAN_ID),
116-
TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
116+
TraceOptions.builder().setIsSampled(true).build()),
117117
// TODO SpanId.fromLowerBase16
118118
SpanId.fromLowerBase16(PARENT_SPAN_ID),
119119
true, /* hasRemoteParent */
@@ -154,7 +154,7 @@ public void generateSpan_ClientKind() {
154154
SpanContext.create(
155155
TraceId.fromLowerBase16(TRACE_ID),
156156
SpanId.fromLowerBase16(SPAN_ID),
157-
TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
157+
TraceOptions.builder().setIsSampled(true).build()),
158158
// TODO SpanId.fromLowerBase16
159159
SpanId.fromLowerBase16(PARENT_SPAN_ID),
160160
true, /* hasRemoteParent */
@@ -199,7 +199,7 @@ public void generateSpan_WithAttributes() {
199199
SpanContext.create(
200200
TraceId.fromLowerBase16(TRACE_ID),
201201
SpanId.fromLowerBase16(SPAN_ID),
202-
TraceOptions.fromBytes(new byte[] {1} /* sampled */)),
202+
TraceOptions.builder().setIsSampled(true).build()),
203203
// TODO SpanId.fromLowerBase16
204204
SpanId.fromLowerBase16(PARENT_SPAN_ID),
205205
true, /* hasRemoteParent */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public SpanContext fromByteArray(byte[] bytes) throws SpanContextParseException
141141
if (bytes.length < ALL_FORMAT_LENGTH) {
142142
throw new SpanContextParseException("Invalid input: truncated");
143143
}
144-
traceOptions = TraceOptions.fromBytes(bytes, pos + ID_SIZE);
144+
traceOptions = TraceOptions.fromByte(bytes[pos + ID_SIZE]);
145145
}
146146
return SpanContext.create(traceId, spanId, traceOptions, TRACESTATE_DEFAULT);
147147
}

impl_core/src/test/java/io/opencensus/implcore/trace/propagation/B3FormatTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public class B3FormatTest {
4949
TraceId.fromLowerBase16("0000000000000000" + TRACE_ID_BASE16_EIGHT_BYTES);
5050
private static final String SPAN_ID_BASE16 = "ff00000000000041";
5151
private static final SpanId SPAN_ID = SpanId.fromLowerBase16(SPAN_ID_BASE16);
52-
private static final byte[] TRACE_OPTIONS_BYTES = new byte[] {1};
53-
private static final TraceOptions TRACE_OPTIONS = TraceOptions.fromBytes(TRACE_OPTIONS_BYTES);
52+
private static final byte TRACE_OPTIONS_BYTE = 1;
53+
private static final TraceOptions TRACE_OPTIONS = TraceOptions.fromByte(TRACE_OPTIONS_BYTE);
5454
private final B3Format b3Format = new B3Format();
5555
@Rule public ExpectedException thrown = ExpectedException.none();
5656
private final Setter<Map<String, String>> setter =

impl_core/src/test/java/io/opencensus/implcore/trace/propagation/BinaryFormatImplTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public class BinaryFormatImplTest {
3939
private static final TraceId TRACE_ID = TraceId.fromBytes(TRACE_ID_BYTES);
4040
private static final byte[] SPAN_ID_BYTES = new byte[] {97, 98, 99, 100, 101, 102, 103, 104};
4141
private static final SpanId SPAN_ID = SpanId.fromBytes(SPAN_ID_BYTES);
42-
private static final byte[] TRACE_OPTIONS_BYTES = new byte[] {1};
43-
private static final TraceOptions TRACE_OPTIONS = TraceOptions.fromBytes(TRACE_OPTIONS_BYTES);
42+
private static final byte TRACE_OPTIONS_BYTES = 1;
43+
private static final TraceOptions TRACE_OPTIONS = TraceOptions.fromByte(TRACE_OPTIONS_BYTES);
4444
private static final byte[] EXAMPLE_BYTES =
4545
new byte[] {
4646
0, 0, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 1, 97, 98, 99, 100,

0 commit comments

Comments
 (0)