Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Span.SetName method #1110

Merged
merged 9 commits into from
Sep 27, 2024
2 changes: 1 addition & 1 deletion sdk/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions sdk/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package sdk

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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")
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}

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
}
Loading