|
7 | 7 | import static org.junit.jupiter.api.Assertions.assertEquals; |
8 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue; |
9 | 9 |
|
10 | | -import datadog.communication.serialization.ByteBufferConsumer; |
11 | | -import datadog.communication.serialization.FlushingBuffer; |
| 10 | +import datadog.communication.serialization.GrowableBuffer; |
12 | 11 | import datadog.communication.serialization.msgpack.MsgPackWriter; |
13 | 12 | import datadog.trace.common.writer.ListWriter; |
14 | 13 | import datadog.trace.common.writer.ddagent.TraceMapperV0_4; |
15 | | -import java.nio.ByteBuffer; |
| 14 | +import java.io.IOException; |
16 | 15 | import java.util.ArrayList; |
17 | | -import java.util.HashMap; |
18 | 16 | import java.util.List; |
19 | | -import java.util.Map; |
20 | 17 | import java.util.concurrent.TimeoutException; |
21 | 18 | import org.junit.jupiter.api.Test; |
22 | 19 | import org.msgpack.core.MessagePack; |
23 | 20 | import org.msgpack.core.MessageUnpacker; |
| 21 | +import org.msgpack.value.MapValue; |
| 22 | +import org.msgpack.value.Value; |
| 23 | +import org.msgpack.value.ValueFactory; |
24 | 24 |
|
25 | 25 | /** |
26 | 26 | * Verifies that trace-level sampling metadata is preserved when a root and a later child are |
@@ -70,87 +70,61 @@ void rootlessLateChunkRetainsAdaptiveSamplingMechanism() |
70 | 70 | assertEquals(singletonList(root), writer.get(0)); |
71 | 71 | assertEquals(singletonList(lateChild), writer.get(1)); |
72 | 72 |
|
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); |
75 | 75 |
|
76 | 76 | 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")); |
79 | 80 |
|
80 | 81 | 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()); |
82 | 84 | // 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")); |
84 | 86 | } finally { |
85 | 87 | tracer.close(); |
86 | 88 | } |
87 | 89 | } |
88 | 90 |
|
| 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 | + |
89 | 106 | private static final class SerializingWriter extends ListWriter { |
90 | | - private final List<SerializedChunk> serializedChunks = new ArrayList<>(); |
| 107 | + private final List<MapValue> serializedSpans = new ArrayList<>(); |
91 | 108 |
|
92 | 109 | @Override |
93 | 110 | public void write(List<DDSpan> trace) { |
94 | | - serializedChunks.add(serialize(trace)); |
| 111 | + serializedSpans.add(serialize(trace)); |
95 | 112 | super.write(trace); |
96 | 113 | } |
97 | 114 |
|
98 | | - private static SerializedChunk serialize(List<DDSpan> trace) { |
| 115 | + private static MapValue serialize(List<DDSpan> trace) { |
99 | 116 | 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); |
102 | 119 | 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) { |
129 | 124 | throw new IllegalStateException("Unable to decode serialized trace chunk", e); |
130 | 125 | } finally { |
131 | 126 | mapper.reset(); |
132 | 127 | } |
133 | 128 | } |
134 | 129 | } |
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 | | - } |
156 | 130 | } |
0 commit comments