Debian Codegraph @

sdk/trace/util_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package trace
5
6 import (
7 "context"
8 "fmt"
9 "strconv"
10 "sync"
11 "testing"
12 "time"
13
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16
17 "go.opentelemetry.io/otel/attribute"
18 "go.opentelemetry.io/otel/codes"
19 "go.opentelemetry.io/otel/trace"
20 )
21
22 func basicTracerProvider(t *testing.T) *TracerProvider {
23 tp := NewTracerProvider(WithSampler(AlwaysSample()))
24 t.Cleanup(func() {
25 //nolint:usetesting // required to avoid getting a canceled context at cleanup.
26 assert.NoError(t, tp.Shutdown(context.Background()))
27 })
28 return tp
29 }
30
31 type testError string
32
33 var _ error = testError("")
34
35 func newTestError(s string) error {
36 return testError(s)
37 }
38
39 func (e testError) Error() string {
40 return string(e)
41 }
42
43 // harness is a testing harness used to test implementations of the
44 // OpenTelemetry API.
45 type harness struct {
46 t *testing.T
47 }
48
49 // newHarness returns an instantiated *harness using t.
50 func newHarness(t *testing.T) *harness {
51 return &harness{
52 t: t,
53 }
54 }
55
56 // testTracerProvider runs validation tests for an implementation of the OpenTelemetry
57 // TracerProvider API.
58 func (h *harness) testTracerProvider(subjectFactory func() trace.TracerProvider) {
59 h.t.Run("#Start", func(t *testing.T) {
60 t.Run("allow creating an arbitrary number of TracerProvider instances", func(t *testing.T) {
61 t.Parallel()
62
63 tp1 := subjectFactory()
64 tp2 := subjectFactory()
65
66 require.NotEqual(t, tp1, tp2)
67 })
68 t.Run("all methods are safe to be called concurrently", func(t *testing.T) {
69 t.Parallel()
70
71 runner := func(tp trace.TracerProvider) <-chan struct{} {
72 done := make(chan struct{})
73 go func(tp trace.TracerProvider) {
74 var wg sync.WaitGroup
75 for i := range 20 {
76 wg.Add(1)
77 go func(name, version string) {
78 _ = tp.Tracer(name, trace.WithInstrumentationVersion(version))
79 wg.Done()
80 }(fmt.Sprintf("tracer %d", i%5), strconv.Itoa(i))
81 }
82 wg.Wait()
83 done <- struct{}{}
84 }(tp)
85 return done
86 }
87
88 require.NotPanics(t,
89 func() {
90 // Run with multiple TracerProvider to ensure they encapsulate
91 // their own Tracers.
92 tp1 := subjectFactory()
93 tp2 := subjectFactory()
94
95 done1 := runner(tp1)
96 done2 := runner(tp2)
97
98 <-done1
99 <-done2
100 })
101 })
102 })
103 }
104
105 // testTracer runs validation tests for an implementation of the OpenTelemetry
106 // Tracer API.
107 func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
108 h.t.Run("#Start", func(t *testing.T) {
109 t.Run("propagates the original context", func(t *testing.T) {
110 t.Parallel()
111
112 subject := subjectFactory()
113
114 ctxKey := testCtxKey{}
115 ctxValue := "ctx value"
116 ctx := context.WithValue(t.Context(), ctxKey, ctxValue)
117
118 ctx, _ = subject.Start(ctx, "test")
119
120 require.Equal(t, ctx.Value(ctxKey), ctxValue)
121 })
122
123 t.Run("returns a span containing the expected properties", func(t *testing.T) {
124 t.Parallel()
125
126 subject := subjectFactory()
127
128 _, span := subject.Start(t.Context(), "test")
129
130 require.NotNil(t, span)
131 require.True(t, span.SpanContext().IsValid())
132 })
133
134 t.Run("stores the span on the provided context", func(t *testing.T) {
135 t.Parallel()
136
137 subject := subjectFactory()
138
139 ctx, span := subject.Start(t.Context(), "test")
140
141 require.NotNil(t, span)
142 require.NotEqual(t, trace.SpanContext{}, span.SpanContext())
143 require.Equal(t, trace.SpanFromContext(ctx), span)
144 })
145
146 t.Run("starts spans with unique trace and span IDs", func(t *testing.T) {
147 t.Parallel()
148
149 subject := subjectFactory()
150
151 _, span1 := subject.Start(t.Context(), "span1")
152 _, span2 := subject.Start(t.Context(), "span2")
153
154 sc1 := span1.SpanContext()
155 sc2 := span2.SpanContext()
156
157 require.NotEqual(t, sc1.TraceID(), sc2.TraceID())
158 require.NotEqual(t, sc1.SpanID(), sc2.SpanID())
159 })
160
161 t.Run("propagates a parent's trace ID through the context", func(t *testing.T) {
162 t.Parallel()
163
164 subject := subjectFactory()
165
166 ctx, parent := subject.Start(t.Context(), "parent")
167 _, child := subject.Start(ctx, "child")
168
169 psc := parent.SpanContext()
170 csc := child.SpanContext()
171
172 require.Equal(t, csc.TraceID(), psc.TraceID())
173 require.NotEqual(t, csc.SpanID(), psc.SpanID())
174 })
175
176 t.Run("ignores parent's trace ID when new root is requested", func(t *testing.T) {
177 t.Parallel()
178
179 subject := subjectFactory()
180
181 ctx, parent := subject.Start(t.Context(), "parent")
182 _, child := subject.Start(ctx, "child", trace.WithNewRoot())
183
184 psc := parent.SpanContext()
185 csc := child.SpanContext()
186
187 require.NotEqual(t, csc.TraceID(), psc.TraceID())
188 require.NotEqual(t, csc.SpanID(), psc.SpanID())
189 })
190
191 t.Run("propagates remote parent's trace ID through the context", func(t *testing.T) {
192 t.Parallel()
193
194 subject := subjectFactory()
195
196 _, remoteParent := subject.Start(t.Context(), "remote parent")
197 parentCtx := trace.ContextWithRemoteSpanContext(t.Context(), remoteParent.SpanContext())
198 _, child := subject.Start(parentCtx, "child")
199
200 psc := remoteParent.SpanContext()
201 csc := child.SpanContext()
202
203 require.Equal(t, csc.TraceID(), psc.TraceID())
204 require.NotEqual(t, csc.SpanID(), psc.SpanID())
205 })
206
207 t.Run("ignores remote parent's trace ID when new root is requested", func(t *testing.T) {
208 t.Parallel()
209
210 subject := subjectFactory()
211
212 _, remoteParent := subject.Start(t.Context(), "remote parent")
213 parentCtx := trace.ContextWithRemoteSpanContext(t.Context(), remoteParent.SpanContext())
214 _, child := subject.Start(parentCtx, "child", trace.WithNewRoot())
215
216 psc := remoteParent.SpanContext()
217 csc := child.SpanContext()
218
219 require.NotEqual(t, csc.TraceID(), psc.TraceID())
220 require.NotEqual(t, csc.SpanID(), psc.SpanID())
221 })
222
223 t.Run("all methods are safe to be called concurrently", func(t *testing.T) {
224 t.Parallel()
225
226 tracer := subjectFactory()
227
228 ctx, parent := tracer.Start(t.Context(), "span")
229
230 runner := func(tp trace.Tracer) <-chan struct{} {
231 done := make(chan struct{})
232 go func(tp trace.Tracer) {
233 var wg sync.WaitGroup
234 for i := range 20 {
235 wg.Add(1)
236 go func(name string) {
237 defer wg.Done()
238 _, child := tp.Start(ctx, name)
239
240 psc := parent.SpanContext()
241 csc := child.SpanContext()
242
243 require.Equal(t, csc.TraceID(), psc.TraceID())
244 require.NotEqual(t, csc.SpanID(), psc.SpanID())
245 }(fmt.Sprintf("span %d", i))
246 }
247 wg.Wait()
248 done <- struct{}{}
249 }(tp)
250 return done
251 }
252
253 require.NotPanics(t, func() {
254 done := runner(tracer)
255
256 <-done
257 })
258 })
259 })
260
261 h.testSpan(subjectFactory)
262 }
263
264 func (h *harness) testSpan(tracerFactory func() trace.Tracer) {
265 methods := map[string]func(span trace.Span){
266 "#End": func(span trace.Span) {
267 span.End()
268 },
269 "#AddEvent": func(span trace.Span) {
270 span.AddEvent("test event")
271 },
272 "#AddEventWithTimestamp": func(span trace.Span) {
273 span.AddEvent("test event", trace.WithTimestamp(time.Now().Add(1*time.Second)))
274 },
275 "#SetStatus": func(span trace.Span) {
276 span.SetStatus(codes.Error, "internal")
277 },
278 "#SetName": func(span trace.Span) {
279 span.SetName("new name")
280 },
281 "#SetAttributes": func(span trace.Span) {
282 span.SetAttributes(attribute.String("key1", "value"), attribute.Int("key2", 123))
283 },
284 }
285 mechanisms := map[string]func() trace.Span{
286 "Span created via Tracer#Start": func() trace.Span {
287 tracer := tracerFactory()
288 _, subject := tracer.Start(h.t.Context(), "test")
289
290 return subject
291 },
292 "Span created via span.TracerProvider()": func() trace.Span {
293 ctx, spanA := tracerFactory().Start(h.t.Context(), "span1")
294
295 _, spanB := spanA.TracerProvider().Tracer("second").Start(ctx, "span2")
296 return spanB
297 },
298 }
299
300 for mechanismName, mechanism := range mechanisms {
301 h.t.Run(mechanismName, func(t *testing.T) {
302 for methodName, method := range methods {
303 t.Run(methodName, func(t *testing.T) {
304 t.Run("is thread-safe", func(t *testing.T) {
305 t.Parallel()
306
307 span := mechanism()
308
309 wg := &sync.WaitGroup{}
310 wg.Add(2)
311
312 go func() {
313 defer wg.Done()
314
315 method(span)
316 }()
317
318 go func() {
319 defer wg.Done()
320
321 method(span)
322 }()
323
324 wg.Wait()
325 })
326 })
327 }
328
329 t.Run("#End", func(t *testing.T) {
330 t.Run("can be called multiple times", func(t *testing.T) {
331 t.Parallel()
332
333 span := mechanism()
334
335 span.End()
336 span.End()
337 })
338 })
339 })
340 }
341 }
342
343 type testCtxKey struct{}