You've already forked opentelemetry-go
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:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user