From 5e3b939f4bc5d33e202dd6217043b8b5391043c1 Mon Sep 17 00:00:00 2001 From: ADITYA RAUT <159172287+adity1raut@users.noreply.github.com> Date: Tue, 2 Sep 2025 01:45:17 +0530 Subject: [PATCH] Add tracetest example for testing instrumentation (#7107) Fixes https://github.com/open-telemetry/opentelemetry-go/issues/7051 --- sdk/trace/tracetest/example_test.go | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sdk/trace/tracetest/example_test.go diff --git a/sdk/trace/tracetest/example_test.go b/sdk/trace/tracetest/example_test.go new file mode 100644 index 000000000..6478abfcf --- /dev/null +++ b/sdk/trace/tracetest/example_test.go @@ -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 +}