Skip to content

Commit e3c92dd

Browse files
committed
chore: Extract tristate as an enum
Signed-off-by: Brice Dutheil <[email protected]>
1 parent 59496fc commit e3c92dd

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

dd-trace-core/src/main/java/datadog/trace/common/metrics/SerializingMetricWriter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ public final class SerializingMetricWriter implements MetricWriter {
3636
private static final byte[] SPAN_KIND = "SpanKind".getBytes(ISO_8859_1);
3737
private static final byte[] PEER_TAGS = "PeerTags".getBytes(ISO_8859_1);
3838

39-
@SuppressWarnings("unused") // Kept for representing all possible states
40-
public static final int TRISTATE_UNKNOWN = 0;
41-
42-
public static final int TRISTATE_TRUE = 1;
43-
public static final int TRISTATE_FALSE = 2;
39+
// Constant declared here for compile-time folding
40+
public static final int TRISTATE_TRUE = TriState.TRUE.serialValue;
41+
public static final int TRISTATE_FALSE = TriState.FALSE.serialValue;
4442

4543
private final WellKnownTags wellKnownTags;
4644
private final WritableFormatter writer;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package datadog.trace.common.metrics;
2+
3+
/**
4+
* TriState is used to represent a three-valued logic: true, false, and unknown.
5+
* The "integer" values are used for metrics serialization.
6+
*/
7+
public enum TriState {
8+
UNKNOWN(0),
9+
TRUE(1),
10+
FALSE(2);
11+
public final int serialValue;
12+
13+
TriState(int serialValue) {
14+
this.serialValue = serialValue;
15+
}
16+
}

dd-trace-core/src/test/groovy/datadog/trace/common/metrics/SerializingMetricWriterTest.groovy

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS
1717
import static java.util.concurrent.TimeUnit.SECONDS
1818

1919
class SerializingMetricWriterTest extends DDSpecification {
20-
21-
public static final int TRISTATE_TRUE = 1
22-
public static final int TRISTATE_FALSE = 2
23-
2420
def "should produce correct message #iterationIndex with process tags enabled #withProcessTags" () {
2521
setup:
2622
if (withProcessTags) {
@@ -180,7 +176,7 @@ class SerializingMetricWriterTest extends DDSpecification {
180176
assert unpacker.unpackBoolean() == key.isSynthetics()
181177
++elementCount
182178
assert unpacker.unpackString() == "IsTraceRoot"
183-
assert unpacker.unpackInt() == (key.isTraceRoot() ? TRISTATE_TRUE : TRISTATE_FALSE)
179+
assert unpacker.unpackInt() == (key.isTraceRoot() ? TriState.TRUE.serialValue : TriState.FALSE.serialValue)
184180
++elementCount
185181
assert unpacker.unpackString() == "SpanKind"
186182
assert unpacker.unpackString() == key.getSpanKind() as String

0 commit comments

Comments
 (0)