-
Notifications
You must be signed in to change notification settings - Fork 279
/
TraceMapperV0_4.java
325 lines (295 loc) · 10.6 KB
/
TraceMapperV0_4.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package datadog.trace.common.writer.ddagent;
import static datadog.communication.http.OkHttpUtils.msgpackRequestBodyOf;
import datadog.communication.serialization.Codec;
import datadog.communication.serialization.GrowableBuffer;
import datadog.communication.serialization.Writable;
import datadog.communication.serialization.msgpack.MsgPackWriter;
import datadog.trace.bootstrap.instrumentation.api.InstrumentationTags;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.common.writer.Payload;
import datadog.trace.core.CoreSpan;
import datadog.trace.core.Metadata;
import datadog.trace.core.MetadataConsumer;
import datadog.trace.core.PendingTrace;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import okhttp3.RequestBody;
public final class TraceMapperV0_4 implements TraceMapper {
private final int size;
public TraceMapperV0_4(int size) {
this.size = size;
}
public TraceMapperV0_4() {
this(5 << 20);
}
private static final class MetaWriter implements MetadataConsumer {
private Writable writable;
private boolean writeSamplingPriority;
MetaWriter withWritable(Writable writable) {
this.writable = writable;
return this;
}
MetaWriter withWriteSamplingPriority(final boolean writeSamplingPriority) {
this.writeSamplingPriority = writeSamplingPriority;
return this;
}
@Override
public void accept(Metadata metadata) {
int metaSize =
metadata.getBaggage().size()
+ metadata.getTags().size()
+ (null == metadata.getHttpStatusCode() ? 0 : 1)
+ (null == metadata.getOrigin() ? 0 : 1)
+ 1;
int metricsSize =
(writeSamplingPriority && metadata.hasSamplingPriority() ? 1 : 0)
+ (metadata.measured() ? 1 : 0)
+ (metadata.topLevel() ? 1 : 0)
+ (metadata.longRunningVersion() != 0 ? 1 : 0)
+ 1;
for (Map.Entry<String, Object> tag : metadata.getTags().entrySet()) {
Object value = tag.getValue();
if (value instanceof Number) {
++metricsSize;
--metaSize;
} else if (value instanceof Map) {
// Compute size based on amount of elements in tree
--metaSize;
metaSize += getFlatMapSize((Map) value);
}
}
writable.writeUTF8(METRICS);
writable.startMap(metricsSize);
if (writeSamplingPriority && metadata.hasSamplingPriority()) {
writable.writeUTF8(SAMPLING_PRIORITY_KEY);
writable.writeInt(metadata.samplingPriority());
}
if (metadata.measured()) {
writable.writeUTF8(InstrumentationTags.DD_MEASURED);
writable.writeInt(1);
}
if (metadata.topLevel()) {
writable.writeUTF8(InstrumentationTags.DD_TOP_LEVEL);
writable.writeInt(1);
}
if (metadata.longRunningVersion() != 0) {
if (metadata.longRunningVersion() > 0) {
writable.writeUTF8(InstrumentationTags.DD_PARTIAL_VERSION);
writable.writeInt(metadata.longRunningVersion());
} else {
writable.writeUTF8(InstrumentationTags.DD_WAS_LONG_RUNNING);
writable.writeInt(1);
}
}
writable.writeUTF8(THREAD_ID);
writable.writeLong(metadata.getThreadId());
for (Map.Entry<String, Object> entry : metadata.getTags().entrySet()) {
if (entry.getValue() instanceof Number) {
writable.writeString(entry.getKey(), null);
writable.writeObject(entry.getValue(), null);
}
}
writable.writeUTF8(META);
writable.startMap(metaSize);
// we don't need to deduplicate any overlap between tags and baggage here
// since they will be accumulated into maps in the same order downstream,
// we just need to be sure that the size is the same as the number of elements
for (Map.Entry<String, String> entry : metadata.getBaggage().entrySet()) {
writable.writeString(entry.getKey(), null);
writable.writeString(entry.getValue(), null);
}
writable.writeUTF8(THREAD_NAME);
writable.writeUTF8(metadata.getThreadName());
if (null != metadata.getHttpStatusCode()) {
writable.writeUTF8(HTTP_STATUS);
writable.writeUTF8(metadata.getHttpStatusCode());
}
if (null != metadata.getOrigin()) {
writable.writeUTF8(ORIGIN_KEY);
writable.writeString(metadata.getOrigin(), null);
}
for (Map.Entry<String, Object> entry : metadata.getTags().entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof Map) {
// Write map as flat map
writeFlatMap(key, (Map) value);
} else if (!(value instanceof Number)) {
writable.writeString(entry.getKey(), null);
writable.writeObjectString(entry.getValue(), null);
}
}
}
/**
* Calculate number of all values from map and all sub-maps Assuming map could be a binary tree
*
* @param map map to traverse
* @return number of all elements in the tree
*/
private int getFlatMapSize(Map<String, Object> map) {
int size = 0;
for (Object value : map.values()) {
if (value instanceof Map) {
size += getFlatMapSize((Map) value);
} else {
size++;
}
}
return size;
}
/**
* Method write map of maps into writeable as FlatMap
*
* <p>Example: "root": { "key1": "val1" "key2": { "sub1": "val2", "sub2": "val3" } } "plain":
* "123"
*
* <p>Result: "root.key1" -> "val1" "root.key2.sub1" -> "val2" "root.key2.sub2" -> "val3"
* "plain" -> "123"
*
* @param key key name used as base
* @param mapValue map of tags that can contain sub-maps as values
*/
private void writeFlatMap(String key, Map<String, Object> mapValue) {
for (Map.Entry<String, Object> entry : mapValue.entrySet()) {
String newKey = key + '.' + entry.getKey();
Object newValue = entry.getValue();
if (newValue instanceof Map) {
writeFlatMap(newKey, (Map) newValue);
} else {
writable.writeString(newKey, null);
writable.writeObjectString(newValue, null);
}
}
}
}
/**
* The MetaStruct field can safely be used with v4 agents and will be discarded for other
* versions.
*
* <p>Any type that needs to be serialized as part of the meta_struct field has to either be a JDK
* known type (primitives, wrappers, collections ...) or registered with {@link
* datadog.communication.serialization.Codec#Codec(Map)}, in the rest of the cases the {@code
* toString} representation of the object will be used instead
*/
public static class MetaStructWriter {
private static final UTF8BytesString META_STRUCT = UTF8BytesString.create("meta_struct");
private static final int BUFFER_SIZE = 1 << 10;
private Writable writable;
MetaStructWriter withWritable(final Writable writable) {
this.writable = writable;
return this;
}
public void write(final Map<String, Object> metaStruct) {
writable.writeUTF8(META_STRUCT);
writable.startMap(metaStruct.size());
final GrowableBuffer buffer = new GrowableBuffer(BUFFER_SIZE);
final MsgPackWriter metaStructWriter = new MsgPackWriter(Codec.INSTANCE, buffer);
for (Map.Entry<String, Object> entry : metaStruct.entrySet()) {
writeMetaStructEntry(metaStructWriter, buffer, entry.getKey(), entry.getValue());
}
}
private void writeMetaStructEntry(
final MsgPackWriter writer,
final GrowableBuffer buffer,
final String key,
final Object value) {
buffer.mark();
try {
writer.writeObject(value, null);
writer.flush();
writable.writeString(key, null);
writable.writeBinary(buffer.slice());
} finally {
buffer.reset();
}
}
}
private final MetaWriter metaWriter = new MetaWriter();
private final MetaStructWriter metaStructWriter = new MetaStructWriter();
@Override
public void map(List<? extends CoreSpan<?>> trace, final Writable writable) {
writable.startArray(trace.size());
for (int i = 0; i < trace.size(); i++) {
final CoreSpan<?> span = trace.get(i);
final Map<String, Object> metaStruct = span.getMetaStruct();
writable.startMap(metaStruct.isEmpty() ? 12 : 13);
/* 1 */
writable.writeUTF8(SERVICE);
writable.writeString(span.getServiceName(), null);
/* 2 */
writable.writeUTF8(NAME);
writable.writeObject(span.getOperationName(), null);
/* 3 */
writable.writeUTF8(RESOURCE);
writable.writeObject(span.getResourceName(), null);
/* 4 */
writable.writeUTF8(TRACE_ID);
writable.writeUnsignedLong(span.getTraceId().toLong());
/* 5 */
writable.writeUTF8(SPAN_ID);
writable.writeUnsignedLong(span.getSpanId());
/* 6 */
writable.writeUTF8(PARENT_ID);
writable.writeUnsignedLong(span.getParentId());
/* 7 */
writable.writeUTF8(START);
writable.writeLong(span.getStartTime());
/* 8 */
writable.writeUTF8(DURATION);
writable.writeLong(PendingTrace.getDurationNano(span));
/* 9 */
writable.writeUTF8(TYPE);
writable.writeString(span.getType(), null);
/* 10 */
writable.writeUTF8(ERROR);
writable.writeInt(span.getError());
/* 11, 12 */
span.processTagsAndBaggage(
metaWriter
.withWritable(writable)
.withWriteSamplingPriority(i == 0 || i == trace.size() - 1));
if (!metaStruct.isEmpty()) {
/* 13 */
metaStructWriter.withWritable(writable).write(metaStruct);
}
}
}
@Override
public Payload newPayload() {
return new PayloadV0_4();
}
@Override
public int messageBufferSize() {
return size; // 5MB
}
@Override
public void reset() {}
@Override
public String endpoint() {
return "v0.4";
}
private static class PayloadV0_4 extends Payload {
@Override
public int sizeInBytes() {
return msgpackArrayHeaderSize(traceCount()) + body.remaining();
}
@Override
public void writeTo(WritableByteChannel channel) throws IOException {
ByteBuffer header = msgpackArrayHeader(traceCount());
while (header.hasRemaining()) {
channel.write(header);
}
while (body.hasRemaining()) {
channel.write(body);
}
}
@Override
public RequestBody toRequest() {
return msgpackRequestBodyOf(Arrays.asList(msgpackArrayHeader(traceCount()), body));
}
}
}