Skip to content

Commit 5e3b939

Browse files
authored
Add tracetest example for testing instrumentation (#7107)
Fixes #7051
1 parent 090e9ef commit 5e3b939

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package tracetest_test
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"go.opentelemetry.io/otel/sdk/trace"
11+
"go.opentelemetry.io/otel/sdk/trace/tracetest"
12+
)
13+
14+
func ExampleSpanRecorder() {
15+
ctx := context.Background()
16+
17+
// Set up an in-memory span recorder and tracer provider.
18+
sr := tracetest.NewSpanRecorder()
19+
tp := trace.NewTracerProvider(
20+
trace.WithSpanProcessor(sr),
21+
)
22+
defer tp.Shutdown(ctx) //nolint:errcheck // Example code, error handling omitted.
23+
24+
tracer := tp.Tracer("example/simple")
25+
26+
// Start and end a span.
27+
_, span := tracer.Start(ctx, "test-span")
28+
span.End()
29+
30+
// Print the recorded span name.
31+
for _, s := range sr.Ended() {
32+
fmt.Println(s.Name())
33+
}
34+
35+
// Output:
36+
// test-span
37+
}

0 commit comments

Comments
 (0)