| 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 | "context" |
| 9 | "encoding/json" |
| 10 | "io" |
| 11 | "net/http" |
| 12 | "net/http/httptest" |
| 13 | "sync" |
| 14 | "testing" |
| 15 | |
| 16 | "golang.org/x/tools/internal/event" |
| 17 | "golang.org/x/tools/internal/event/core" |
| 18 | "golang.org/x/tools/internal/event/export" |
| 19 | "golang.org/x/tools/internal/event/label" |
| 20 | ) |
| 21 | |
| 22 | func TestConvertSpan(t *testing.T) { |
| 23 | span := &export.Span{ |
| 24 | Name: "test-operation", |
| 25 | ID: export.SpanContext{ |
| 26 | TraceID: export.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, |
| 27 | SpanID: export.SpanID{1, 2, 3, 4, 5, 6, 7, 8}, |
| 28 | }, |
| 29 | ParentID: export.SpanID{8, 7, 6, 5, 4, 3, 2, 1}, |
| 30 | } |
| 31 | |
| 32 | result := convertSpan(span) |
| 33 | |
| 34 | if result.Name != "test-operation" { |
| 35 | t.Errorf("expected name 'test-operation', got %q", result.Name) |
| 36 | } |
| 37 | if result.TraceID != "0102030405060708090a0b0c0d0e0f10" { |
| 38 | t.Errorf("unexpected traceId: %s", result.TraceID) |
| 39 | } |
| 40 | if result.SpanID != "0102030405060708" { |
| 41 | t.Errorf("unexpected spanId: %s", result.SpanID) |
| 42 | } |
| 43 | if result.ParentSpanID != "0807060504030201" { |
| 44 | t.Errorf("unexpected parentSpanId: %s", result.ParentSpanID) |
| 45 | } |
| 46 | if result.Kind != 1 { |
| 47 | t.Errorf("expected kind 1, got %d", result.Kind) |
| 48 | } |
| 49 | if result.Status.Code != 0 { |
| 50 | t.Errorf("expected status code 0, got %d", result.Status.Code) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // TestE2E_SpanExport is a minimal end-to-end test verifying the full pipeline. |
| 55 | func TestE2E_SpanExport(t *testing.T) { |
| 56 | var received []byte |
| 57 | var mu sync.Mutex |
| 58 | |
| 59 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 60 | mu.Lock() |
| 61 | received, _ = io.ReadAll(r.Body) |
| 62 | mu.Unlock() |
| 63 | w.WriteHeader(http.StatusOK) |
| 64 | })) |
| 65 | defer server.Close() |
| 66 | |
| 67 | baseExporter := func(ctx context.Context, ev core.Event, lm label.Map) context.Context { |
| 68 | return ctx |
| 69 | } |
| 70 | spansExporter := export.Spans(baseExporter) |
| 71 | otelExporter := NewExporter(context.Background(), WithEndpoint(server.URL)) |
| 72 | |
| 73 | chainedExporter := func(ctx context.Context, ev core.Event, lm label.Map) context.Context { |
| 74 | ctx = spansExporter(ctx, ev, lm) |
| 75 | return otelExporter.ProcessEvent(ctx, ev, lm) |
| 76 | } |
| 77 | |
| 78 | event.SetExporter(chainedExporter) |
| 79 | |
| 80 | ctx := context.Background() |
| 81 | ctx, end := event.Start(ctx, "e2e-span") |
| 82 | end() |
| 83 | |
| 84 | otelExporter.Flush() |
| 85 | |
| 86 | mu.Lock() |
| 87 | data := received |
| 88 | mu.Unlock() |
| 89 | |
| 90 | if len(data) == 0 { |
| 91 | t.Fatal("no data received") |
| 92 | } |
| 93 | |
| 94 | // Just verify it's valid JSON with a span |
| 95 | var req otlpTraceExportRequest |
| 96 | if err := json.Unmarshal(data, &req); err != nil { |
| 97 | t.Fatalf("invalid JSON: %v", err) |
| 98 | } |
| 99 | if len(req.ResourceSpans) == 0 || |
| 100 | len(req.ResourceSpans[0].ScopeSpans) == 0 || |
| 101 | len(req.ResourceSpans[0].ScopeSpans[0].Spans) == 0 { |
| 102 | t.Fatal("expected at least one span") |
| 103 | } |
| 104 | } |