forked from open-telemetry/opentelemetry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalt_sdk_test.go
More file actions
175 lines (137 loc) · 4.62 KB
/
alt_sdk_test.go
File metadata and controls
175 lines (137 loc) · 4.62 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package global // import "go.opentelemetry.io/otel/internal/global"
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/embedded"
)
type altMeterProvider struct {
t *testing.T
meters []*altMeter
embedded.MeterProvider
}
var _ metric.MeterProvider = &altMeterProvider{}
func (amp *altMeterProvider) Meter(name string, opts ...metric.MeterOption) metric.Meter {
am := &altMeter{
provider: amp,
}
amp.meters = append(amp.meters, am)
return am
}
type altMeter struct {
provider *altMeterProvider
cbs []metric.Callback
embedded.Meter
}
var _ metric.Meter = &altMeter{}
type testAsyncCounter struct {
meter *altMeter
embedded.Int64ObservableCounter
metric.Int64Observable
}
var _ metric.Int64ObservableCounter = &testAsyncCounter{}
type altRegistration struct {
cb metric.Callback
embedded.Registration
}
type altObserver struct {
t *testing.T
embedded.Observer
}
func (*altRegistration) Unregister() error {
return nil
}
func (am *altMeter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) {
return nil, nil
}
func (am *altMeter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) {
return nil, nil
}
func (am *altMeter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
return nil, nil
}
func (am *altMeter) Int64Gauge(name string, options ...metric.Int64GaugeOption) (metric.Int64Gauge, error) {
return nil, nil
}
func (am *altMeter) Int64ObservableCounter(name string, options ...metric.Int64ObservableCounterOption) (metric.Int64ObservableCounter, error) {
return &testAsyncCounter{
meter: am,
}, nil
}
func (am *altMeter) Int64ObservableUpDownCounter(name string, options ...metric.Int64ObservableUpDownCounterOption) (metric.Int64ObservableUpDownCounter, error) {
return nil, nil
}
func (am *altMeter) Int64ObservableGauge(name string, options ...metric.Int64ObservableGaugeOption) (metric.Int64ObservableGauge, error) {
return nil, nil
}
func (am *altMeter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) {
return nil, nil
}
func (am *altMeter) Float64UpDownCounter(name string, options ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) {
return nil, nil
}
func (am *altMeter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
return nil, nil
}
func (am *altMeter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) {
return nil, nil
}
func (am *altMeter) Float64ObservableCounter(name string, options ...metric.Float64ObservableCounterOption) (metric.Float64ObservableCounter, error) {
return nil, nil
}
func (am *altMeter) Float64ObservableUpDownCounter(name string, options ...metric.Float64ObservableUpDownCounterOption) (metric.Float64ObservableUpDownCounter, error) {
return nil, nil
}
func (am *altMeter) Float64ObservableGauge(name string, options ...metric.Float64ObservableGaugeOption) (metric.Float64ObservableGauge, error) {
// Note: The global delegation also breaks when we return nil in one of these!
return nil, nil
}
func (am *altMeter) RegisterCallback(f metric.Callback, instruments ...metric.Observable) (metric.Registration, error) {
for _, inst := range instruments {
switch inst.(type) {
case *testAsyncCounter:
// OK!
default:
am.provider.t.Errorf("unexpected type %T", inst)
}
}
am.cbs = append(am.cbs, f)
return &altRegistration{cb: f}, nil
}
func (ao *altObserver) ObserveFloat64(inst metric.Float64Observable, _ float64, _ ...metric.ObserveOption) {
ao.observe(inst)
}
func (ao *altObserver) ObserveInt64(inst metric.Int64Observable, _ int64, _ ...metric.ObserveOption) {
ao.observe(inst)
}
func (ao *altObserver) observe(inst any) {
switch inst.(type) {
case *testAsyncCounter:
// OK!
default:
ao.t.Errorf("unexpected type %T", inst)
}
}
func TestMeterDelegation(t *testing.T) {
ResetForTest(t)
amp := &altMeterProvider{t: t}
gm := MeterProvider().Meter("test")
ai, err := gm.Int64ObservableCounter("test_counter")
require.NoError(t, err)
_, err = gm.RegisterCallback(func(_ context.Context, obs metric.Observer) error {
obs.ObserveInt64(ai, 10)
return nil
}, ai)
require.NoError(t, err)
SetMeterProvider(amp)
ctx := context.Background()
ao := &altObserver{t: t}
for _, meter := range amp.meters {
for _, cb := range meter.cbs {
require.NoError(t, cb(ctx, ao))
}
}
}