| 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 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/metric" |
| 13 |
"go.opentelemetry.io/otel/metric/embedded" |
| 14 |
"go.opentelemetry.io/otel/metric/noop" |
| 15 |
) |
| 16 |
|
| 17 |
// Below, an alternate meter provider is constructed specifically to |
| 18 |
// test the asynchronous instrument path. The alternative SDK uses |
| 19 |
// no-op implementations for its synchronous instruments, and the six |
| 20 |
// asynchronous instrument types are created here to test that |
| 21 |
// instruments and callbacks are unwrapped inside this library. |
| 22 |
|
| 23 |
type altMeterProvider struct { |
| 24 |
t *testing.T |
| 25 |
meters []*altMeter |
| 26 |
embedded.MeterProvider |
| 27 |
} |
| 28 |
|
| 29 |
var _ metric.MeterProvider = &altMeterProvider{} |
| 30 |
|
| 31 |
func (amp *altMeterProvider) Meter(string, ...metric.MeterOption) metric.Meter { |
| 32 |
am := &altMeter{ |
| 33 |
provider: amp, |
| 34 |
} |
| 35 |
amp.meters = append(amp.meters, am) |
| 36 |
return am |
| 37 |
} |
| 38 |
|
| 39 |
type altMeter struct { |
| 40 |
provider *altMeterProvider |
| 41 |
cbs []metric.Callback |
| 42 |
embedded.Meter |
| 43 |
} |
| 44 |
|
| 45 |
var _ metric.Meter = &altMeter{} |
| 46 |
|
| 47 |
type testAiCounter struct { |
| 48 |
meter *altMeter |
| 49 |
embedded.Int64ObservableCounter |
| 50 |
metric.Int64Observable |
| 51 |
} |
| 52 |
|
| 53 |
var _ metric.Int64ObservableCounter = &testAiCounter{} |
| 54 |
|
| 55 |
type testAfCounter struct { |
| 56 |
meter *altMeter |
| 57 |
embedded.Float64ObservableCounter |
| 58 |
metric.Float64Observable |
| 59 |
} |
| 60 |
|
| 61 |
var _ metric.Float64ObservableCounter = &testAfCounter{} |
| 62 |
|
| 63 |
type testAiUpDownCounter struct { |
| 64 |
meter *altMeter |
| 65 |
embedded.Int64ObservableUpDownCounter |
| 66 |
metric.Int64Observable |
| 67 |
} |
| 68 |
|
| 69 |
var _ metric.Int64ObservableUpDownCounter = &testAiUpDownCounter{} |
| 70 |
|
| 71 |
type testAfUpDownCounter struct { |
| 72 |
meter *altMeter |
| 73 |
embedded.Float64ObservableUpDownCounter |
| 74 |
metric.Float64Observable |
| 75 |
} |
| 76 |
|
| 77 |
var _ metric.Float64ObservableUpDownCounter = &testAfUpDownCounter{} |
| 78 |
|
| 79 |
type testAiGauge struct { |
| 80 |
meter *altMeter |
| 81 |
embedded.Int64ObservableGauge |
| 82 |
metric.Int64Observable |
| 83 |
} |
| 84 |
|
| 85 |
var _ metric.Int64ObservableGauge = &testAiGauge{} |
| 86 |
|
| 87 |
type testAfGauge struct { |
| 88 |
meter *altMeter |
| 89 |
embedded.Float64ObservableGauge |
| 90 |
metric.Float64Observable |
| 91 |
} |
| 92 |
|
| 93 |
var _ metric.Float64ObservableGauge = &testAfGauge{} |
| 94 |
|
| 95 |
type altRegistration struct { |
| 96 |
cb metric.Callback |
| 97 |
embedded.Registration |
| 98 |
} |
| 99 |
|
| 100 |
type altObserver struct { |
| 101 |
t *testing.T |
| 102 |
embedded.Observer |
| 103 |
} |
| 104 |
|
| 105 |
func (*altRegistration) Unregister() error { |
| 106 |
return nil |
| 107 |
} |
| 108 |
|
| 109 |
func (*altMeter) Int64Counter(name string, _ ...metric.Int64CounterOption) (metric.Int64Counter, error) { |
| 110 |
return noop.NewMeterProvider().Meter("noop").Int64Counter(name) |
| 111 |
} |
| 112 |
|
| 113 |
func (*altMeter) Int64UpDownCounter( |
| 114 |
name string, |
| 115 |
_ ...metric.Int64UpDownCounterOption, |
| 116 |
) (metric.Int64UpDownCounter, error) { |
| 117 |
return noop.NewMeterProvider().Meter("noop").Int64UpDownCounter(name) |
| 118 |
} |
| 119 |
|
| 120 |
func (*altMeter) Int64Histogram(name string, _ ...metric.Int64HistogramOption) (metric.Int64Histogram, error) { |
| 121 |
return noop.NewMeterProvider().Meter("noop").Int64Histogram(name) |
| 122 |
} |
| 123 |
|
| 124 |
func (*altMeter) Int64Gauge(name string, _ ...metric.Int64GaugeOption) (metric.Int64Gauge, error) { |
| 125 |
return noop.NewMeterProvider().Meter("noop").Int64Gauge(name) |
| 126 |
} |
| 127 |
|
| 128 |
func (am *altMeter) Int64ObservableCounter( |
| 129 |
string, |
| 130 |
...metric.Int64ObservableCounterOption, |
| 131 |
) (metric.Int64ObservableCounter, error) { |
| 132 |
return &testAiCounter{ |
| 133 |
meter: am, |
| 134 |
}, nil |
| 135 |
} |
| 136 |
|
| 137 |
func (am *altMeter) Int64ObservableUpDownCounter( |
| 138 |
string, |
| 139 |
...metric.Int64ObservableUpDownCounterOption, |
| 140 |
) (metric.Int64ObservableUpDownCounter, error) { |
| 141 |
return &testAiUpDownCounter{ |
| 142 |
meter: am, |
| 143 |
}, nil |
| 144 |
} |
| 145 |
|
| 146 |
func (am *altMeter) Int64ObservableGauge( |
| 147 |
string, |
| 148 |
...metric.Int64ObservableGaugeOption, |
| 149 |
) (metric.Int64ObservableGauge, error) { |
| 150 |
return &testAiGauge{ |
| 151 |
meter: am, |
| 152 |
}, nil |
| 153 |
} |
| 154 |
|
| 155 |
func (*altMeter) Float64Counter(name string, _ ...metric.Float64CounterOption) (metric.Float64Counter, error) { |
| 156 |
return noop.NewMeterProvider().Meter("noop").Float64Counter(name) |
| 157 |
} |
| 158 |
|
| 159 |
func (*altMeter) Float64UpDownCounter( |
| 160 |
name string, |
| 161 |
_ ...metric.Float64UpDownCounterOption, |
| 162 |
) (metric.Float64UpDownCounter, error) { |
| 163 |
return noop.NewMeterProvider().Meter("noop").Float64UpDownCounter(name) |
| 164 |
} |
| 165 |
|
| 166 |
func (*altMeter) Float64Histogram( |
| 167 |
name string, |
| 168 |
_ ...metric.Float64HistogramOption, |
| 169 |
) (metric.Float64Histogram, error) { |
| 170 |
return noop.NewMeterProvider().Meter("noop").Float64Histogram(name) |
| 171 |
} |
| 172 |
|
| 173 |
func (*altMeter) Float64Gauge(name string, _ ...metric.Float64GaugeOption) (metric.Float64Gauge, error) { |
| 174 |
return noop.NewMeterProvider().Meter("noop").Float64Gauge(name) |
| 175 |
} |
| 176 |
|
| 177 |
func (am *altMeter) Float64ObservableCounter( |
| 178 |
string, |
| 179 |
...metric.Float64ObservableCounterOption, |
| 180 |
) (metric.Float64ObservableCounter, error) { |
| 181 |
return &testAfCounter{ |
| 182 |
meter: am, |
| 183 |
}, nil |
| 184 |
} |
| 185 |
|
| 186 |
func (am *altMeter) Float64ObservableUpDownCounter( |
| 187 |
string, |
| 188 |
...metric.Float64ObservableUpDownCounterOption, |
| 189 |
) (metric.Float64ObservableUpDownCounter, error) { |
| 190 |
return &testAfUpDownCounter{ |
| 191 |
meter: am, |
| 192 |
}, nil |
| 193 |
} |
| 194 |
|
| 195 |
func (am *altMeter) Float64ObservableGauge( |
| 196 |
string, |
| 197 |
...metric.Float64ObservableGaugeOption, |
| 198 |
) (metric.Float64ObservableGauge, error) { |
| 199 |
return &testAfGauge{ |
| 200 |
meter: am, |
| 201 |
}, nil |
| 202 |
} |
| 203 |
|
| 204 |
func (am *altMeter) RegisterCallback(f metric.Callback, instruments ...metric.Observable) (metric.Registration, error) { |
| 205 |
for _, inst := range instruments { |
| 206 |
switch inst.(type) { |
| 207 |
case *testAiCounter, *testAfCounter, |
| 208 |
*testAiUpDownCounter, *testAfUpDownCounter, |
| 209 |
*testAiGauge, *testAfGauge: |
| 210 |
// OK! |
| 211 |
default: |
| 212 |
am.provider.t.Errorf("unexpected type %T", inst) |
| 213 |
} |
| 214 |
} |
| 215 |
am.cbs = append(am.cbs, f) |
| 216 |
return &altRegistration{cb: f}, nil |
| 217 |
} |
| 218 |
|
| 219 |
func (ao *altObserver) ObserveFloat64(inst metric.Float64Observable, _ float64, _ ...metric.ObserveOption) { |
| 220 |
ao.observe(inst) |
| 221 |
} |
| 222 |
|
| 223 |
func (ao *altObserver) ObserveInt64(inst metric.Int64Observable, _ int64, _ ...metric.ObserveOption) { |
| 224 |
ao.observe(inst) |
| 225 |
} |
| 226 |
|
| 227 |
func (ao *altObserver) observe(inst any) { |
| 228 |
switch inst.(type) { |
| 229 |
case *testAiCounter, *testAfCounter, |
| 230 |
*testAiUpDownCounter, *testAfUpDownCounter, |
| 231 |
*testAiGauge, *testAfGauge: |
| 232 |
// OK! |
| 233 |
default: |
| 234 |
ao.t.Errorf("unexpected type %T", inst) |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
func TestMeterDelegation(t *testing.T) { |
| 239 |
ResetForTest(t) |
| 240 |
|
| 241 |
amp := &altMeterProvider{t: t} |
| 242 |
|
| 243 |
gm := MeterProvider().Meter("test") |
| 244 |
aic, err := gm.Int64ObservableCounter("test_counter_i") |
| 245 |
require.NoError(t, err) |
| 246 |
afc, err := gm.Float64ObservableCounter("test_counter_f") |
| 247 |
require.NoError(t, err) |
| 248 |
aiu, err := gm.Int64ObservableUpDownCounter("test_updowncounter_i") |
| 249 |
require.NoError(t, err) |
| 250 |
afu, err := gm.Float64ObservableUpDownCounter("test_updowncounter_f") |
| 251 |
require.NoError(t, err) |
| 252 |
aig, err := gm.Int64ObservableGauge("test_gauge_i") |
| 253 |
require.NoError(t, err) |
| 254 |
afg, err := gm.Float64ObservableGauge("test_gauge_f") |
| 255 |
require.NoError(t, err) |
| 256 |
|
| 257 |
_, err = gm.RegisterCallback(func(_ context.Context, obs metric.Observer) error { |
| 258 |
obs.ObserveInt64(aic, 10) |
| 259 |
obs.ObserveFloat64(afc, 10) |
| 260 |
obs.ObserveInt64(aiu, 10) |
| 261 |
obs.ObserveFloat64(afu, 10) |
| 262 |
obs.ObserveInt64(aig, 10) |
| 263 |
obs.ObserveFloat64(afg, 10) |
| 264 |
return nil |
| 265 |
}, aic, afc, aiu, afu, aig, afg) |
| 266 |
require.NoError(t, err) |
| 267 |
|
| 268 |
SetMeterProvider(amp) |
| 269 |
|
| 270 |
ctx := t.Context() |
| 271 |
ao := &altObserver{t: t} |
| 272 |
for _, meter := range amp.meters { |
| 273 |
for _, cb := range meter.cbs { |
| 274 |
require.NoError(t, cb(ctx, ao)) |
| 275 |
} |
| 276 |
} |
| 277 |
} |