Debian Codegraph @

internal/global/meter_types_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package global // import "go.opentelemetry.io/otel/internal/global"
5
6 import (
7 "context"
8
9 "go.opentelemetry.io/otel/metric"
10 "go.opentelemetry.io/otel/metric/embedded"
11 )
12
13 type testMeterProvider struct {
14 embedded.MeterProvider
15
16 count int
17 }
18
19 func (p *testMeterProvider) Meter(string, ...metric.MeterOption) metric.Meter {
20 p.count++
21 return &testMeter{}
22 }
23
24 type testMeter struct {
25 embedded.Meter
26
27 afCount int
28 afUDCount int
29 afGauge int
30
31 aiCount int
32 aiUDCount int
33 aiGauge int
34
35 sfCount int
36 sfUDCount int
37 sfHist int
38 sfGauge int
39
40 siCount int
41 siUDCount int
42 siHist int
43 siGauge int
44
45 callbacks []metric.Callback
46 }
47
48 func (m *testMeter) Int64Counter(string, ...metric.Int64CounterOption) (metric.Int64Counter, error) {
49 m.siCount++
50 return &testCountingIntInstrument{}, nil
51 }
52
53 func (m *testMeter) Int64UpDownCounter(
54 string,
55 ...metric.Int64UpDownCounterOption,
56 ) (metric.Int64UpDownCounter, error) {
57 m.siUDCount++
58 return &testCountingIntInstrument{}, nil
59 }
60
61 func (m *testMeter) Int64Histogram(string, ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
62 m.siHist++
63 return &testCountingIntInstrument{}, nil
64 }
65
66 func (m *testMeter) Int64Gauge(string, ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
67 m.siGauge++
68 return &testCountingIntInstrument{}, nil
69 }
70
71 func (m *testMeter) Int64ObservableCounter(
72 string,
73 ...metric.Int64ObservableCounterOption,
74 ) (metric.Int64ObservableCounter, error) {
75 m.aiCount++
76 return &testCountingIntInstrument{}, nil
77 }
78
79 func (m *testMeter) Int64ObservableUpDownCounter(
80 string,
81 ...metric.Int64ObservableUpDownCounterOption,
82 ) (metric.Int64ObservableUpDownCounter, error) {
83 m.aiUDCount++
84 return &testCountingIntInstrument{}, nil
85 }
86
87 func (m *testMeter) Int64ObservableGauge(
88 string,
89 ...metric.Int64ObservableGaugeOption,
90 ) (metric.Int64ObservableGauge, error) {
91 m.aiGauge++
92 return &testCountingIntInstrument{}, nil
93 }
94
95 func (m *testMeter) Float64Counter(string, ...metric.Float64CounterOption) (metric.Float64Counter, error) {
96 m.sfCount++
97 return &testCountingFloatInstrument{}, nil
98 }
99
100 func (m *testMeter) Float64UpDownCounter(
101 string,
102 ...metric.Float64UpDownCounterOption,
103 ) (metric.Float64UpDownCounter, error) {
104 m.sfUDCount++
105 return &testCountingFloatInstrument{}, nil
106 }
107
108 func (m *testMeter) Float64Histogram(
109 string,
110 ...metric.Float64HistogramOption,
111 ) (metric.Float64Histogram, error) {
112 m.sfHist++
113 return &testCountingFloatInstrument{}, nil
114 }
115
116 func (m *testMeter) Float64Gauge(string, ...metric.Float64GaugeOption) (metric.Float64Gauge, error) {
117 m.sfGauge++
118 return &testCountingFloatInstrument{}, nil
119 }
120
121 func (m *testMeter) Float64ObservableCounter(
122 string,
123 ...metric.Float64ObservableCounterOption,
124 ) (metric.Float64ObservableCounter, error) {
125 m.afCount++
126 return &testCountingFloatInstrument{}, nil
127 }
128
129 func (m *testMeter) Float64ObservableUpDownCounter(
130 string,
131 ...metric.Float64ObservableUpDownCounterOption,
132 ) (metric.Float64ObservableUpDownCounter, error) {
133 m.afUDCount++
134 return &testCountingFloatInstrument{}, nil
135 }
136
137 func (m *testMeter) Float64ObservableGauge(
138 string,
139 ...metric.Float64ObservableGaugeOption,
140 ) (metric.Float64ObservableGauge, error) {
141 m.afGauge++
142 return &testCountingFloatInstrument{}, nil
143 }
144
145 // RegisterCallback captures the function that will be called during Collect.
146 func (m *testMeter) RegisterCallback(f metric.Callback, _ ...metric.Observable) (metric.Registration, error) {
147 m.callbacks = append(m.callbacks, f)
148 return testReg{
149 f: func(idx int) func() {
150 return func() { m.callbacks[idx] = nil }
151 }(len(m.callbacks) - 1),
152 }, nil
153 }
154
155 type testReg struct {
156 embedded.Registration
157
158 f func()
159 }
160
161 func (r testReg) Unregister() error {
162 r.f()
163 return nil
164 }
165
166 // This enables async collection.
167 func (m *testMeter) collect() {
168 ctx := context.Background()
169 o := observationRecorder{ctx: ctx}
170 for _, f := range m.callbacks {
171 if f == nil {
172 // Unregister.
173 continue
174 }
175 _ = f(ctx, o)
176 }
177 }
178
179 type observationRecorder struct {
180 embedded.Observer
181
182 ctx context.Context
183 }
184
185 func (observationRecorder) ObserveFloat64(i metric.Float64Observable, _ float64, _ ...metric.ObserveOption) {
186 iImpl, ok := i.(*testCountingFloatInstrument)
187 if ok {
188 iImpl.observe()
189 }
190 }
191
192 func (observationRecorder) ObserveInt64(i metric.Int64Observable, _ int64, _ ...metric.ObserveOption) {
193 iImpl, ok := i.(*testCountingIntInstrument)
194 if ok {
195 iImpl.observe()
196 }
197 }