| 1 | // Copyright 2026 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package otel |
| 6 | |
| 7 | import ( |
| 8 | "slices" |
| 9 | |
| 10 | "golang.org/x/tools/internal/event" |
| 11 | "golang.org/x/tools/internal/event/core" |
| 12 | "golang.org/x/tools/internal/event/export" |
| 13 | "golang.org/x/tools/internal/event/keys" |
| 14 | ) |
| 15 | |
| 16 | // exportTraces sends traces to the OTLP endpoint. |
| 17 | func (e *Exporter) exportTraces(spans []otlpSpan) error { |
| 18 | req := otlpTraceExportRequest{ |
| 19 | ResourceSpans: []otlpResourceSpans{{ |
| 20 | Resource: e.resource, |
| 21 | ScopeSpans: []otlpScopeSpans{{ |
| 22 | Scope: otlpScope{Name: "golang.org/x/tools"}, |
| 23 | Spans: spans, |
| 24 | }}, |
| 25 | }}, |
| 26 | } |
| 27 | return e.post("/v1/traces", req) |
| 28 | } |
| 29 | |
| 30 | // convertSpan converts an internal Span to an OTLP span. |
| 31 | func convertSpan(span *export.Span) otlpSpan { |
| 32 | s := otlpSpan{ |
| 33 | TraceID: span.ID.TraceID.String(), |
| 34 | SpanID: span.ID.SpanID.String(), |
| 35 | Name: span.Name, |
| 36 | Kind: 1, // SPAN_KIND_INTERNAL |
| 37 | Status: otlpSpanStatus{Code: 0}, // STATUS_CODE_UNSET |
| 38 | } |
| 39 | |
| 40 | if span.ParentID.IsValid() { |
| 41 | s.ParentSpanID = span.ParentID.String() |
| 42 | } |
| 43 | |
| 44 | // Get timestamps from start event |
| 45 | startEvent := span.Start() |
| 46 | if !startEvent.At().IsZero() { |
| 47 | s.StartTimeUnixNano = intAsString(startEvent.At().UnixNano()) |
| 48 | } |
| 49 | |
| 50 | // Get end timestamp from finish event |
| 51 | finishEvent := span.Finish() |
| 52 | if !finishEvent.At().IsZero() { |
| 53 | s.EndTimeUnixNano = intAsString(finishEvent.At().UnixNano()) |
| 54 | } |
| 55 | |
| 56 | // Check for errors in span events to set status |
| 57 | if slices.ContainsFunc(span.Events(), event.IsError) { |
| 58 | s.Status.Code = 2 // STATUS_CODE_ERROR |
| 59 | } |
| 60 | |
| 61 | // Extract attributes from span events |
| 62 | s.Attributes = extractAttributes(span) |
| 63 | |
| 64 | return s |
| 65 | } |
| 66 | |
| 67 | // extractAttributes extracts labels from span events as OTLP attributes. |
| 68 | func extractAttributes(span *export.Span) []otlpAttribute { |
| 69 | var attrs []otlpAttribute |
| 70 | |
| 71 | // Extract from start event |
| 72 | startEvent := span.Start() |
| 73 | attrs = appendEventAttributes(attrs, startEvent) |
| 74 | |
| 75 | // Extract from logged events |
| 76 | for _, ev := range span.Events() { |
| 77 | attrs = appendEventAttributes(attrs, ev) |
| 78 | } |
| 79 | |
| 80 | return attrs |
| 81 | } |
| 82 | |
| 83 | // appendEventAttributes appends labels from an event as attributes. |
| 84 | func appendEventAttributes(attrs []otlpAttribute, ev core.Event) []otlpAttribute { |
| 85 | for l := range ev.Labels() { |
| 86 | key := l.Key() |
| 87 | // Skip internal event type markers |
| 88 | if key == keys.Start || key == keys.End || key == keys.Label || |
| 89 | key == keys.Detach || key == keys.Metric { |
| 90 | continue |
| 91 | } |
| 92 | |
| 93 | if attr, ok := labelToAttribute(l); ok { |
| 94 | attrs = append(attrs, attr) |
| 95 | } |
| 96 | } |
| 97 | return attrs |
| 98 | } |