forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.go
More file actions
228 lines (180 loc) · 5.63 KB
/
test.go
File metadata and controls
228 lines (180 loc) · 5.63 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package registry
import (
"fmt"
"math"
"sort"
"strings"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/storage"
)
// newTestLabelLimiter returns a PerLabelLimiter with limiting disabled (maxCardinality=0).
func newTestLabelLimiter() *PerLabelLimiter {
return NewPerLabelLimiter("test", func(string) uint64 { return 0 }, 0)
}
// TestRegistry is a simple implementation of Registry intended for tests. It is not concurrent-safe.
type TestRegistry struct {
// "metric{labels}" -> value
metrics map[string]float64
}
var _ Registry = (*TestRegistry)(nil)
func NewTestRegistry() *TestRegistry {
return &TestRegistry{
metrics: map[string]float64{},
}
}
func (t *TestRegistry) NewCounter(name string) Counter {
return &testCounter{
n: name,
registry: t,
}
}
func (t *TestRegistry) NewGauge(name string) Gauge {
return &testGauge{
n: name,
registry: t,
}
}
func (t *TestRegistry) NewLabelBuilder() LabelBuilder {
nds := NewDrainSanitizer("test", func(string) string { return SpanNameSanitizationDisabled }, 0)
return NewLabelBuilder(0, 0, nds, newTestLabelLimiter())
}
func (t *TestRegistry) NewInfoMetricLabelBuilder() LabelBuilder {
return NewLabelBuilder(0, 0, noopSanitizer{}, noopLabelLimiter{})
}
func (t *TestRegistry) NewHistogram(name string, buckets []float64, histogramOverrides HistogramMode) Histogram {
return &testHistogram{
nameSum: name + "_sum",
nameCount: name + "_count",
nameBucket: name + "_bucket",
buckets: buckets,
registry: t,
histogramOverrides: histogramOverrides,
}
}
func (t *TestRegistry) addToMetric(name string, lbls labels.Labels, value float64) {
if t == nil || t.metrics == nil {
return
}
t.metrics[name+lbls.String()] += value
}
func (t *TestRegistry) setMetric(name string, lbls labels.Labels, value float64) {
if t == nil || t.metrics == nil {
return
}
t.metrics[name+lbls.String()] = value
}
// Query returns the value of the given metric. Note this is a rather naive query engine, it's only
// possible to query metrics by using the exact same labels as they were stored with.
func (t *TestRegistry) Query(name string, lbls labels.Labels) float64 {
// The prometheus data model does not distinguish between empty labels and
// missing labels, so here we ignore empty labels in the query.
return t.metrics[name+lbls.WithoutEmpty().String()]
}
func (t *TestRegistry) String() string {
var metrics []string
for metric, value := range t.metrics {
metrics = append(metrics, fmt.Sprintf("%s %g", metric, value))
}
sort.Strings(metrics)
return strings.Join(metrics, "\n")
}
type testCounter struct {
n string
registry *TestRegistry
}
var _ Counter = (*testCounter)(nil)
func (t *testCounter) Inc(lbls labels.Labels, value float64) {
if value < 0 {
panic("counter can only increase")
}
t.registry.addToMetric(t.n, lbls, value)
}
func (t *testCounter) name() string {
return t.n
}
func (t *testCounter) collectMetrics(_ storage.Appender, _ int64) error {
return nil
}
func (t *testCounter) removeStaleSeries(int64) {
panic("implement me")
}
func (t *testCounter) countActiveSeries() int {
return 0
}
// countSeriesDemand is a stub to satisfy optional estimator usage in registry.
// Test registry does not track estimates, so return 0.
func (t *testCounter) countSeriesDemand() int { return 0 }
type testGauge struct {
n string
registry *TestRegistry
}
var _ Gauge = (*testGauge)(nil)
func (t *testGauge) Inc(lbls labels.Labels, value float64) {
if value < 0 {
panic("counter can only increase")
}
t.registry.addToMetric(t.n, lbls, value)
}
func (t *testGauge) Set(lbls labels.Labels, value float64) {
t.registry.setMetric(t.n, lbls, value)
}
func (t *testGauge) SetForTargetInfo(lbls labels.Labels, value float64) {
t.Set(lbls, value)
}
func (t *testGauge) name() string {
return t.n
}
func (t *testGauge) collectMetrics(_ storage.Appender, _ int64) error {
return nil
}
func (t *testGauge) removeStaleSeries(int64) {
panic("implement me")
}
func (t *testGauge) countActiveSeries() int {
return 0
}
// countSeriesDemand is a stub to satisfy optional estimator usage in registry.
// Test registry does not track estimates, so return 0.
func (t *testGauge) countSeriesDemand() int { return 0 }
type testHistogram struct {
nameSum string
nameCount string
nameBucket string
buckets []float64
registry *TestRegistry
histogramOverrides HistogramMode
}
var (
_ Histogram = (*testHistogram)(nil)
_ metric = (*testHistogram)(nil)
)
func (t *testHistogram) ObserveWithExemplar(lbls labels.Labels, value float64, _ string, multiplier float64) {
t.registry.addToMetric(t.nameCount, lbls, 1*multiplier)
t.registry.addToMetric(t.nameSum, lbls, value*multiplier)
for _, bucket := range t.buckets {
if value <= bucket {
t.registry.addToMetric(t.nameBucket, withLe(lbls, bucket), 1*multiplier)
}
}
t.registry.addToMetric(t.nameBucket, withLe(lbls, math.Inf(1)), 1*multiplier)
}
func (t *testHistogram) name() string {
panic("implement me")
}
func withLe(lbls labels.Labels, le float64) labels.Labels {
lb := labels.NewBuilder(lbls)
lb.Set(labels.BucketLabel, formatFloat(le))
return lb.Labels()
}
func (t *testHistogram) collectMetrics(_ storage.Appender, _ int64) error {
panic("implement me")
}
func (t *testHistogram) removeStaleSeries(int64) {
panic("implement me")
}
func (t *testHistogram) countActiveSeries() int {
return 0
}
// countSeriesDemand is a stub to satisfy optional estimator usage in registry.
// Test registry does not track estimates, so return 0.
func (t *testHistogram) countSeriesDemand() int { return 0 }