-
Notifications
You must be signed in to change notification settings - Fork 13
/
explicit_test.go
180 lines (138 loc) · 4.47 KB
/
explicit_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
175
176
177
178
179
180
package explicit
import (
"reflect"
"testing"
"go.opentelemetry.io/otel/metric/unit"
export "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
"go.opentelemetry.io/otel/sdk/metric/number"
"go.opentelemetry.io/otel/sdk/metric/sdkapi"
)
var (
defaultFloat64ExplicitBoundaries = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
)
func TestSelector_HistogramConfig(t *testing.T) {
_, cache := NewExplicitHistogramDistribution()
t.Run("when empty", func(t *testing.T) {
got := cache.Fetch("test")
if len(got) != 0 {
t.Fatalf("expected empty options, got %v", got)
}
})
t.Run("with single option", func(t *testing.T) {
_, cache := NewExplicitHistogramDistribution()
boundaries := defaultFloat64ExplicitBoundaries
want := []histogram.Option{
histogram.WithExplicitBoundaries(boundaries),
}
// store and fetch the default boundaries
cache.Store("test", want...)
got := cache.Fetch("test")
if len(got) != 1 {
t.Fatalf("unexpected options length, want %v, got %v", 1, len(got))
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected options, want %v and got %v", want, got)
}
})
t.Run("replaces single option", func(t *testing.T) {
_, cache := NewExplicitHistogramDistribution()
// store the default boundaries
cache.Store("test", histogram.WithExplicitBoundaries(defaultFloat64ExplicitBoundaries))
boundaries := []float64{1, 2, 3, 4, 5}
want := []histogram.Option{
histogram.WithExplicitBoundaries(boundaries),
}
// store and fetch some updated boundaries
cache.Store("test", want...)
got := cache.Fetch("test")
if len(got) != 1 {
t.Fatalf("unexpected options length, want %v, got %v", 1, len(got))
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected options, want %v and got %v", want, got)
}
})
t.Run("many named metrics", func(t *testing.T) {
_, cache := NewExplicitHistogramDistribution()
metrics := []struct {
name string
option []float64
}{
{
name: "test1",
option: []float64{1, 2, 3, 4, 5},
},
{
name: "test2",
option: []float64{10, 20, 30, 40, 50},
},
}
for _, metric := range metrics {
want := []histogram.Option{
histogram.WithExplicitBoundaries(metric.option),
}
// store and fetch some updated boundaries
cache.Store(metric.name, want...)
got := cache.Fetch(metric.name)
if len(got) != 1 {
t.Fatalf("expected empty options, got %v", got)
}
if !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected options, want %v and got %v", want, got)
}
}
c, ok := cache.(*selectorCache)
if !ok {
t.Errorf("unexpected casting problem err: %v", ok)
}
if len(c.opts) != 2 {
t.Fatalf("unexpected opts map, want 2 keys and got %v", c.opts)
}
})
}
func TestSelector_Histogram(t *testing.T) {
var aggregator export.Aggregator
desc := sdkapi.NewDescriptor("test", sdkapi.HistogramInstrumentKind, number.Float64Kind, "testing", unit.Milliseconds)
t.Run("no options", func(t *testing.T) {
selector, _ := NewExplicitHistogramDistribution()
selector.AggregatorFor(&desc, &aggregator)
if aggregator == nil {
t.Fatal("expected aggregator to be initialized, got nil")
}
agg := aggregator.Aggregation()
if agg.Kind() != aggregation.HistogramKind {
t.Fatalf("expected kind to be %v, got %v", aggregation.HistogramKind, agg.Kind())
}
buckets, err := agg.(aggregation.Histogram).Histogram()
if err != nil {
t.Error(err)
}
boundaries := buckets.Boundaries
if !reflect.DeepEqual(boundaries, defaultFloat64ExplicitBoundaries) {
t.Fatalf("expected boundaries to match, want: %v, got: %v", defaultFloat64ExplicitBoundaries, boundaries)
}
})
t.Run("custom options", func(t *testing.T) {
want := []float64{1, 2, 3, 4, 5}
selector, cache := NewExplicitHistogramDistribution()
cache.Store(desc.Name(), histogram.WithExplicitBoundaries(want))
selector.AggregatorFor(&desc, &aggregator)
if aggregator == nil {
t.Fatal("expected aggregator to be initialized, got nil")
}
agg := aggregator.Aggregation()
if agg.Kind() != aggregation.HistogramKind {
t.Fatalf("expected kind to be %v, got %v", aggregation.HistogramKind, agg.Kind())
}
buckets, err := agg.(aggregation.Histogram).Histogram()
if err != nil {
t.Error(err)
}
got := buckets.Boundaries
if !reflect.DeepEqual(want, got) {
t.Fatalf("expected boundaries to match, want: %v, got: %v", want, got)
}
})
}