|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { SpanData, TraceData } from '@genkit-ai/tools-common'; |
| 18 | + |
| 19 | +// These interfaces are based on the OTLP JSON format. |
| 20 | +// A full definition can be found at: |
| 21 | +// https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto |
| 22 | + |
| 23 | +interface OtlpValue { |
| 24 | + stringValue?: string; |
| 25 | + intValue?: number; |
| 26 | + boolValue?: boolean; |
| 27 | + arrayValue?: { |
| 28 | + values: OtlpValue[]; |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +interface OtlpAttribute { |
| 33 | + key: string; |
| 34 | + value: OtlpValue; |
| 35 | +} |
| 36 | + |
| 37 | +interface OtlpSpan { |
| 38 | + traceId: string; |
| 39 | + spanId: string; |
| 40 | + parentSpanId?: string; |
| 41 | + name: string; |
| 42 | + kind: number; |
| 43 | + startTimeUnixNano: string; |
| 44 | + endTimeUnixNano: string; |
| 45 | + attributes: OtlpAttribute[]; |
| 46 | + droppedAttributesCount: number; |
| 47 | + events: any[]; |
| 48 | + droppedEventsCount: number; |
| 49 | + status?: { |
| 50 | + code: number; |
| 51 | + message?: string; |
| 52 | + }; |
| 53 | + links: any[]; |
| 54 | + droppedLinksCount: number; |
| 55 | +} |
| 56 | + |
| 57 | +interface OtlpScopeSpan { |
| 58 | + scope: { |
| 59 | + name: string; |
| 60 | + version: string; |
| 61 | + }; |
| 62 | + spans: OtlpSpan[]; |
| 63 | +} |
| 64 | + |
| 65 | +interface OtlpResourceSpan { |
| 66 | + resource: { |
| 67 | + attributes: OtlpAttribute[]; |
| 68 | + droppedAttributesCount: number; |
| 69 | + }; |
| 70 | + scopeSpans: OtlpScopeSpan[]; |
| 71 | +} |
| 72 | + |
| 73 | +interface OtlpPayload { |
| 74 | + resourceSpans: OtlpResourceSpan[]; |
| 75 | +} |
| 76 | + |
| 77 | +function toMillis(nano: string): number { |
| 78 | + return Math.round(parseInt(nano) / 1_000_000); |
| 79 | +} |
| 80 | + |
| 81 | +function toSpanData(span: OtlpSpan, scope: OtlpScopeSpan['scope']): SpanData { |
| 82 | + const attributes: Record<string, any> = {}; |
| 83 | + span.attributes.forEach((attr) => { |
| 84 | + if (attr.value.stringValue) { |
| 85 | + attributes[attr.key] = attr.value.stringValue; |
| 86 | + } else if (attr.value.intValue) { |
| 87 | + attributes[attr.key] = attr.value.intValue; |
| 88 | + } else if (attr.value.boolValue) { |
| 89 | + attributes[attr.key] = attr.value.boolValue; |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + let spanKind: string; |
| 94 | + switch (span.kind) { |
| 95 | + case 1: |
| 96 | + spanKind = 'INTERNAL'; |
| 97 | + break; |
| 98 | + case 2: |
| 99 | + spanKind = 'SERVER'; |
| 100 | + break; |
| 101 | + case 3: |
| 102 | + spanKind = 'CLIENT'; |
| 103 | + break; |
| 104 | + case 4: |
| 105 | + spanKind = 'PRODUCER'; |
| 106 | + break; |
| 107 | + case 5: |
| 108 | + spanKind = 'CONSUMER'; |
| 109 | + break; |
| 110 | + default: |
| 111 | + spanKind = 'UNSPECIFIED'; |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + const spanData: SpanData = { |
| 116 | + traceId: span.traceId, |
| 117 | + spanId: span.spanId, |
| 118 | + parentSpanId: span.parentSpanId, |
| 119 | + startTime: toMillis(span.startTimeUnixNano), |
| 120 | + endTime: toMillis(span.endTimeUnixNano), |
| 121 | + displayName: span.name, |
| 122 | + attributes, |
| 123 | + instrumentationLibrary: { |
| 124 | + name: scope.name, |
| 125 | + version: scope.version, |
| 126 | + }, |
| 127 | + spanKind, |
| 128 | + }; |
| 129 | + if (span.status && span.status.code !== 0) { |
| 130 | + const status: { code: number; message?: string } = { |
| 131 | + code: span.status.code, |
| 132 | + }; |
| 133 | + if (span.status.message) { |
| 134 | + status.message = span.status.message; |
| 135 | + } |
| 136 | + spanData.status = status; |
| 137 | + } |
| 138 | + return spanData; |
| 139 | +} |
| 140 | + |
| 141 | +export function traceDataFromOtlp(otlpData: OtlpPayload): TraceData[] { |
| 142 | + const traces: Record<string, TraceData> = {}; |
| 143 | + |
| 144 | + otlpData.resourceSpans.forEach((resourceSpan) => { |
| 145 | + resourceSpan.scopeSpans.forEach((scopeSpan) => { |
| 146 | + scopeSpan.spans.forEach((span) => { |
| 147 | + if (!traces[span.traceId]) { |
| 148 | + traces[span.traceId] = { |
| 149 | + traceId: span.traceId, |
| 150 | + spans: {}, |
| 151 | + }; |
| 152 | + } |
| 153 | + traces[span.traceId].spans[span.spanId] = toSpanData( |
| 154 | + span, |
| 155 | + scopeSpan.scope |
| 156 | + ); |
| 157 | + }); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + return Object.values(traces); |
| 162 | +} |
0 commit comments