...

Source file src/cloud.google.com/go/bigquery/trace_integration_test.go

Documentation: cloud.google.com/go/bigquery

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bigquery
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  	"testing"
    21  	"time"
    22  
    23  	"go.opencensus.io/trace"
    24  )
    25  
    26  // testExporter is a testing exporter for validating captured spans.
    27  type testExporter struct {
    28  	spans []*trace.SpanData
    29  }
    30  
    31  func (te *testExporter) ExportSpan(s *trace.SpanData) {
    32  	te.spans = append(te.spans, s)
    33  }
    34  
    35  // hasSpans checks that the exporter has all the span names
    36  // specified in the slice.  It returns the unmatched names.
    37  func (te *testExporter) hasSpans(names []string) []string {
    38  	matches := make(map[string]struct{})
    39  	for _, n := range names {
    40  		matches[n] = struct{}{}
    41  	}
    42  	for _, s := range te.spans {
    43  		delete(matches, s.Name)
    44  	}
    45  	var unmatched []string
    46  	for k := range matches {
    47  		unmatched = append(unmatched, k)
    48  	}
    49  	return unmatched
    50  }
    51  
    52  func TestIntegration_Tracing(t *testing.T) {
    53  	if client == nil {
    54  		t.Skip("Integration tests skipped")
    55  	}
    56  
    57  	ctx := context.Background()
    58  
    59  	for _, tc := range []struct {
    60  		description string
    61  		callF       func(ctx context.Context)
    62  		wantSpans   []string
    63  	}{
    64  		{
    65  			description: "fast path query",
    66  			callF: func(ctx context.Context) {
    67  				client.Query("SELECT SESSION_USER()").Read(ctx)
    68  			},
    69  			wantSpans: []string{"bigquery.jobs.query", "cloud.google.com/go/bigquery.Query.Run"},
    70  		},
    71  		{
    72  			description: "slow path query",
    73  			callF: func(ctx context.Context) {
    74  				q := client.Query("SELECT SESSION_USER()")
    75  				q.JobTimeout = time.Hour
    76  				q.Read(ctx)
    77  			},
    78  			wantSpans: []string{"bigquery.jobs.insert", "bigquery.jobs.getQueryResults", "cloud.google.com/go/bigquery.Job.Read", "cloud.google.com/go/bigquery.Query.Run"},
    79  		},
    80  		{
    81  			description: "table metadata",
    82  			callF: func(ctx context.Context) {
    83  				client.DatasetInProject("bigquery-public-data", "samples").Table("shakespeare").Metadata(ctx)
    84  			},
    85  			wantSpans: []string{"bigquery.tables.get", "cloud.google.com/go/bigquery.Table.Metadata"},
    86  		},
    87  	} {
    88  		exporter := &testExporter{}
    89  		trace.RegisterExporter(exporter)
    90  		traceCtx, span := trace.StartSpan(ctx, "testspan", trace.WithSampler(trace.AlwaysSample()))
    91  		tc.callF(traceCtx)
    92  		span.End()
    93  		trace.UnregisterExporter(exporter)
    94  
    95  		if unmatched := exporter.hasSpans(tc.wantSpans); len(unmatched) > 0 {
    96  			t.Errorf("case (%s): unmatched spans: %s", tc.description, strings.Join(unmatched, ","))
    97  		}
    98  	}
    99  }
   100  

View as plain text