|
| 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(name string, opts ...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 (am *altMeter) Int64Counter(name string, _ ...metric.Int64CounterOption) (metric.Int64Counter, error) { |
| 110 | + return noop.NewMeterProvider().Meter("noop").Int64Counter(name) |
| 111 | +} |
| 112 | + |
| 113 | +func (am *altMeter) Int64UpDownCounter(name string, _ ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) { |
| 114 | + return noop.NewMeterProvider().Meter("noop").Int64UpDownCounter(name) |
| 115 | +} |
| 116 | + |
| 117 | +func (am *altMeter) Int64Histogram(name string, _ ...metric.Int64HistogramOption) (metric.Int64Histogram, error) { |
| 118 | + return noop.NewMeterProvider().Meter("noop").Int64Histogram(name) |
| 119 | +} |
| 120 | + |
| 121 | +func (am *altMeter) Int64Gauge(name string, _ ...metric.Int64GaugeOption) (metric.Int64Gauge, error) { |
| 122 | + return noop.NewMeterProvider().Meter("noop").Int64Gauge(name) |
| 123 | +} |
| 124 | + |
| 125 | +func (am *altMeter) Int64ObservableCounter(name string, options ...metric.Int64ObservableCounterOption) (metric.Int64ObservableCounter, error) { |
| 126 | + return &testAiCounter{ |
| 127 | + meter: am, |
| 128 | + }, nil |
| 129 | +} |
| 130 | + |
| 131 | +func (am *altMeter) Int64ObservableUpDownCounter(name string, options ...metric.Int64ObservableUpDownCounterOption) (metric.Int64ObservableUpDownCounter, error) { |
| 132 | + return &testAiUpDownCounter{ |
| 133 | + meter: am, |
| 134 | + }, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (am *altMeter) Int64ObservableGauge(name string, options ...metric.Int64ObservableGaugeOption) (metric.Int64ObservableGauge, error) { |
| 138 | + return &testAiGauge{ |
| 139 | + meter: am, |
| 140 | + }, nil |
| 141 | +} |
| 142 | + |
| 143 | +func (am *altMeter) Float64Counter(name string, _ ...metric.Float64CounterOption) (metric.Float64Counter, error) { |
| 144 | + return noop.NewMeterProvider().Meter("noop").Float64Counter(name) |
| 145 | +} |
| 146 | + |
| 147 | +func (am *altMeter) Float64UpDownCounter(name string, _ ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) { |
| 148 | + return noop.NewMeterProvider().Meter("noop").Float64UpDownCounter(name) |
| 149 | +} |
| 150 | + |
| 151 | +func (am *altMeter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) { |
| 152 | + return noop.NewMeterProvider().Meter("noop").Float64Histogram(name) |
| 153 | +} |
| 154 | + |
| 155 | +func (am *altMeter) Float64Gauge(name string, options ...metric.Float64GaugeOption) (metric.Float64Gauge, error) { |
| 156 | + return noop.NewMeterProvider().Meter("noop").Float64Gauge(name) |
| 157 | +} |
| 158 | + |
| 159 | +func (am *altMeter) Float64ObservableCounter(name string, options ...metric.Float64ObservableCounterOption) (metric.Float64ObservableCounter, error) { |
| 160 | + return &testAfCounter{ |
| 161 | + meter: am, |
| 162 | + }, nil |
| 163 | +} |
| 164 | + |
| 165 | +func (am *altMeter) Float64ObservableUpDownCounter(name string, options ...metric.Float64ObservableUpDownCounterOption) (metric.Float64ObservableUpDownCounter, error) { |
| 166 | + return &testAfUpDownCounter{ |
| 167 | + meter: am, |
| 168 | + }, nil |
| 169 | +} |
| 170 | + |
| 171 | +func (am *altMeter) Float64ObservableGauge(name string, options ...metric.Float64ObservableGaugeOption) (metric.Float64ObservableGauge, error) { |
| 172 | + return &testAfGauge{ |
| 173 | + meter: am, |
| 174 | + }, nil |
| 175 | +} |
| 176 | + |
| 177 | +func (am *altMeter) RegisterCallback(f metric.Callback, instruments ...metric.Observable) (metric.Registration, error) { |
| 178 | + for _, inst := range instruments { |
| 179 | + switch inst.(type) { |
| 180 | + case *testAiCounter, *testAfCounter, |
| 181 | + *testAiUpDownCounter, *testAfUpDownCounter, |
| 182 | + *testAiGauge, *testAfGauge: |
| 183 | + // OK! |
| 184 | + default: |
| 185 | + am.provider.t.Errorf("unexpected type %T", inst) |
| 186 | + } |
| 187 | + } |
| 188 | + am.cbs = append(am.cbs, f) |
| 189 | + return &altRegistration{cb: f}, nil |
| 190 | +} |
| 191 | + |
| 192 | +func (ao *altObserver) ObserveFloat64(inst metric.Float64Observable, _ float64, _ ...metric.ObserveOption) { |
| 193 | + ao.observe(inst) |
| 194 | +} |
| 195 | + |
| 196 | +func (ao *altObserver) ObserveInt64(inst metric.Int64Observable, _ int64, _ ...metric.ObserveOption) { |
| 197 | + ao.observe(inst) |
| 198 | +} |
| 199 | + |
| 200 | +func (ao *altObserver) observe(inst any) { |
| 201 | + switch inst.(type) { |
| 202 | + case *testAiCounter, *testAfCounter, |
| 203 | + *testAiUpDownCounter, *testAfUpDownCounter, |
| 204 | + *testAiGauge, *testAfGauge: |
| 205 | + // OK! |
| 206 | + default: |
| 207 | + ao.t.Errorf("unexpected type %T", inst) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func TestMeterDelegation(t *testing.T) { |
| 212 | + ResetForTest(t) |
| 213 | + |
| 214 | + amp := &altMeterProvider{t: t} |
| 215 | + |
| 216 | + gm := MeterProvider().Meter("test") |
| 217 | + aic, err := gm.Int64ObservableCounter("test_counter_i") |
| 218 | + require.NoError(t, err) |
| 219 | + afc, err := gm.Float64ObservableCounter("test_counter_f") |
| 220 | + require.NoError(t, err) |
| 221 | + aiu, err := gm.Int64ObservableUpDownCounter("test_updowncounter_i") |
| 222 | + require.NoError(t, err) |
| 223 | + afu, err := gm.Float64ObservableUpDownCounter("test_updowncounter_f") |
| 224 | + require.NoError(t, err) |
| 225 | + aig, err := gm.Int64ObservableGauge("test_gauge_i") |
| 226 | + require.NoError(t, err) |
| 227 | + afg, err := gm.Float64ObservableGauge("test_gauge_f") |
| 228 | + require.NoError(t, err) |
| 229 | + |
| 230 | + _, err = gm.RegisterCallback(func(_ context.Context, obs metric.Observer) error { |
| 231 | + obs.ObserveInt64(aic, 10) |
| 232 | + obs.ObserveFloat64(afc, 10) |
| 233 | + obs.ObserveInt64(aiu, 10) |
| 234 | + obs.ObserveFloat64(afu, 10) |
| 235 | + obs.ObserveInt64(aig, 10) |
| 236 | + obs.ObserveFloat64(afg, 10) |
| 237 | + return nil |
| 238 | + }, aic, afc, aiu, afu, aig, afg) |
| 239 | + require.NoError(t, err) |
| 240 | + |
| 241 | + SetMeterProvider(amp) |
| 242 | + |
| 243 | + ctx := context.Background() |
| 244 | + ao := &altObserver{t: t} |
| 245 | + for _, meter := range amp.meters { |
| 246 | + for _, cb := range meter.cbs { |
| 247 | + require.NoError(t, cb(ctx, ao)) |
| 248 | + } |
| 249 | + } |
| 250 | +} |
0 commit comments