Skip to content

Commit 6f232d3

Browse files
Refactored code duplication.
1 parent c41c697 commit 6f232d3

3 files changed

Lines changed: 54 additions & 53 deletions

File tree

utils/test-agent-utils/decoder/src/main/java/datadog/trace/test/agent/decoder/v1/raw/MessageV1.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,7 @@ private static void skipPayloadField(
8989
break;
9090
case 10:
9191
// Header-level attributes: same triplet encoding as span attributes.
92-
int arraySize = unpacker.unpackArrayHeader();
93-
if (arraySize % 3 != 0) {
94-
throw new IllegalArgumentException(
95-
"Attributes array size must be divisible by 3, got: " + arraySize);
96-
}
97-
int tripletCount = arraySize / 3;
98-
for (int i = 0; i < tripletCount; i++) {
99-
SpanV1.unpackStreamingString(unpacker, stringTable);
100-
int valueType = unpacker.unpackInt();
101-
switch (valueType) {
102-
case SpanV1.STRING_VALUE_TYPE:
103-
SpanV1.unpackStreamingString(unpacker, stringTable);
104-
break;
105-
case SpanV1.BOOL_VALUE_TYPE:
106-
unpacker.unpackBoolean();
107-
break;
108-
case SpanV1.FLOAT_VALUE_TYPE:
109-
unpacker.unpackDouble();
110-
break;
111-
default:
112-
unpacker.skipValue();
113-
break;
114-
}
115-
}
92+
SpanV1.skipAttributes(unpacker, stringTable);
11693
break;
11794
default:
11895
unpacker.skipValue();

utils/test-agent-utils/decoder/src/main/java/datadog/trace/test/agent/decoder/v1/raw/SpanV1.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,58 @@ private static void unpackAttributes(
324324
}
325325
}
326326

327+
static void skipAttributes(MessageUnpacker unpacker, List<String> stringTable)
328+
throws IOException {
329+
forEachAttribute(unpacker, stringTable, AttributeSkipper.INSTANCE);
330+
}
331+
332+
private static void forEachAttribute(
333+
MessageUnpacker unpacker, List<String> stringTable, AttributeConsumer consumer)
334+
throws IOException {
335+
int arraySize = unpacker.unpackArrayHeader();
336+
// Array contains triplets (key, type, value), so size must be divisible by 3
337+
if (arraySize % 3 != 0) {
338+
throw new IllegalArgumentException(
339+
"Attributes array size must be divisible by 3, got: " + arraySize);
340+
}
341+
342+
int tripletCount = arraySize / 3;
343+
for (int i = 0; i < tripletCount; i++) {
344+
String key = unpackStreamingString(unpacker, stringTable);
345+
int valueType = unpacker.unpackInt();
346+
consumer.accept(unpacker, stringTable, key, valueType);
347+
}
348+
}
349+
350+
private interface AttributeConsumer {
351+
void accept(MessageUnpacker unpacker, List<String> stringTable, String key, int valueType)
352+
throws IOException;
353+
}
354+
355+
private enum AttributeSkipper implements AttributeConsumer {
356+
INSTANCE;
357+
358+
@Override
359+
public void accept(
360+
MessageUnpacker unpacker, List<String> stringTable, String key, int valueType)
361+
throws IOException {
362+
switch (valueType) {
363+
case STRING_VALUE_TYPE:
364+
unpackStreamingString(unpacker, stringTable);
365+
break;
366+
case BOOL_VALUE_TYPE:
367+
unpacker.unpackBoolean();
368+
break;
369+
case FLOAT_VALUE_TYPE:
370+
unpacker.unpackDouble();
371+
break;
372+
default:
373+
unpacker.skipValue();
374+
break;
375+
}
376+
}
377+
}
378+
327379
/**
328380
* Converts span kind integer to string representation.
329381
*

utils/test-agent-utils/decoder/src/main/java/datadog/trace/test/agent/decoder/v1/raw/TraceV1.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static TraceV1 unpackChunk(MessageUnpacker unpacker, List<String> string
6565
break;
6666
case 3:
6767
// chunk attributes (triplets array)
68-
skipAttributes(unpacker, stringTable);
68+
SpanV1.skipAttributes(unpacker, stringTable);
6969
break;
7070
case 4:
7171
spans = SpanV1.unpackSpans(unpacker, stringTable, traceId);
@@ -131,34 +131,6 @@ private static DecodedSpan[] withChunkFields(
131131
return updated;
132132
}
133133

134-
private static void skipAttributes(MessageUnpacker unpacker, List<String> stringTable)
135-
throws IOException {
136-
int arraySize = unpacker.unpackArrayHeader();
137-
if (arraySize % 3 != 0) {
138-
throw new IllegalArgumentException(
139-
"Attributes array size must be divisible by 3, got: " + arraySize);
140-
}
141-
int tripletCount = arraySize / 3;
142-
for (int i = 0; i < tripletCount; i++) {
143-
SpanV1.unpackStreamingString(unpacker, stringTable);
144-
int valueType = unpacker.unpackInt();
145-
switch (valueType) {
146-
case SpanV1.STRING_VALUE_TYPE:
147-
SpanV1.unpackStreamingString(unpacker, stringTable);
148-
break;
149-
case SpanV1.BOOL_VALUE_TYPE:
150-
unpacker.unpackBoolean();
151-
break;
152-
case SpanV1.FLOAT_VALUE_TYPE:
153-
unpacker.unpackDouble();
154-
break;
155-
default:
156-
unpacker.skipValue();
157-
break;
158-
}
159-
}
160-
}
161-
162134
private final DecodedSpan[] spans;
163135
private final Integer chunkSamplingPriority;
164136

0 commit comments

Comments
 (0)