Skip to content

Commit 44444c4

Browse files
authored
Merge branch 'main' into semconv-v1.27.0
2 parents 9bc3840 + 2578acc commit 44444c4

File tree

6 files changed

+325
-50
lines changed

6 files changed

+325
-50
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1818
- The `go.opentelemetry.io/otel/semconv/v1.27.0` package.
1919
The package contains semantic conventions from the `v1.27.0` version of the OpenTelemetry Semantic Conventions. (#5894)
2020

21+
### Fixed
22+
23+
- Global MeterProvider registration unwraps global instrument Observers, the undocumented Unwrap() methods are now private. (#5881)
24+
2125
<!-- Released section -->
2226
<!-- Don't change this section unless doing release -->
2327

+250
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
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+
}

internal/global/instruments.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// unwrapper unwraps to return the underlying instrument implementation.
1515
type unwrapper interface {
16-
Unwrap() metric.Observable
16+
unwrap() metric.Observable
1717
}
1818

1919
type afCounter struct {
@@ -40,7 +40,7 @@ func (i *afCounter) setDelegate(m metric.Meter) {
4040
i.delegate.Store(ctr)
4141
}
4242

43-
func (i *afCounter) Unwrap() metric.Observable {
43+
func (i *afCounter) unwrap() metric.Observable {
4444
if ctr := i.delegate.Load(); ctr != nil {
4545
return ctr.(metric.Float64ObservableCounter)
4646
}
@@ -71,7 +71,7 @@ func (i *afUpDownCounter) setDelegate(m metric.Meter) {
7171
i.delegate.Store(ctr)
7272
}
7373

74-
func (i *afUpDownCounter) Unwrap() metric.Observable {
74+
func (i *afUpDownCounter) unwrap() metric.Observable {
7575
if ctr := i.delegate.Load(); ctr != nil {
7676
return ctr.(metric.Float64ObservableUpDownCounter)
7777
}
@@ -102,7 +102,7 @@ func (i *afGauge) setDelegate(m metric.Meter) {
102102
i.delegate.Store(ctr)
103103
}
104104

105-
func (i *afGauge) Unwrap() metric.Observable {
105+
func (i *afGauge) unwrap() metric.Observable {
106106
if ctr := i.delegate.Load(); ctr != nil {
107107
return ctr.(metric.Float64ObservableGauge)
108108
}
@@ -133,7 +133,7 @@ func (i *aiCounter) setDelegate(m metric.Meter) {
133133
i.delegate.Store(ctr)
134134
}
135135

136-
func (i *aiCounter) Unwrap() metric.Observable {
136+
func (i *aiCounter) unwrap() metric.Observable {
137137
if ctr := i.delegate.Load(); ctr != nil {
138138
return ctr.(metric.Int64ObservableCounter)
139139
}
@@ -164,7 +164,7 @@ func (i *aiUpDownCounter) setDelegate(m metric.Meter) {
164164
i.delegate.Store(ctr)
165165
}
166166

167-
func (i *aiUpDownCounter) Unwrap() metric.Observable {
167+
func (i *aiUpDownCounter) unwrap() metric.Observable {
168168
if ctr := i.delegate.Load(); ctr != nil {
169169
return ctr.(metric.Int64ObservableUpDownCounter)
170170
}
@@ -195,7 +195,7 @@ func (i *aiGauge) setDelegate(m metric.Meter) {
195195
i.delegate.Store(ctr)
196196
}
197197

198-
func (i *aiGauge) Unwrap() metric.Observable {
198+
func (i *aiGauge) unwrap() metric.Observable {
199199
if ctr := i.delegate.Load(); ctr != nil {
200200
return ctr.(metric.Int64ObservableGauge)
201201
}

internal/global/instruments_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ func TestAsyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
5757
t.Run("Float64", func(t *testing.T) {
5858
t.Run("Counter", func(t *testing.T) {
5959
delegate := &afCounter{}
60-
f := func(float64) { _ = delegate.Unwrap() }
60+
f := func(float64) { _ = delegate.unwrap() }
6161
testFloat64ConcurrentSafe(f, delegate.setDelegate)
6262
})
6363

6464
t.Run("UpDownCounter", func(t *testing.T) {
6565
delegate := &afUpDownCounter{}
66-
f := func(float64) { _ = delegate.Unwrap() }
66+
f := func(float64) { _ = delegate.unwrap() }
6767
testFloat64ConcurrentSafe(f, delegate.setDelegate)
6868
})
6969

7070
t.Run("Gauge", func(t *testing.T) {
7171
delegate := &afGauge{}
72-
f := func(float64) { _ = delegate.Unwrap() }
72+
f := func(float64) { _ = delegate.unwrap() }
7373
testFloat64ConcurrentSafe(f, delegate.setDelegate)
7474
})
7575
})
@@ -79,19 +79,19 @@ func TestAsyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
7979
t.Run("Int64", func(t *testing.T) {
8080
t.Run("Counter", func(t *testing.T) {
8181
delegate := &aiCounter{}
82-
f := func(int64) { _ = delegate.Unwrap() }
82+
f := func(int64) { _ = delegate.unwrap() }
8383
testInt64ConcurrentSafe(f, delegate.setDelegate)
8484
})
8585

8686
t.Run("UpDownCounter", func(t *testing.T) {
8787
delegate := &aiUpDownCounter{}
88-
f := func(int64) { _ = delegate.Unwrap() }
88+
f := func(int64) { _ = delegate.unwrap() }
8989
testInt64ConcurrentSafe(f, delegate.setDelegate)
9090
})
9191

9292
t.Run("Gauge", func(t *testing.T) {
9393
delegate := &aiGauge{}
94-
f := func(int64) { _ = delegate.Unwrap() }
94+
f := func(int64) { _ = delegate.unwrap() }
9595
testInt64ConcurrentSafe(f, delegate.setDelegate)
9696
})
9797
})

0 commit comments

Comments
 (0)