Skip to content

Commit

Permalink
testing(bigquery): fix legacy trace test (#10411)
Browse files Browse the repository at this point in the history
This PR ensures that the trace environment is setup to expose the expected spans (and restores settings after complete), as well as converting this to a subtest-style test.

Fixes: https://togithub.com/googleapis/google-cloud-go/issues/10389
  • Loading branch information
shollyman authored Jun 21, 2024
1 parent d6c543c commit 1068ca8
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions bigquery/trace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

traceinternal "cloud.google.com/go/internal/trace"
"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -49,11 +50,20 @@ func (te *testExporter) hasSpans(names []string) []string {
return unmatched
}

func TestIntegration_Tracing(t *testing.T) {
func TestIntegration_OpenCensusTracing(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 @@ -85,15 +95,17 @@ func TestIntegration_Tracing(t *testing.T) {
wantSpans: []string{"bigquery.tables.get", "cloud.google.com/go/bigquery.Table.Metadata"},
},
} {
exporter := &testExporter{}
trace.RegisterExporter(exporter)
traceCtx, span := trace.StartSpan(ctx, "testspan", trace.WithSampler(trace.AlwaysSample()))
tc.callF(traceCtx)
span.End()
trace.UnregisterExporter(exporter)
t.Run(tc.description, func(t *testing.T) {
exporter := &testExporter{}
trace.RegisterExporter(exporter)
traceCtx, span := trace.StartSpan(ctx, "testspan", trace.WithSampler(trace.AlwaysSample()))
tc.callF(traceCtx)
span.End()
trace.UnregisterExporter(exporter)

if unmatched := exporter.hasSpans(tc.wantSpans); len(unmatched) > 0 {
t.Errorf("case (%s): unmatched spans: %s", tc.description, strings.Join(unmatched, ","))
}
if unmatched := exporter.hasSpans(tc.wantSpans); len(unmatched) > 0 {
t.Errorf("case (%s): unmatched spans: %s", tc.description, strings.Join(unmatched, ","))
}
})
}
}

0 comments on commit 1068ca8

Please sign in to comment.