/
exporter.go
104 lines (90 loc) · 3.12 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019 The Go Cloud Development Kit Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package octest supports testing of OpenCensus integrations.
package octest
// This code was copied from cloud.google.com/go/internal/testutil/trace.go
import (
"log"
"sync"
"time"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
)
// TestExporter is an exporter of OpenCensus traces and metrics, for testing.
// It should be created with NewTestExporter.
type TestExporter struct {
mu sync.Mutex
spans []*trace.SpanData
Stats chan *view.Data
}
// NewTestExporter creates a TestExporter and registers it with OpenCensus.
func NewTestExporter(views []*view.View) *TestExporter {
te := &TestExporter{Stats: make(chan *view.Data)}
// Register for metrics.
view.RegisterExporter(te)
// The reporting period will affect how long it takes to get stats (view.Data).
// We want it short so tests don't take too long, but long enough so that all
// the actions in a test complete.
// If the period is too short, then some actions may not be finished when the first
// call to ExportView happens. diffCounts checks for matching counts, so it will
// fail in that case.
// Tests that use the exporter (search for TestOpenCensus) are designed to avoid
// network traffic or computation, so they finish quickly. But we must account for
// the race detector, which slows everything down.
view.SetReportingPeriod(100 * time.Millisecond)
if err := view.Register(views...); err != nil {
log.Fatal(err)
}
// Register for traces.
trace.RegisterExporter(te)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
return te
}
// ExportSpan "exports" a span by remembering it.
func (te *TestExporter) ExportSpan(s *trace.SpanData) {
te.mu.Lock()
defer te.mu.Unlock()
te.spans = append(te.spans, s)
}
// ExportView exports a view by writing it to the Stats channel.
func (te *TestExporter) ExportView(vd *view.Data) {
if len(vd.Rows) > 0 {
select {
case te.Stats <- vd:
default:
}
}
}
func (te *TestExporter) Spans() []*trace.SpanData {
te.mu.Lock()
defer te.mu.Unlock()
return te.spans
}
// Counts returns the first exported data that includes aggregated counts.
func (te *TestExporter) Counts() []*view.Row {
// Wait for counts. Expect all counts to arrive in the same view.Data.
for {
data := <-te.Stats
if _, ok := data.Rows[0].Data.(*view.CountData); !ok {
continue
}
return data.Rows
}
}
// Unregister unregisters the exporter from OpenCensus.
func (te *TestExporter) Unregister() {
view.UnregisterExporter(te)
trace.UnregisterExporter(te)
view.SetReportingPeriod(0) // reset to default value
}