|
| 1 | +// Copyright 2026 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package telemetry |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + sdktrace "go.opentelemetry.io/otel/sdk/trace" |
| 24 | + "go.opentelemetry.io/otel/sdk/trace/tracetest" |
| 25 | + "go.opentelemetry.io/otel/trace" |
| 26 | +) |
| 27 | + |
| 28 | +// newTestTracer creates a TracerProvider backed by an in-memory exporter. |
| 29 | +func newTestTracer(t *testing.T) (trace.Tracer, *sdktrace.TracerProvider, *tracetest.InMemoryExporter) { |
| 30 | + t.Helper() |
| 31 | + exporter := tracetest.NewInMemoryExporter() |
| 32 | + tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter)) |
| 33 | + t.Cleanup(func() { _ = tp.Shutdown(context.Background()) }) |
| 34 | + return tp.Tracer("test"), tp, exporter |
| 35 | +} |
| 36 | + |
| 37 | +func TestStartSpanWithTracer_NoParent(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + tracer, tp, exporter := newTestTracer(t) |
| 40 | + |
| 41 | + // Create a span without a parent. |
| 42 | + ctx := context.Background() |
| 43 | + retCtx, _, endSpan := StartSpanWithTracer(ctx, tracer, "should-not-exist") |
| 44 | + endSpan(nil) |
| 45 | + |
| 46 | + // The returned context should be the original context (unchanged). |
| 47 | + if retCtx != ctx { |
| 48 | + t.Fatal("expected original context to be returned unchanged") |
| 49 | + } |
| 50 | + |
| 51 | + // Flush and verify no spans were recorded. |
| 52 | + if err := tp.ForceFlush(context.Background()); err != nil { |
| 53 | + t.Fatalf("failed to flush: %v", err) |
| 54 | + } |
| 55 | + spans := exporter.GetSpans() |
| 56 | + if len(spans) != 0 { |
| 57 | + t.Fatalf("expected no spans, got %d", len(spans)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestStartSpanWithTracer_WithParent(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + tracer, tp, exporter := newTestTracer(t) |
| 64 | + |
| 65 | + // Create a parent span to establish a valid span context. |
| 66 | + ctx, parentSpan := tracer.Start(context.Background(), "parent") |
| 67 | + defer parentSpan.End() |
| 68 | + |
| 69 | + // Should create a real child span. |
| 70 | + _, _, endSpan := StartSpanWithTracer(ctx, tracer, "child") |
| 71 | + endSpan(nil) |
| 72 | + |
| 73 | + // Flush and verify the child span was recorded. |
| 74 | + parentSpan.End() |
| 75 | + if err := tp.ForceFlush(context.Background()); err != nil { |
| 76 | + t.Fatalf("failed to flush: %v", err) |
| 77 | + } |
| 78 | + spans := exporter.GetSpans() |
| 79 | + var childSpan *tracetest.SpanStub |
| 80 | + for i := range spans { |
| 81 | + if spans[i].Name == "child" { |
| 82 | + childSpan = &spans[i] |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + if childSpan == nil { |
| 87 | + t.Fatal("child span not found") |
| 88 | + } |
| 89 | + |
| 90 | + // Verify it is parented to the correct trace. |
| 91 | + if childSpan.Parent.TraceID() != parentSpan.SpanContext().TraceID() { |
| 92 | + t.Errorf("trace ID mismatch: got %s, want %s", |
| 93 | + childSpan.Parent.TraceID(), parentSpan.SpanContext().TraceID()) |
| 94 | + } |
| 95 | + if childSpan.SpanKind != trace.SpanKindInternal { |
| 96 | + t.Errorf("expected SpanKindInternal, got %v", childSpan.SpanKind) |
| 97 | + } |
| 98 | +} |
0 commit comments