From 20e7b8b1569c75816f797315252ee572f97dcb56 Mon Sep 17 00:00:00 2001 From: Chris Cotter Date: Wed, 11 Dec 2024 17:04:59 +0000 Subject: [PATCH 1/2] test(storage): fix trace span test Fixes #11264 --- storage/integration_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/storage/integration_test.go b/storage/integration_test.go index a9dc265c0ba5..b2b6775ab878 100644 --- a/storage/integration_test.go +++ b/storage/integration_test.go @@ -5859,10 +5859,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 := testutil.NewOpenTelemetryTestExporter() + defer te.Unregister(ctx) bkt := client.Bucket(bucket) bkt.Attrs(ctx) From 42b11bdeb5aaaae51a3cf1b4754ad51b18deb44b Mon Sep 17 00:00:00 2001 From: Chris Cotter Date: Wed, 11 Dec 2024 17:15:18 +0000 Subject: [PATCH 2/2] remove testutil dep --- storage/integration_test.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/storage/integration_test.go b/storage/integration_test.go index b2b6775ab878..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" @@ -5861,7 +5864,7 @@ func TestIntegration_PostPolicyV4_SignedURL_WithSignBytes(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.NewOpenTelemetryTestExporter() + te := newOpenTelemetryTestExporter() defer te.Unregister(ctx) bkt := client.Bucket(bucket) @@ -5873,6 +5876,38 @@ func TestIntegration_OTelTracing(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 {