Skip to content

Commit a4b3898

Browse files
authored
internal/telemetry: don't create internal spans without parents (#33780)
This prevents orphaned spans from appearing in traces.
1 parent 0cba803 commit a4b3898

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

internal/telemetry/telemetry.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ func StartSpan(ctx context.Context, spanName string, attributes ...Attribute) (c
5252

5353
// StartSpanWithTracer requires a tracer to be passed in and creates a SpanKind=INTERNAL span.
5454
func StartSpanWithTracer(ctx context.Context, tracer trace.Tracer, name string, attributes ...Attribute) (context.Context, trace.Span, func(*error)) {
55+
// Don't create a span if there's no parent span in the context.
56+
parent := trace.SpanFromContext(ctx)
57+
if !parent.SpanContext().IsValid() {
58+
return ctx, parent, func(*error) {}
59+
}
5560
return startSpan(ctx, tracer, trace.SpanKindInternal, name, attributes...)
5661
}
5762

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)