Skip to content

Commit 71f3bbb

Browse files
committed
Simplify test
1 parent b47d44a commit 71f3bbb

1 file changed

Lines changed: 37 additions & 63 deletions

File tree

dd-trace-core/src/test/java/datadog/trace/core/LateSpanSamplingMechanismSerializationTest.java

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
import static org.junit.jupiter.api.Assertions.assertEquals;
88
import static org.junit.jupiter.api.Assertions.assertTrue;
99

10-
import datadog.communication.serialization.ByteBufferConsumer;
11-
import datadog.communication.serialization.FlushingBuffer;
10+
import datadog.communication.serialization.GrowableBuffer;
1211
import datadog.communication.serialization.msgpack.MsgPackWriter;
1312
import datadog.trace.common.writer.ListWriter;
1413
import datadog.trace.common.writer.ddagent.TraceMapperV0_4;
15-
import java.nio.ByteBuffer;
14+
import java.io.IOException;
1615
import java.util.ArrayList;
17-
import java.util.HashMap;
1816
import java.util.List;
19-
import java.util.Map;
2017
import java.util.concurrent.TimeoutException;
2118
import org.junit.jupiter.api.Test;
2219
import org.msgpack.core.MessagePack;
2320
import org.msgpack.core.MessageUnpacker;
21+
import org.msgpack.value.MapValue;
22+
import org.msgpack.value.Value;
23+
import org.msgpack.value.ValueFactory;
2424

2525
/**
2626
* Verifies that trace-level sampling metadata is preserved when a root and a later child are
@@ -70,87 +70,61 @@ void rootlessLateChunkRetainsAdaptiveSamplingMechanism()
7070
assertEquals(singletonList(root), writer.get(0));
7171
assertEquals(singletonList(lateChild), writer.get(1));
7272

73-
SerializedChunk rootChunk = writer.serializedChunks.get(0);
74-
SerializedChunk lateChunk = writer.serializedChunks.get(1);
73+
MapValue rootChunk = writer.serializedSpans.get(0);
74+
MapValue lateChunk = writer.serializedSpans.get(1);
7575

7676
assertEquals(
77-
(int) USER_KEEP, rootChunk.metrics.get(DDSpanContext.PRIORITY_SAMPLING_KEY).intValue());
78-
assertEquals("-12", rootChunk.meta.get("_dd.p.dm"));
77+
(int) USER_KEEP,
78+
numberValue(rootChunk, "metrics", DDSpanContext.PRIORITY_SAMPLING_KEY).intValue());
79+
assertEquals("-12", stringValue(rootChunk, "meta", "_dd.p.dm"));
7980

8081
assertEquals(
81-
(int) USER_KEEP, lateChunk.metrics.get(DDSpanContext.PRIORITY_SAMPLING_KEY).intValue());
82+
(int) USER_KEEP,
83+
numberValue(lateChunk, "metrics", DDSpanContext.PRIORITY_SAMPLING_KEY).intValue());
8284
// The late chunk's decision maker must match its propagated trace-level sampling priority.
83-
assertEquals("-12", lateChunk.meta.get("_dd.p.dm"));
85+
assertEquals("-12", stringValue(lateChunk, "meta", "_dd.p.dm"));
8486
} finally {
8587
tracer.close();
8688
}
8789
}
8890

91+
private static Number numberValue(MapValue span, String mapName, String key) {
92+
Value value = value(span, mapName, key);
93+
return value == null ? null : value.asNumberValue().toInt();
94+
}
95+
96+
private static String stringValue(MapValue span, String mapName, String key) {
97+
Value value = value(span, mapName, key);
98+
return value == null ? null : value.asStringValue().asString();
99+
}
100+
101+
private static Value value(MapValue span, String mapName, String key) {
102+
Value map = span.map().get(ValueFactory.newString(mapName));
103+
return map.asMapValue().map().get(ValueFactory.newString(key));
104+
}
105+
89106
private static final class SerializingWriter extends ListWriter {
90-
private final List<SerializedChunk> serializedChunks = new ArrayList<>();
107+
private final List<MapValue> serializedSpans = new ArrayList<>();
91108

92109
@Override
93110
public void write(List<DDSpan> trace) {
94-
serializedChunks.add(serialize(trace));
111+
serializedSpans.add(serialize(trace));
95112
super.write(trace);
96113
}
97114

98-
private static SerializedChunk serialize(List<DDSpan> trace) {
115+
private static MapValue serialize(List<DDSpan> trace) {
99116
TraceMapperV0_4 mapper = new TraceMapperV0_4();
100-
CaptureBuffer capture = new CaptureBuffer();
101-
MsgPackWriter packer = new MsgPackWriter(new FlushingBuffer(16 * 1024, capture));
117+
GrowableBuffer buffer = new GrowableBuffer(16 * 1024);
118+
MsgPackWriter packer = new MsgPackWriter(buffer);
102119
assertTrue(packer.format(trace, mapper));
103-
packer.flush();
104-
105-
try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(capture.bytes)) {
106-
assertEquals(1, unpacker.unpackArrayHeader());
107-
int fieldCount = unpacker.unpackMapHeader();
108-
Map<String, Number> metrics = new HashMap<>();
109-
Map<String, String> meta = new HashMap<>();
110-
111-
for (int i = 0; i < fieldCount; i++) {
112-
String field = unpacker.unpackString();
113-
if ("metrics".equals(field)) {
114-
int size = unpacker.unpackMapHeader();
115-
for (int j = 0; j < size; j++) {
116-
metrics.put(unpacker.unpackString(), unpacker.unpackValue().asNumberValue().toInt());
117-
}
118-
} else if ("meta".equals(field)) {
119-
int size = unpacker.unpackMapHeader();
120-
for (int j = 0; j < size; j++) {
121-
meta.put(unpacker.unpackString(), unpacker.unpackString());
122-
}
123-
} else {
124-
unpacker.unpackValue();
125-
}
126-
}
127-
return new SerializedChunk(metrics, meta);
128-
} catch (Exception e) {
120+
121+
try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(buffer.slice())) {
122+
return unpacker.unpackValue().asArrayValue().get(0).asMapValue();
123+
} catch (IOException e) {
129124
throw new IllegalStateException("Unable to decode serialized trace chunk", e);
130125
} finally {
131126
mapper.reset();
132127
}
133128
}
134129
}
135-
136-
private static final class CaptureBuffer implements ByteBufferConsumer {
137-
private byte[] bytes;
138-
139-
@Override
140-
public void accept(int messageCount, ByteBuffer buffer) {
141-
assertEquals(1, messageCount);
142-
bytes = new byte[buffer.remaining()];
143-
buffer.get(bytes);
144-
}
145-
}
146-
147-
private static final class SerializedChunk {
148-
private final Map<String, Number> metrics;
149-
private final Map<String, String> meta;
150-
151-
private SerializedChunk(Map<String, Number> metrics, Map<String, String> meta) {
152-
this.metrics = metrics;
153-
this.meta = meta;
154-
}
155-
}
156130
}

0 commit comments

Comments
 (0)