| 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 |
} |