From e538e1c30f4a12919f8011174c47f571951e7aaf Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 19 Sep 2024 12:47:22 -0700 Subject: [PATCH] Implement the Span.SetName method Part of #954 --- sdk/trace.go | 2 +- sdk/trace_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/sdk/trace.go b/sdk/trace.go index b011fae2a..212ff4a6c 100644 --- a/sdk/trace.go +++ b/sdk/trace.go @@ -160,7 +160,7 @@ func (s *span) SetName(name string) { if s == nil || !s.sampled { return } - /* TODO: implement */ + s.span.SetName(name) } func (*span) TracerProvider() trace.TracerProvider { diff --git a/sdk/trace_test.go b/sdk/trace_test.go index 91a9636e5..bd3693aa4 100644 --- a/sdk/trace_test.go +++ b/sdk/trace_test.go @@ -4,6 +4,7 @@ package sdk import ( + "context" "testing" "github.com/stretchr/testify/assert" @@ -47,3 +48,38 @@ func TestSpanNilUnsampledGuards(t *testing.T) { t.Run("SetAttributes", run(func(s *span) { s.SetAttributes(attrs...) })) t.Run("TracerProvider", run(func(s *span) { _ = s.TracerProvider() })) } + +func TestSpanSetName(t *testing.T) { + const name = "span name" + builder := spanBuilder{} + + s := builder.Build() + s.SetName(name) + assert.Equal(t, name, s.span.Name(), "span name not set") + + builder.Name = "alt" + s = builder.Build() + s.SetName(name) + assert.Equal(t, name, s.span.Name(), "SetName overrides default") +} + +type spanBuilder struct { + Name string + NotSampled bool + SpanContext trace.SpanContext + Options []trace.SpanStartOption +} + +func (b spanBuilder) Build() *span { + tracer := new(tracer) + s := &span{sampled: !b.NotSampled, spanContext: b.SpanContext} + s.traces, s.span = tracer.traces( + context.Background(), + b.Name, + trace.NewSpanStartConfig(b.Options...), + s.spanContext, + trace.SpanContext{}, + ) + + return s +}