-
Notifications
You must be signed in to change notification settings - Fork 839
/
TraceFlags.java
253 lines (226 loc) · 7.07 KB
/
TraceFlags.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.trace;
import io.opentelemetry.internal.Utils;
import java.util.Arrays;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* A class that represents global trace options. These options are propagated to all child {@link
* Span spans}. These determine features such as whether a {@code Span} should be traced. It is
* implemented as a bitmask.
*
* @since 0.1.0
*/
@Immutable
public final class TraceFlags {
// Default options. Nothing set.
private static final byte DEFAULT_OPTIONS = 0;
// Bit to represent whether trace is sampled or not.
private static final byte IS_SAMPLED = 0x1;
private static final int SIZE = 1;
private static final int BASE16_SIZE = 2 * SIZE;
private static final TraceFlags DEFAULT = fromByte(DEFAULT_OPTIONS);
// The set of enabled features is determined by all the enabled bits.
private final byte options;
// Creates a new {@code TraceFlags} with the given options.
private TraceFlags(byte options) {
this.options = options;
}
/**
* Returns the size in bytes of the {@code TraceFlags}.
*
* @return the size in bytes of the {@code TraceFlags}.
* @since 0.1.0
*/
public static int getSize() {
return SIZE;
}
/**
* Returns the default {@code TraceFlags}.
*
* @return the default {@code TraceFlags}.
* @since 0.1.0
*/
public static TraceFlags getDefault() {
return DEFAULT;
}
/**
* Returns a {@code TraceFlags} whose representation is {@code src}.
*
* @param src the byte representation of the {@code TraceFlags}.
* @return a {@code TraceFlags} whose representation is {@code src}.
* @since 0.1.0
*/
public static TraceFlags fromByte(byte src) {
return new TraceFlags(src);
}
/**
* Returns a {@code TraceOption} built from a lowercase base16 representation.
*
* @param src the lowercase base16 representation.
* @param srcOffset the offset in the buffer where the representation of the {@code TraceFlags}
* begins.
* @return a {@code TraceOption} built from a lowercase base16 representation.
* @throws NullPointerException if {@code src} is null.
* @throws IllegalArgumentException if {@code src.length} is not {@code 2 * TraceOption.SIZE} OR
* if the {@code str} has invalid characters.
* @since 0.1.0
*/
public static TraceFlags fromLowerBase16(CharSequence src, int srcOffset) {
return new TraceFlags(BigendianEncoding.byteFromBase16String(src, srcOffset));
}
/**
* Returns the one byte representation of the {@code TraceFlags}.
*
* @return the one byte representation of the {@code TraceFlags}.
* @since 0.1.0
*/
public byte getByte() {
return options;
}
/**
* Copies the byte representations of the {@code TraceFlags} into the {@code dest} beginning at
* the {@code destOffset} offset.
*
* <p>Equivalent with (but faster because it avoids any new allocations):
*
* <pre>{@code
* System.arraycopy(getBytes(), 0, dest, destOffset, TraceFlags.getSize());
* }</pre>
*
* @param dest the destination buffer.
* @param destOffset the starting offset in the destination buffer.
* @throws NullPointerException if {@code dest} is null.
* @throws IndexOutOfBoundsException if {@code destOffset+TraceFlags.getSize()} is greater than
* {@code dest.length}.
* @since 0.1.0
*/
public void copyBytesTo(byte[] dest, int destOffset) {
Utils.checkIndex(destOffset, dest.length);
dest[destOffset] = options;
}
/**
* Copies the lowercase base16 representations of the {@code TraceId} into the {@code dest}
* beginning at the {@code destOffset} offset.
*
* @param dest the destination buffer.
* @param destOffset the starting offset in the destination buffer.
* @throws IndexOutOfBoundsException if {@code destOffset + 2} is greater than {@code
* dest.length}.
* @since 0.1.0
*/
public void copyLowerBase16To(char[] dest, int destOffset) {
BigendianEncoding.byteToBase16String(options, dest, destOffset);
}
/**
* Returns the lowercase base16 encoding of this {@code TraceFlags}.
*
* @return the lowercase base16 encoding of this {@code TraceFlags}.
* @since 0.1.0
*/
public String toLowerBase16() {
char[] chars = new char[BASE16_SIZE];
copyLowerBase16To(chars, 0);
return new String(chars);
}
/**
* Returns a new {@link Builder} with default options.
*
* @return a new {@code Builder} with default options.
* @since 0.1.0
*/
public static Builder builder() {
return new Builder(DEFAULT_OPTIONS);
}
/**
* Returns a new {@link Builder} with all given options set.
*
* @param traceFlags the given options set.
* @return a new {@code Builder} with all given options set.
* @since 0.1.0
*/
public static Builder builder(TraceFlags traceFlags) {
return new Builder(traceFlags.options);
}
/**
* Returns a boolean indicating whether this {@code Span} is part of a sampled trace and data
* should be exported to a persistent store.
*
* @return a boolean indicating whether the trace is sampled.
* @since 0.1.0
*/
public boolean isSampled() {
return hasOption(IS_SAMPLED);
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof TraceFlags)) {
return false;
}
TraceFlags that = (TraceFlags) obj;
return options == that.options;
}
@Override
public int hashCode() {
return Arrays.hashCode(new byte[] {options});
}
@Override
public String toString() {
return "TraceFlags{sampled=" + isSampled() + "}";
}
/**
* Builder class for {@link TraceFlags}.
*
* @since 0.1.0
*/
public static final class Builder {
private byte options;
private Builder(byte options) {
this.options = options;
}
/**
* Sets the sampling bit in the options.
*
* @param isSampled the sampling bit.
* @return this.
* @since 0.1.0
*/
public Builder setIsSampled(boolean isSampled) {
if (isSampled) {
options = (byte) (options | IS_SAMPLED);
} else {
options = (byte) (options & ~IS_SAMPLED);
}
return this;
}
/**
* Builds and returns a {@code TraceFlags} with the desired options.
*
* @return a {@code TraceFlags} with the desired options.
* @since 0.1.0
*/
public TraceFlags build() {
return fromByte(options);
}
}
private boolean hasOption(int mask) {
return (this.options & mask) != 0;
}
}