8000 Fix callback registration bug (#4888) · open-telemetry/opentelemetry-go@11ebd19 · GitHub
[go: up one dir, main page]

Skip to content

Commit 11ebd19

Browse files
authored
Fix callback registration bug (#4888)
* Fix cback reg bug Register all callbacks not just the last passed. * Add changelog entry
1 parent e3eb3f7 commit 11ebd19

File tree

3 files changed

+98
-40
lines changed

3 files changed

+98
-40
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
88

99
## [Unreleased]
1010

11+
### Fixed
12+
13+
- Register all callbacks passed during observable instrument creation instead of just the last one multiple times in `go.opentelemetry.io/otel/sdk/metric`. (#4888)
14+
1115
## [1.23.0] 2024-02-06
1216

1317
This release contains the first stable, `v1`, release of the following modules:

sdk/metric/meter.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ func (m *meter) int64ObservableInstrument(id Instrument, callbacks []metric.Int6
148148
inst.appendMeasures(in)
149149
for _, cback := range callbacks {
150150
inst := int64Observer{measures: in}
151-
insert.addCallback(func(ctx context.Context) error { return cback(ctx, inst) })
151+
fn := cback
152+
insert.addCallback(func(ctx context.Context) error { return fn(ctx, inst) })
152153
}
153154
}
154155
return inst, validateInstrumentName(id.Name)
@@ -281,7 +282,8 @@ func (m *meter) float64ObservableInstrument(id Instrument, callbacks []metric.Fl
281282
inst.appendMeasures(in)
282283
for _, cback := range callbacks {
283284
inst := float64Observer{measures: in}
284-
insert.addCallback(func(ctx context.Context) error { return cback(ctx, inst) })
285+
fn := cback
286+
insert.addCallback(func(ctx context.Context) error { return fn(ctx, inst) })
285287
}
286288
}
287289
return inst, validateInstrumentName(id.Name)

sdk/metric/meter_test.go

Lines changed: 90 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,18 @@ func TestMeterCreatesInstruments(t *testing.T) {
174174
ctx, cancel := context.WithCancel(context.Background())
175175
cancel()
176176

177-
attrs := attribute.NewSet(attribute.String("name", "alice"))
178-
opt := metric.WithAttributeSet(attrs)
177+
alice := attribute.NewSet(
178+
attribute.String("name", "Alice"),
179+
attribute.Bool("admin", true),
180+
)
181+
optAlice := metric.WithAttributeSet(alice)
182+
183+
bob := attribute.NewSet(
184+
attribute.String("name", "Bob"),
185+
attribute.Bool("admin", false),
186+
)
187+
optBob := metric.WithAttributeSet(bob)
188+
179189
testCases := []struct {
180190
name string
181191
fn func(*testing.T, metric.Meter)
@@ -184,11 +194,17 @@ func TestMeterCreatesInstruments(t *testing.T) {
184194
{
185195
name: "ObservableInt64Count",
186196
fn: func(t *testing.T, m metric.Meter) {
187-
cback := func(_ context.Context, o metric.Int64Observer) error {
188-
o.Observe(4, opt)
189-
return nil
190-
}
191-
ctr, err := m.Int64ObservableCounter("aint", metric.WithInt64Callback(cback))
197+
ctr, err := m.Int64ObservableCounter(
198+
"aint",
199+
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
200+
o.Observe(4, optAlice)
201+
return nil
202+
}),
203+
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
204+
o.Observe(5, optBob)
205+
return nil
206+
}),
207+
)
192208
assert.NoError(t, err)
193209
_, err = m.RegisterCallback(func(_ context.Context, o metric.Observer) error {
194210
o.ObserveInt64(ctr, 3)
@@ -202,7 +218,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
202218
Temporality: metricdata.CumulativeTemporality,
203219
IsMonotonic: true,
204220
DataPoints: []metricdata.DataPoint[int64]{
205-
{Attributes: attrs, Value: 4},
221+
{Attributes: alice, Value: 4},
222+
{Attributes: bob, Value: 5},
206223
{Value: 3},
207224
},
208225
},
@@ -211,11 +228,17 @@ func TestMeterCreatesInstruments(t *testing.T) {
211228
{
212229
name: "ObservableInt64UpDownCount",
213230
fn: func(t *testing.T, m metric.Meter) {
214-
cback := func(_ context.Context, o metric.Int64Observer) error {
215-
o.Observe(4, opt)
216-
return nil
217-
}
218-
ctr, err := m.Int64ObservableUpDownCounter("aint", metric.WithInt64Callback(cback))
231+
ctr, err := m.Int64ObservableUpDownCounter(
232+
"aint",
233+
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
234+
o.Observe(4, optAlice)
235+
return nil
236+
}),
237+
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
238+
o.Observe(5, optBob)
239+
return nil
240+
}),
241+
)
219242
assert.NoError(t, err)
220243
_, err = m.RegisterCallback(func(_ context.Context, o metric.Observer) error {
221244
o.ObserveInt64(ctr, 11)
@@ -229,7 +252,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
229252
Temporality: metricdata.CumulativeTemporality,
230253
IsMonotonic: false,
231254
DataPoints: []metricdata.DataPoint[int64]{
232-
{Attributes: attrs, Value: 4},
255+
{Attributes: alice, Value: 4},
256+
{Attributes: bob, Value: 5},
233257
{Value: 11},
234258
},
235259
},
@@ -238,11 +262,17 @@ func TestMeterCreatesInstruments(t *testing.T) {
238262
{
239263
name: "ObservableInt64Gauge",
240264
fn: func(t *testing.T, m metric.Meter) {
241-
cback := func(_ context.Context, o metric.Int64Observer) error {
242-
o.Observe(4, opt)
243-
return nil
244-
}
245-
gauge, err := m.Int64ObservableGauge("agauge", metric.WithInt64Callback(cback))
265+
gauge, err := m.Int64ObservableGauge(
266+
"agauge",
267+
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
268+
o.Observe(4, optAlice)
269+
return nil
270+
}),
271+
metric.WithInt64Callback(func(_ context.Context, o metric.Int64Observer) error {
272+
o.Observe(5, optBob)
273+
return nil
274+
}),
275+
)
246276
assert.NoError(t, err)
247277
_, err = m.RegisterCallback(func(_ context.Context, o metric.Observer) error {
248278
o.ObserveInt64(gauge, 11)
@@ -254,7 +284,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
254284
Name: "agauge",
255285
Data: metricdata.Gauge[int64]{
256286
DataPoints: []metricdata.DataPoint[int64]{
257-
{Attributes: attrs, Value: 4},
287+
{Attributes: alice, Value: 4},
288+
{Attributes: bob, Value: 5},
258289
{Value: 11},
259290
},
260291
},
@@ -263,11 +294,17 @@ func TestMeterCreatesInstruments(t *testing.T) {
263294
{
264295
name: "ObservableFloat64Count",
265296
fn: func(t *testing.T, m metric.Meter) {
266-
cback := func(_ context.Context, o metric.Float64Observer) error {
267-
o.Observe(4, opt)
268-
return nil
269-
}
270-
ctr, err := m.Float64ObservableCounter("afloat", metric.WithFloat64Callback(cback))
297+
ctr, err := m.Float64ObservableCounter(
298+
"afloat",
299+
metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
300+
o.Observe(4, optAlice)
301+
return nil
302+
}),
303+
metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
304+
o.Observe(5, optBob)
305+
return nil
306+
}),
307+
)
271308
assert.NoError(t, err)
272309
_, err = m.RegisterCallback(func(_ context.Context, o metric.Observer) error {
273310
o.ObserveFloat64(ctr, 3)
@@ -281,7 +318,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
281318
Temporality: metricdata.CumulativeTemporality,
282319
IsMonotonic: true,
283320
DataPoints: []metricdata.DataPoint[float64]{
284-
{Attributes: attrs, Value: 4},
321+
{Attributes: alice, Value: 4},
322+
{Attributes: bob, Value: 5},
285323
{Value: 3},
286324
},
287325
},
@@ -290,11 +328,17 @@ func TestMeterCreatesInstruments(t *testing.T) {
290328
{
291329
name: "ObservableFloat64UpDownCount",
292330
fn: func(t *testing.T, m metric.Meter) {
293-
cback := func(_ context.Context, o metric.Float64Observer) error {
294-
o.Observe(4, opt)
295-
return nil
296-
}
297-
ctr, err := m.Float64ObservableUpDownCounter("afloat", metric.WithFloat64Callback(cback))
331+
ctr, err := m.Float64ObservableUpDownCounter(
332+
"afloat",
333+
metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
334+
o.Observe(4, optAlice)
335+
return nil
336+
}),
337+
metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
338+
o.Observe(5, optBob)
339+
return nil
340+
}),
341+
)
298342
assert.NoError(t, err)
299343
_, err = m.RegisterCallback(func(_ context.Context, o metric.Observer) error {
300344
o.ObserveFloat64(ctr, 11)
@@ -308,7 +352,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
308352
Temporality: metricdata.CumulativeTemporality,
309353
IsMonotonic: false,
310354
DataPoints: []metricdata.DataPoint[float64]{
311-
{Attributes: attrs, Value: 4},
355+
{Attributes: alice, Value: 4},
356+
{Attributes: bob, Value: 5},
312357
{Value: 11},
313358
},
314359
},
@@ -317,11 +362,17 @@ func TestMeterCreatesInstruments(t *testing.T) {
317362
{
318363
name: "ObservableFloat64Gauge",
319364
fn: func(t *testing.T, m metric.Meter) {
320-
cback := func(_ context.Context, o metric.Float64Observer) error {
321-
o.Observe(4, opt)
322-
return nil
323-
}
324-
gauge, err := m.Float64ObservableGauge("agauge", metric.WithFloat64Callback(cback))
365+
gauge, err := m.Float64ObservableGauge(
366+
"agauge",
367+
metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
368+
o.Observe(4, optAlice)
369+
return nil
370+
}),
371+
metric.WithFloat64Callback(func(_ context.Context, o metric.Float64Observer) error {
372+
o.Observe(5, optBob)
373+
return nil
374+
}),
375+
)
325376
assert.NoError(t, err)
326377
_, err = m.RegisterCallback(func(_ context.Context, o metric.Observer) error {
327378
o.ObserveFloat64(gauge, 11)
@@ -333,7 +384,8 @@ func TestMeterCreatesInstruments(t *testing.T) {
333384
Name: "agauge",
334385
Data: metricdata.Gauge[float64]{
335386
DataPoints: []metricdata.DataPoint[float64]{
336-
{Attributes: attrs, Value: 4},
387+
{Attributes: alice, Value: 4},
388+
{Attributes: bob, Value: 5},
337389
{Value: 11},
338390
},
339391
},

0 commit comments

Comments
 (0)
0