Debian Codegraph @

trace/noop/noop_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package noop // import "go.opentelemetry.io/otel/trace/noop"
5
6 import (
7 "reflect"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11
12 "go.opentelemetry.io/otel/trace"
13 )
14
15 func TestImplementationNoPanics(t *testing.T) {
16 // Check that if type has an embedded interface and that interface has
17 // methods added to it than the No-Op implementation implements them.
18 t.Run("TracerProvider", assertAllExportedMethodNoPanic(
19 reflect.ValueOf(TracerProvider{}),
20 reflect.TypeFor[trace.TracerProvider](),
21 ))
22 t.Run("Tracer", assertAllExportedMethodNoPanic(
23 reflect.ValueOf(Tracer{}),
24 reflect.TypeFor[trace.Tracer](),
25 ))
26 t.Run("Span", assertAllExportedMethodNoPanic(
27 reflect.ValueOf(Span{}),
28 reflect.TypeFor[trace.Span](),
29 ))
30 }
31
32 func assertAllExportedMethodNoPanic(rVal reflect.Value, rType reflect.Type) func(*testing.T) {
33 return func(t *testing.T) {
34 for n := 0; n < rType.NumMethod(); n++ {
35 mType := rType.Method(n)
36 if !mType.IsExported() {
37 t.Logf("ignoring unexported %s", mType.Name)
38 continue
39 }
40 m := rVal.MethodByName(mType.Name)
41 if !m.IsValid() {
42 t.Errorf("unknown method for %s: %s", rVal.Type().Name(), mType.Name)
43 }
44
45 numIn := mType.Type.NumIn()
46 if mType.Type.IsVariadic() {
47 numIn--
48 }
49 args := make([]reflect.Value, numIn)
50 ctx := t.Context()
51 for i := range args {
52 aType := mType.Type.In(i)
53 if aType.Name() == "Context" {
54 // Do not panic on a nil context.
55 args[i] = reflect.ValueOf(ctx)
56 } else {
57 args[i] = reflect.New(aType).Elem()
58 }
59 }
60
61 assert.NotPanicsf(t, func() {
62 _ = m.Call(args)
63 }, "%s.%s", rVal.Type().Name(), mType.Name)
64 }
65 }
66 }
67
68 func TestNewTracerProvider(t *testing.T) {
69 tp := NewTracerProvider()
70 assert.Equal(t, TracerProvider{}, tp)
71 tracer := tp.Tracer("")
72 assert.Equal(t, Tracer{}, tracer)
73 }
74
75 func TestTracerStartPropagatesSpanContext(t *testing.T) {
76 tracer := NewTracerProvider().Tracer("")
77 spanCtx := trace.SpanContext{}
78
79 ctx := trace.ContextWithSpanContext(t.Context(), spanCtx)
80 ctx, span := tracer.Start(ctx, "test_span")
81 assert.Equal(t, spanCtx, trace.SpanContextFromContext(ctx), "empty span context not set in context")
82 assert.IsType(t, Span{}, span, "non-noop span returned")
83 assert.Equal(t, spanCtx, span.SpanContext(), "empty span context not returned from span")
84 assert.False(t, span.IsRecording(), "empty span context returned recording span")
85
86 spanCtx = spanCtx.WithTraceID(trace.TraceID([16]byte{1}))
87 spanCtx = spanCtx.WithSpanID(trace.SpanID([8]byte{1}))
88 ctx = trace.ContextWithSpanContext(t.Context(), spanCtx)
89 ctx, span = tracer.Start(ctx, "test_span")
90 assert.Equal(t, spanCtx, trace.SpanContextFromContext(ctx), "non-empty span context not set in context")
91 assert.Equal(t, spanCtx, span.SpanContext(), "non-empty span context not returned from span")
92 assert.False(t, span.IsRecording(), "non-empty span context returned recording span")
93
94 rSpan := recordingSpan{Span: Span{sc: spanCtx}}
95 ctx = trace.ContextWithSpan(t.Context(), rSpan)
96 ctx, span = tracer.Start(ctx, "test_span")
97 assert.Equal(t, spanCtx, trace.SpanContextFromContext(ctx), "recording span's span context not set in context")
98 assert.IsType(t, Span{}, span, "non-noop span returned")
99 assert.Equal(t, spanCtx, span.SpanContext(), "recording span's span context not returned from span")
100 assert.False(t, span.IsRecording(), "recording span returned")
101 }
102
103 func BenchmarkNoopInstance(b *testing.B) {
104 tracer := NewTracerProvider().Tracer("")
105 ctx := trace.ContextWithSpanContext(b.Context(), trace.SpanContext{})
106
107 b.ReportAllocs()
108 b.ResetTimer()
109
110 for i := 0; i < b.N; i++ {
111 _, span := tracer.Start(ctx, "")
112 span.End()
113 }
114 }
115
116 type recordingSpan struct{ Span }
117
118 func (recordingSpan) IsRecording() bool { return true }