Skip to content

Commit

Permalink
Test the SDK's Span.SpanContext method
Browse files Browse the repository at this point in the history
Part of #954
  • Loading branch information
MrAlias committed Sep 19, 2024
1 parent effdec9 commit 725ea56
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 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 All @@ -12,18 +13,26 @@ import (
"go.opentelemetry.io/otel/trace"
)

var attrs = []attribute.KeyValue{
attribute.Bool("bool", true),
attribute.Int("int", -1),
attribute.Int64("int64", 43),
attribute.Float64("float64", 0.3),
attribute.String("string", "value"),
attribute.BoolSlice("bool slice", []bool{true, false, true}),
attribute.IntSlice("int slice", []int{-1, -30, 328}),
attribute.Int64Slice("int64 slice", []int64{1030, 0, 0}),
attribute.Float64Slice("float64 slice", []float64{1e9}),
attribute.StringSlice("string slice", []string{"one", "two"}),
}
var (
attrs = []attribute.KeyValue{
attribute.Bool("bool", true),
attribute.Int("int", -1),
attribute.Int64("int64", 43),
attribute.Float64("float64", 0.3),
attribute.String("string", "value"),
attribute.BoolSlice("bool slice", []bool{true, false, true}),
attribute.IntSlice("int slice", []int{-1, -30, 328}),
attribute.Int64Slice("int64 slice", []int64{1030, 0, 0}),
attribute.Float64Slice("float64 slice", []float64{1e9}),
attribute.StringSlice("string slice", []string{"one", "two"}),
}

spanContext0 = trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{0x1},
SpanID: trace.SpanID{0x1},
TraceFlags: trace.FlagsSampled,
})
)

func TestSpanNilUnsampledGuards(t *testing.T) {
run := func(fn func(s *span)) func(*testing.T) {
Expand All @@ -47,3 +56,29 @@ 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 TestSpanSpanContext(t *testing.T) {
s := spanBuilder{SpanContext: spanContext0}.Build()
assert.Equal(t, spanContext0, s.SpanContext())
}

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 725ea56

Please sign in to comment.