From 725ea560e2756deb32eb8c0afd2d72f05e9eb82e Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 19 Sep 2024 13:24:33 -0700 Subject: [PATCH] Test the SDK's Span.SpanContext method Part of #954 --- sdk/trace_test.go | 59 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/sdk/trace_test.go b/sdk/trace_test.go index 91a9636e5..15b787d7d 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" @@ -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) { @@ -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 +}