Skip to content

Commit

Permalink
test(bigquery): migrate trace testing from opencensus to otel (#11231)
Browse files Browse the repository at this point in the history
  • Loading branch information
shollyman authored Dec 6, 2024
1 parent 7e680b9 commit 7e4ada8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
4 changes: 2 additions & 2 deletions bigquery/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
47 changes: 28 additions & 19 deletions bigquery/trace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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, ","))
Expand Down

0 comments on commit 7e4ada8

Please sign in to comment.