diff --git a/bigquery/go.mod b/bigquery/go.mod index 9141f5298334..9110de1bae40 100644 --- a/bigquery/go.mod +++ b/bigquery/go.mod @@ -13,6 +13,8 @@ require ( github.com/google/uuid v1.6.0 github.com/googleapis/gax-go/v2 v2.14.0 go.opencensus.io v0.24.0 + go.opentelemetry.io/otel v1.29.0 + go.opentelemetry.io/otel/sdk v1.29.0 golang.org/x/sync v0.9.0 golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da google.golang.org/api v0.210.0 @@ -42,9 +44,7 @@ require ( github.com/zeebo/xxh3 v1.0.2 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect - go.opentelemetry.io/otel v1.29.0 // indirect go.opentelemetry.io/otel/metric v1.29.0 // indirect - go.opentelemetry.io/otel/sdk v1.29.0 // indirect go.opentelemetry.io/otel/trace v1.29.0 // indirect golang.org/x/crypto v0.29.0 // indirect golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect diff --git a/bigquery/trace_integration_test.go b/bigquery/trace_integration_test.go index 3ecc99fb37b9..b5dd8125c5a0 100644 --- a/bigquery/trace_integration_test.go +++ b/bigquery/trace_integration_test.go @@ -17,20 +17,30 @@ package bigquery import ( "context" "strings" + "sync" "testing" "time" - traceinternal "cloud.google.com/go/internal/trace" - "go.opencensus.io/trace" + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/sdk/trace" ) // testExporter is a testing exporter for validating captured spans. type testExporter struct { - spans []*trace.SpanData + mu sync.Mutex + spans []trace.ReadOnlySpan } -func (te *testExporter) ExportSpan(s *trace.SpanData) { - te.spans = append(te.spans, s) +func (te *testExporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error { + te.mu.Lock() + defer te.mu.Unlock() + te.spans = append(te.spans, spans...) + return nil +} + +// Satisfy the exporter contract. This method does nothing. +func (te *testExporter) Shutdown(ctx context.Context) error { + return nil } // hasSpans checks that the exporter has all the span names @@ -41,7 +51,7 @@ func (te *testExporter) hasSpans(names []string) []string { matches[n] = struct{}{} } for _, s := range te.spans { - delete(matches, s.Name) + delete(matches, s.Name()) } var unmatched []string for k := range matches { @@ -50,20 +60,11 @@ func (te *testExporter) hasSpans(names []string) []string { return unmatched } -func TestIntegration_OpenCensusTracing(t *testing.T) { +func TestIntegration_Tracing(t *testing.T) { if client == nil { t.Skip("Integration tests skipped") } - if !traceinternal.IsOpenCensusTracingEnabled() { - t.Logf("enabling opencensus tracing") - traceinternal.SetOpenTelemetryTracingEnabledField(false) - defer func() { - t.Logf("enabling otel tracing") - traceinternal.SetOpenTelemetryTracingEnabledField(true) - }() - } - ctx := context.Background() for _, tc := range []struct { @@ -128,11 +129,19 @@ func TestIntegration_OpenCensusTracing(t *testing.T) { } { t.Run(tc.description, func(t *testing.T) { exporter := &testExporter{} - trace.RegisterExporter(exporter) - traceCtx, span := trace.StartSpan(ctx, "testspan", trace.WithSampler(trace.AlwaysSample())) + bsp := trace.NewBatchSpanProcessor(exporter) + tp := trace.NewTracerProvider( + trace.WithSampler(trace.AlwaysSample()), + trace.WithSpanProcessor(bsp), + ) + otel.SetTracerProvider(tp) + + tracer := tp.Tracer("test-trace") + traceCtx, span := tracer.Start(ctx, "startspan") + // Invoke the func to be traced. tc.callF(traceCtx) span.End() - trace.UnregisterExporter(exporter) + tp.Shutdown(traceCtx) if unmatched := exporter.hasSpans(tc.wantSpans); len(unmatched) > 0 { t.Errorf("case (%s): unmatched spans: %s", tc.description, strings.Join(unmatched, ","))