/
test.go
174 lines (142 loc) 路 5.02 KB
/
test.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright The OpenTelemetry 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
//
// http://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 test // import "github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/internal/test"
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/aggregator/aggregation"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/aggregator/gauge"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/data"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/number"
"github.com/lightstep/otel-launcher-go/lightstep/sdk/metric/sdkinstrument"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/instrumentation"
"go.opentelemetry.io/otel/sdk/resource"
)
func Descriptor(name string, ik sdkinstrument.Kind, nk number.Kind) sdkinstrument.Descriptor {
return sdkinstrument.NewDescriptor(name, ik, nk, "", "")
}
func DescriptorDescUnit(name string, ik sdkinstrument.Kind, nk number.Kind, desc, unit string) sdkinstrument.Descriptor {
return sdkinstrument.NewDescriptor(name, ik, nk, desc, unit)
}
func Point(start, end time.Time, agg aggregation.Aggregation, tempo aggregation.Temporality, kvs ...attribute.KeyValue) data.Point {
attrs := attribute.NewSet(kvs...)
return data.Point{
Start: start,
End: end,
Attributes: attrs,
Aggregation: agg,
Temporality: tempo,
}
}
func Instrument(desc sdkinstrument.Descriptor, points ...data.Point) data.Instrument {
return data.Instrument{
Descriptor: desc,
Points: points,
}
}
func Library(name string, opts ...metric.MeterOption) instrumentation.Library {
cfg := metric.NewMeterConfig(opts...)
return instrumentation.Library{
Name: name,
Version: cfg.InstrumentationVersion(),
SchemaURL: cfg.SchemaURL(),
}
}
func Scope(library instrumentation.Library, insts ...data.Instrument) data.Scope {
return data.Scope{
Library: library,
Instruments: insts,
}
}
func Metrics(res *resource.Resource, scopes ...data.Scope) data.Metrics {
return data.Metrics{
Resource: res,
Scopes: scopes,
}
}
func CollectScope(t *testing.T, collectors []data.Collector, seq data.Sequence) []data.Instrument {
t.Helper()
var output data.Scope
return CollectScopeReuse(t, collectors, seq, &output)
}
func CollectScopeReuse(t *testing.T, collectors []data.Collector, seq data.Sequence, output *data.Scope) []data.Instrument {
t.Helper()
output.Reset()
for _, coll := range collectors {
coll.Collect(seq, &output.Instruments)
}
return output.Instruments
}
func RequireEqualPoints(t *testing.T, output []data.Point, expected ...data.Point) {
t.Helper()
require.Equal(t, len(expected), len(output), "points have different length")
cpy := make([]data.Point, len(expected))
copy(cpy, expected)
// If the expectations have zero timestamps, the output
// timestamps are zeroed so they will match exactly. Gauge
// sequence numbers are set to match test conditions.
for idx := range cpy {
exp := &cpy[idx]
out := &output[idx]
if exp.Start.IsZero() {
out.Start = exp.Start
}
if exp.End.IsZero() {
out.End = exp.End
}
if outig, ok := out.Aggregation.(*gauge.Int64); ok {
outig.SetSequenceForTesting()
}
if outfg, ok := out.Aggregation.(*gauge.Float64); ok {
outfg.SetSequenceForTesting()
}
}
require.ElementsMatch(t, cpy, output)
}
// RequireEqualMetrics checks that an output equals the expected
// output, where the points are taken to be unordered. Instrument
// order is expected to match because the compiler preserves the order
// of views and instruments as they are compiled.
func RequireEqualMetrics(
t *testing.T,
output []data.Instrument,
expected ...data.Instrument) {
t.Helper()
require.Equal(t, len(expected), len(output))
for idx := range output {
require.Equal(t, expected[idx].Descriptor, output[idx].Descriptor)
RequireEqualPoints(t, output[idx].Points, expected[idx].Points...)
}
}
func RequireEqualResourceMetrics(t *testing.T, output data.Metrics, expectRes *resource.Resource, expectScopes ...data.Scope) {
t.Helper()
require.Equal(t, expectRes, output.Resource)
require.Equal(t, len(expectScopes), len(output.Scopes))
for i, got := range output.Scopes {
expect := expectScopes[i]
require.Equal(t, expect.Library, got.Library)
RequireEqualMetrics(t, got.Instruments, expect.Instruments...)
}
}
func OTelErrors() *[]error {
errors := new([]error)
otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {
*errors = append(*errors, err)
}))
return errors
}