From 2f94a8204dbd99807bbb6b251da54fd62079d929 Mon Sep 17 00:00:00 2001 From: Chris Cotter Date: Wed, 11 Dec 2024 12:24:27 -0500 Subject: [PATCH] test(storage): fix trace span test (#11272) Fixes #11264 --- storage/integration_test.go | 41 ++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/storage/integration_test.go b/storage/integration_test.go index a9dc265c0ba5..e28afae60850 100644 --- a/storage/integration_test.go +++ b/storage/integration_test.go @@ -54,7 +54,10 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/googleapis/gax-go/v2/apierror" "go.opentelemetry.io/contrib/detectors/gcp" + "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/sdk/resource" + sdktrace "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" "golang.org/x/oauth2/google" "google.golang.org/api/googleapi" "google.golang.org/api/iterator" @@ -5859,10 +5862,10 @@ func TestIntegration_PostPolicyV4_SignedURL_WithSignBytes(t *testing.T) { }) } -func TestIntegration_OCTracing(t *testing.T) { +func TestIntegration_OTelTracing(t *testing.T) { multiTransportTest(context.Background(), t, func(t *testing.T, ctx context.Context, bucket string, _ string, client *Client) { - te := testutil.NewTestExporter() - defer te.Unregister() + te := newOpenTelemetryTestExporter() + defer te.Unregister(ctx) bkt := client.Bucket(bucket) bkt.Attrs(ctx) @@ -5873,6 +5876,38 @@ func TestIntegration_OCTracing(t *testing.T) { }) } +// openTelemetryTestExporter is a test utility exporter. It should be created +// with NewopenTelemetryTestExporter. +type openTelemetryTestExporter struct { + exporter *tracetest.InMemoryExporter + tp *sdktrace.TracerProvider +} + +// newOpenTelemetryTestExporter creates a openTelemetryTestExporter with +// underlying InMemoryExporter and TracerProvider from OpenTelemetry. +func newOpenTelemetryTestExporter() *openTelemetryTestExporter { + exporter := tracetest.NewInMemoryExporter() + tp := sdktrace.NewTracerProvider( + sdktrace.WithSyncer(exporter), + sdktrace.WithSampler(sdktrace.AlwaysSample()), + ) + otel.SetTracerProvider(tp) + return &openTelemetryTestExporter{ + exporter: exporter, + tp: tp, + } +} + +// Spans returns the current in-memory stored spans. +func (te *openTelemetryTestExporter) Spans() tracetest.SpanStubs { + return te.exporter.GetSpans() +} + +// Unregister shuts down the underlying OpenTelemetry TracerProvider. +func (te *openTelemetryTestExporter) Unregister(ctx context.Context) { + te.tp.Shutdown(ctx) +} + // verifySignedURL gets the bytes at the provided url and verifies them against the // expectedFileBody. Make sure the SignedURLOptions set the method as "GET". func verifySignedURL(url string, headers map[string][]string, expectedFileBody []byte) error {