1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

Add tracetest example for testing instrumentation (#7107)

Fixes https://github.com/open-telemetry/opentelemetry-go/issues/7051
This commit is contained in:
ADITYA RAUT
2025-09-02 01:45:17 +05:30
committed by GitHub
parent 090e9ef5e7
commit 5e3b939f4b
+37
View File
@@ -0,0 +1,37 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package tracetest_test
import (
"context"
"fmt"
"go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
)
func ExampleSpanRecorder() {
ctx := context.Background()
// Set up an in-memory span recorder and tracer provider.
sr := tracetest.NewSpanRecorder()
tp := trace.NewTracerProvider(
trace.WithSpanProcessor(sr),
)
defer tp.Shutdown(ctx) //nolint:errcheck // Example code, error handling omitted.
tracer := tp.Tracer("example/simple")
// Start and end a span.
_, span := tracer.Start(ctx, "test-span")
span.End()
// Print the recorded span name.
for _, s := range sr.Ended() {
fmt.Println(s.Name())
}
// Output:
// test-span
}