|
| 1 | +package datadog.trace.test.agent.decoder.json.raw; |
| 2 | + |
| 3 | +import static java.util.Collections.unmodifiableList; |
| 4 | + |
| 5 | +import com.squareup.moshi.FromJson; |
| 6 | +import com.squareup.moshi.JsonAdapter; |
| 7 | +import com.squareup.moshi.JsonDataException; |
| 8 | +import com.squareup.moshi.JsonReader; |
| 9 | +import com.squareup.moshi.JsonWriter; |
| 10 | +import com.squareup.moshi.Moshi; |
| 11 | +import com.squareup.moshi.ToJson; |
| 12 | +import com.squareup.moshi.Types; |
| 13 | +import datadog.trace.test.agent.decoder.DecodedMessage; |
| 14 | +import datadog.trace.test.agent.decoder.DecodedSpan; |
| 15 | +import datadog.trace.test.agent.decoder.DecodedTrace; |
| 16 | +import java.io.IOException; |
| 17 | +import java.lang.reflect.Type; |
| 18 | +import java.math.BigInteger; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +/** |
| 25 | + * MessageJson decodes a JSON trace payload — a JSON array of traces, each a JSON array of spans in |
| 26 | + * the v0.4 shape, as exposed by the dd-apm-test-agent — into the shared {@link DecodedMessage} |
| 27 | + * model. Unlike the msgpack formats there is no message envelope, so the payload maps directly to |
| 28 | + * the list of traces. |
| 29 | + */ |
| 30 | +public final class MessageJson implements DecodedMessage { |
| 31 | + private static final Type LIST_OF_TRACES = |
| 32 | + Types.newParameterizedType( |
| 33 | + List.class, Types.newParameterizedType(List.class, SpanJson.class)); |
| 34 | + private static final JsonAdapter<List<List<SpanJson>>> ADAPTER = |
| 35 | + new Moshi.Builder() |
| 36 | + .add(new MetricNumberAdapter()) |
| 37 | + .add(new MetaStructObjectAdapter()) |
| 38 | + .build() |
| 39 | + .adapter(LIST_OF_TRACES); |
| 40 | + |
| 41 | + private final List<DecodedTrace> traces; |
| 42 | + |
| 43 | + private MessageJson(List<DecodedTrace> traces) { |
| 44 | + this.traces = unmodifiableList(traces); |
| 45 | + } |
| 46 | + |
| 47 | + /** Decodes a JSON trace payload into a {@link MessageJson}. */ |
| 48 | + public static MessageJson fromJson(String json) { |
| 49 | + List<List<SpanJson>> rawTraces; |
| 50 | + try { |
| 51 | + rawTraces = ADAPTER.fromJson(json); |
| 52 | + } catch (IOException | JsonDataException e) { |
| 53 | + throw new IllegalStateException("Failed to parse JSON traces: " + json, e); |
| 54 | + } |
| 55 | + List<DecodedTrace> traces = new ArrayList<>(); |
| 56 | + if (rawTraces != null) { |
| 57 | + for (List<SpanJson> spans : rawTraces) { |
| 58 | + if (spans == null) { |
| 59 | + throw new IllegalStateException("Malformed JSON trace with a null trace: " + json); |
| 60 | + } |
| 61 | + List<DecodedSpan> decodedSpans = new ArrayList<>(spans.size()); |
| 62 | + for (SpanJson span : spans) { |
| 63 | + if (span == null) { |
| 64 | + throw new IllegalStateException("Malformed JSON trace with a null span: " + json); |
| 65 | + } |
| 66 | + if (span.name == null |
| 67 | + || span.traceId == null |
| 68 | + || span.spanId == null |
| 69 | + || span.start == null |
| 70 | + || span.duration == null) { |
| 71 | + throw new IllegalStateException( |
| 72 | + "JSON span missing a required v0.4 field " |
| 73 | + + "(name, trace_id, span_id, start, duration): " |
| 74 | + + span); |
| 75 | + } |
| 76 | + decodedSpans.add(span); |
| 77 | + } |
| 78 | + traces.add(new TraceJson(decodedSpans)); |
| 79 | + } |
| 80 | + } |
| 81 | + return new MessageJson(traces); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Decodes the numeric values of the {@code metrics} map. Moshi coerces every JSON number to |
| 86 | + * {@code Double}; this adapter instead reads the number from its literal form and preserves the |
| 87 | + * integral vs. fractional distinction the msgpack decoders produce (see {@code |
| 88 | + * SpanV04.unpackNumber}): integral values become {@code Integer} (or {@code Long} when they |
| 89 | + * overflow {@code int}, or {@code BigInteger} beyond {@code long} range), fractional values |
| 90 | + * become {@code Double}. Reading the literal rather than coercing through {@code double} keeps |
| 91 | + * every integral value exact — including those above 2^53 and above {@code Long.MAX_VALUE}. |
| 92 | + */ |
| 93 | + static final class MetricNumberAdapter { |
| 94 | + @FromJson |
| 95 | + Number fromJson(JsonReader reader) throws IOException { |
| 96 | + String literal = reader.nextString(); |
| 97 | + boolean fractional = |
| 98 | + literal.indexOf('.') >= 0 || literal.indexOf('e') >= 0 || literal.indexOf('E') >= 0; |
| 99 | + if (!fractional) { |
| 100 | + try { |
| 101 | + return Integer.valueOf(literal); |
| 102 | + } catch (NumberFormatException overflowsInt) { |
| 103 | + try { |
| 104 | + return Long.valueOf(literal); |
| 105 | + } catch (NumberFormatException overflowsLong) { |
| 106 | + // Integral magnitude beyond long range: keep it exact as a BigInteger. |
| 107 | + return new BigInteger(literal); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return Double.valueOf(literal); |
| 112 | + } |
| 113 | + |
| 114 | + @ToJson |
| 115 | + void toJson(JsonWriter writer, Number value) throws IOException { |
| 116 | + writer.value(value); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Decodes the arbitrarily-nested values of the {@code meta_struct} map. Moshi's generic {@code |
| 122 | + * Object} adapter materializes every JSON number as {@code Double}, every object as a map, and |
| 123 | + * every array as a list; this adapter instead mirrors the msgpack decoder (see {@code |
| 124 | + * SpanV04.convertValueToObject}) so both backends expose the same types for a nested leaf: |
| 125 | + * |
| 126 | + * <ul> |
| 127 | + * <li>integers become {@code Long} (reading the literal keeps values above 2^53 exact, and |
| 128 | + * values beyond {@code long} range become {@code BigInteger} rather than a lossy {@code |
| 129 | + * Double}), |
| 130 | + * <li>floating-point numbers become {@code Float}, |
| 131 | + * <li>objects become {@code Map<String, Object>} and arrays {@code List<Object>}, recursively, |
| 132 | + * <li>strings, booleans, and JSON {@code null} are preserved as {@code String}/{@code |
| 133 | + * Boolean}/{@code null}. |
| 134 | + * </ul> |
| 135 | + * |
| 136 | + * <p>Keeping the representations identical means a {@code metaStruct(...)} assertion behaves the |
| 137 | + * same whether the span came from the in-process msgpack backend or the dd-apm-test-agent JSON |
| 138 | + * backend. Binary leaves ({@code byte[]} on the msgpack side) have no JSON representation — the |
| 139 | + * agent fails to serialize them (HTTP 400) so they never reach this decoder; in practice {@code |
| 140 | + * meta_struct} carries only JSON-serializable data. |
| 141 | + */ |
| 142 | + static final class MetaStructObjectAdapter { |
| 143 | + @FromJson |
| 144 | + Object fromJson(JsonReader reader) throws IOException { |
| 145 | + switch (reader.peek()) { |
| 146 | + case BEGIN_OBJECT: |
| 147 | + Map<String, Object> object = new LinkedHashMap<>(); |
| 148 | + reader.beginObject(); |
| 149 | + while (reader.hasNext()) { |
| 150 | + object.put(reader.nextName(), fromJson(reader)); |
| 151 | + } |
| 152 | + reader.endObject(); |
| 153 | + return object; |
| 154 | + case BEGIN_ARRAY: |
| 155 | + List<Object> array = new ArrayList<>(); |
| 156 | + reader.beginArray(); |
| 157 | + while (reader.hasNext()) { |
| 158 | + array.add(fromJson(reader)); |
| 159 | + } |
| 160 | + reader.endArray(); |
| 161 | + return array; |
| 162 | + case STRING: |
| 163 | + return reader.nextString(); |
| 164 | + case NUMBER: |
| 165 | + return parseNumber(reader.nextString()); |
| 166 | + case BOOLEAN: |
| 167 | + return reader.nextBoolean(); |
| 168 | + case NULL: |
| 169 | + return reader.nextNull(); |
| 170 | + default: |
| 171 | + throw new JsonDataException("Unexpected meta_struct token: " + reader.peek()); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private static Number parseNumber(String literal) { |
| 176 | + boolean fractional = |
| 177 | + literal.indexOf('.') >= 0 || literal.indexOf('e') >= 0 || literal.indexOf('E') >= 0; |
| 178 | + if (fractional) { |
| 179 | + return Float.valueOf(literal); |
| 180 | + } |
| 181 | + try { |
| 182 | + return Long.valueOf(literal); |
| 183 | + } catch (NumberFormatException overflowsLong) { |
| 184 | + // Integral magnitude beyond long range: keep it exact as a BigInteger. |
| 185 | + return new BigInteger(literal); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public List<DecodedTrace> getTraces() { |
| 192 | + return this.traces; |
| 193 | + } |
| 194 | +} |
0 commit comments