|
| 1 | +interface OtlpAttribute { |
| 2 | + key: string |
| 3 | + value: OtlpValue |
| 4 | +} |
| 5 | + |
| 6 | +interface OtlpValue { |
| 7 | + stringValue?: string |
| 8 | + intValue?: number |
| 9 | + doubleValue?: number |
| 10 | + boolValue?: boolean |
| 11 | + arrayValue?: { values?: OtlpValue[] } |
| 12 | + kvlistValue?: { values?: { key: string; value: OtlpValue }[] } |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Deserializes OTLP trace data from a JSON string and transforms it into a cleaner JSON structure. |
| 17 | + * This extracts the key information from the OTLP format (resource spans, scope spans, etc.) |
| 18 | + * and returns a more readable representation suitable for test snapshots. |
| 19 | + */ |
| 20 | +export function deserializeRequest(data: string) { |
| 21 | + const otlpData = JSON.parse(data) |
| 22 | + |
| 23 | + // Transform OTLP format into cleaner JSON |
| 24 | + const spans = [] |
| 25 | + |
| 26 | + for (const resourceSpan of otlpData.resourceSpans || []) { |
| 27 | + const resource = resourceSpan.resource?.attributes || [] |
| 28 | + |
| 29 | + for (const scopeSpan of resourceSpan.scopeSpans || []) { |
| 30 | + const scope = scopeSpan.scope?.name |
| 31 | + |
| 32 | + for (const span of scopeSpan.spans || []) { |
| 33 | + // Convert attributes array to object |
| 34 | + const attributes: Record<string, unknown> = {} |
| 35 | + for (const attr of span.attributes || []) { |
| 36 | + attributes[attr.key] = extractOtlpValue(attr.value) |
| 37 | + } |
| 38 | + |
| 39 | + spans.push({ |
| 40 | + name: span.name, |
| 41 | + parentSpanId: span.parentSpanId, |
| 42 | + kind: span.kind, |
| 43 | + attributes, |
| 44 | + status: span.status, |
| 45 | + events: span.events, |
| 46 | + links: span.links, |
| 47 | + resource: Object.fromEntries( |
| 48 | + resource.map((attr: OtlpAttribute) => [ |
| 49 | + attr.key, |
| 50 | + attr.value.stringValue ?? |
| 51 | + attr.value.intValue ?? |
| 52 | + attr.value.doubleValue ?? |
| 53 | + attr.value.boolValue ?? |
| 54 | + attr.value, |
| 55 | + ]), |
| 56 | + ), |
| 57 | + scope, |
| 58 | + }) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return spans |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Recursively extracts the actual value from an OTLP AnyValue structure. |
| 68 | + * Handles all OTLP value types including nested structures. |
| 69 | + */ |
| 70 | +function extractOtlpValue(value: OtlpValue): unknown { |
| 71 | + if (value.stringValue !== undefined) return value.stringValue |
| 72 | + if (value.intValue !== undefined) return value.intValue |
| 73 | + if (value.doubleValue !== undefined) return value.doubleValue |
| 74 | + if (value.boolValue !== undefined) return value.boolValue |
| 75 | + |
| 76 | + if (value.arrayValue) { |
| 77 | + // For arrays, just extract the values directly without wrapping |
| 78 | + return value.arrayValue.values?.map(extractOtlpValue) || [] |
| 79 | + } |
| 80 | + |
| 81 | + if (value.kvlistValue) { |
| 82 | + // For key-value lists, extract as an object |
| 83 | + const obj: Record<string, unknown> = {} |
| 84 | + for (const kv of value.kvlistValue.values || []) { |
| 85 | + obj[kv.key] = extractOtlpValue(kv.value) |
| 86 | + } |
| 87 | + return obj |
| 88 | + } |
| 89 | + |
| 90 | + // For empty objects or unknown types, return the raw value |
| 91 | + return value |
| 92 | +} |
0 commit comments