Skip to content

Commit

Permalink
Implement the Span.SetName method
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Sep 19, 2024
1 parent effdec9 commit e538e1c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
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")
}

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
}

0 comments on commit e538e1c

Please sign in to comment.