|
|
|
@ -1,10 +1,13 @@ |
|
|
|
|
package tracing |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
|
|
"go.opentelemetry.io/otel/attribute" |
|
|
|
|
"go.opentelemetry.io/otel/trace" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestInitSampler(t *testing.T) { |
|
|
|
@ -41,3 +44,36 @@ func TestInitSampler(t *testing.T) { |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
assert.Equal(t, "RateLimitingSampler{100.25}", sampler.Description()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestStart(t *testing.T) { |
|
|
|
|
name := "test-span" |
|
|
|
|
attributes := []attribute.KeyValue{ |
|
|
|
|
attribute.String("test1", "1"), |
|
|
|
|
attribute.Int("test2", 2), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
t.Run("should return noop span if there is not currently a span in context", func(t *testing.T) { |
|
|
|
|
ctx := context.Background() |
|
|
|
|
_, span := Start(ctx, name, attributes...) |
|
|
|
|
defer span.End() |
|
|
|
|
|
|
|
|
|
require.NotNil(t, span) |
|
|
|
|
require.False(t, span.SpanContext().IsValid()) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
t.Run("should return a span with a valid span context if there is currently a span in context", func(t *testing.T) { |
|
|
|
|
spanCtx := trace.NewSpanContext(trace.SpanContextConfig{ |
|
|
|
|
TraceID: trace.TraceID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, |
|
|
|
|
SpanID: trace.SpanID{1, 2, 3, 4, 5, 6, 7, 8}, |
|
|
|
|
TraceFlags: trace.FlagsSampled, |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
ctx := trace.ContextWithSpanContext(context.Background(), spanCtx) |
|
|
|
|
_, childSpan := Start(ctx, name, attributes...) |
|
|
|
|
defer childSpan.End() |
|
|
|
|
|
|
|
|
|
require.NotNil(t, childSpan) |
|
|
|
|
require.Equal(t, spanCtx.TraceID(), childSpan.SpanContext().TraceID()) |
|
|
|
|
require.True(t, childSpan.SpanContext().IsValid()) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|