...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package bigquery
16
17 import (
18 "context"
19 "strings"
20 "testing"
21 "time"
22
23 "go.opencensus.io/trace"
24 )
25
26
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
36
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