Skip to content

Commit 222d4cf

Browse files
authoredMay 21, 2024··
Fix gauge metrics with latest otel SDK (#1482)
Fix gauge metrics with latest otel SDK
1 parent 06c0ebc commit 222d4cf

File tree

6 files changed

+80
-33
lines changed

6 files changed

+80
-33
lines changed
 

‎contrib/opentelemetry/go.mod

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ go 1.20
44

55
require (
66
github.com/stretchr/testify v1.9.0
7-
go.opentelemetry.io/otel v1.21.0
8-
go.opentelemetry.io/otel/sdk v1.21.0
9-
go.opentelemetry.io/otel/trace v1.21.0
7+
go.opentelemetry.io/otel v1.23.0
8+
go.opentelemetry.io/otel/sdk v1.23.0
9+
go.opentelemetry.io/otel/trace v1.23.0
1010
go.temporal.io/sdk v1.12.0
1111
)
1212

1313
require (
14-
github.com/go-logr/logr v1.3.0 // indirect
14+
github.com/go-logr/logr v1.4.1 // indirect
1515
github.com/go-logr/stdr v1.2.2 // indirect
1616
)
1717

@@ -27,8 +27,8 @@ require (
2727
github.com/pmezard/go-difflib v1.0.0 // indirect
2828
github.com/robfig/cron v1.2.0 // indirect
2929
github.com/stretchr/objx v0.5.2 // indirect
30-
go.opentelemetry.io/otel/metric v1.21.0
31-
go.opentelemetry.io/otel/sdk/metric v1.21.0
30+
go.opentelemetry.io/otel/metric v1.23.0
31+
go.opentelemetry.io/otel/sdk/metric v1.23.0
3232
go.temporal.io/api v1.32.0 // indirect
3333
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
3434
golang.org/x/net v0.24.0 // indirect

‎contrib/opentelemetry/go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
1717
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1818
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
1919
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
20+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
21+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
2022
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
2123
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
2224
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@@ -69,14 +71,24 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
6971
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
7072
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
7173
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
74+
go.opentelemetry.io/otel v1.23.0 h1:Df0pqjqExIywbMCMTxkAwzjLZtRf+bBKLbUcpxO2C9E=
75+
go.opentelemetry.io/otel v1.23.0/go.mod h1:YCycw9ZeKhcJFrb34iVSkyT0iczq/zYDtZYFufObyB0=
7276
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
7377
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
78+
go.opentelemetry.io/otel/metric v1.23.0 h1:pazkx7ss4LFVVYSxYew7L5I6qvLXHA0Ap2pwV+9Cnpo=
79+
go.opentelemetry.io/otel/metric v1.23.0/go.mod h1:MqUW2X2a6Q8RN96E2/nqNoT+z9BSms20Jb7Bbp+HiTo=
7480
go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZXQ8=
7581
go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E=
82+
go.opentelemetry.io/otel/sdk v1.23.0 h1:0KM9Zl2esnl+WSukEmlaAEjVY5HDZANOHferLq36BPc=
83+
go.opentelemetry.io/otel/sdk v1.23.0/go.mod h1:wUscup7byToqyKJSilEtMf34FgdCAsFpFOjXnAwFfO0=
7684
go.opentelemetry.io/otel/sdk/metric v1.21.0 h1:smhI5oD714d6jHE6Tie36fPx4WDFIg+Y6RfAY4ICcR0=
7785
go.opentelemetry.io/otel/sdk/metric v1.21.0/go.mod h1:FJ8RAsoPGv/wYMgBdUJXOm+6pzFY3YdljnXtv1SBE8Q=
86+
go.opentelemetry.io/otel/sdk/metric v1.23.0 h1:u81lMvmK6GMgN4Fty7K7S6cSKOZhMKJMK2TB+KaTs0I=
87+
go.opentelemetry.io/otel/sdk/metric v1.23.0/go.mod h1:2LUOToN/FdX6wtfpHybOnCZjoZ6ViYajJYMiJ1LKDtQ=
7888
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
7989
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
90+
go.opentelemetry.io/otel/trace v1.23.0 h1:37Ik5Ib7xfYVb4V1UtnT97T1jI+AoIYkJyPkuL4iJgI=
91+
go.opentelemetry.io/otel/trace v1.23.0/go.mod h1:GSGTbIClEsuZrGIzoEHqsVfxgn5UkggkflQwDScNUsk=
8092
go.temporal.io/api v1.32.0 h1:Jv0FieWDq0HJVqoHRE/kRHM+tIaRtR16RbXZZl+8Qb4=
8193
go.temporal.io/api v1.32.0/go.mod h1:MClRjMCgXZTKmxyItEJPRR5NuJRBhSEpuF9wuh97N6U=
8294
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=

‎contrib/opentelemetry/handler.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,20 @@ func (m MetricsHandler) Counter(name string) client.MetricsCounter {
130130
func (m MetricsHandler) Gauge(name string) client.MetricsGauge {
131131
// TODO(https://github.com/open-telemetry/opentelemetry-go/issues/3984) replace with sync gauge once supported
132132
var config atomic.Value
133-
config.Store(0.0)
134-
_, err := m.meter.Float64ObservableGauge(name,
135-
metric.WithFloat64Callback(func(ctx context.Context, o metric.Float64Observer) error {
136-
o.Observe(config.Load().(float64), metric.WithAttributeSet(m.attributes))
133+
og, err := m.meter.Float64ObservableGauge(name)
134+
if err != nil {
135+
m.onError(err)
136+
return client.MetricsNopHandler.Gauge(name)
137+
}
138+
_, err = m.meter.RegisterCallback(func(ctx context.Context, o metric.Observer) error {
139+
val := config.Load()
140+
if val == nil {
141+
// Skip observation if no value has been stored.
137142
return nil
138-
}))
143+
}
144+
o.ObserveFloat64(og, config.Load().(float64), metric.WithAttributeSet(m.attributes))
145+
return nil
146+
}, og)
139147
if err != nil {
140148
m.onError(err)
141149
return client.MetricsNopHandler.Gauge(name)

‎contrib/opentelemetry/handler_test.go

+33-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ func TestCounterHandler(t *testing.T) {
8282
Meter: meterProvider.Meter("test"),
8383
},
8484
)
85-
handlerWithTag := handler.WithTags(map[string]string{"tag1": "value1"})
8685
// Emit some values
87-
testCounter := handlerWithTag.Counter("testCounter")
86+
testCounter := handler.WithTags(map[string]string{"tag1": "value1"}).Counter("testCounter")
8887
testCounter.Inc(1)
8988
testCounter.Inc(1)
9089
testCounter.Inc(-1)
90+
// Emit some values with different tags
91+
testCounter2 := handler.WithTags(map[string]string{"tag1": "value2"}).Counter("testCounter")
92+
testCounter2.Inc(5)
9193
// Assert result
9294
var rm metricdata.ResourceMetrics
9395
metricReader.Collect(ctx, &rm)
@@ -104,6 +106,10 @@ func TestCounterHandler(t *testing.T) {
104106
Value: 1,
105107
Attributes: attribute.NewSet(attribute.String("tag1", "value1")),
106108
},
109+
{
110+
Value: 5,
111+
Attributes: attribute.NewSet(attribute.String("tag1", "value2")),
112+
},
107113
},
108114
},
109115
}
@@ -119,12 +125,18 @@ func TestGaugeHandler(t *testing.T) {
119125
Meter: meterProvider.Meter("test"),
120126
},
121127
)
122-
handlerWithTag := handler.WithTags(map[string]string{"tag1": "value1"})
128+
123129
// Emit some values
124-
testGauge := handlerWithTag.Gauge("testGauge")
130+
testGauge := handler.WithTags(map[string]string{"tag1": "value1"}).Gauge("testGauge")
125131
testGauge.Update(1)
126132
testGauge.Update(5)
127133
testGauge.Update(100)
134+
// Emit some values with different tags
135+
testGauge2 := handler.WithTags(map[string]string{"tag1": "value2"}).Gauge("testGauge")
136+
testGauge2.Update(1000)
137+
// Create a gauge, but don't set a value
138+
_ = handler.Gauge("testGaugeNoValue")
139+
128140
// Assert result
129141
var rm metricdata.ResourceMetrics
130142
metricReader.Collect(ctx, &rm)
@@ -139,6 +151,10 @@ func TestGaugeHandler(t *testing.T) {
139151
Value: 100,
140152
Attributes: attribute.NewSet(attribute.String("tag1", "value1")),
141153
},
154+
{
155+
Value: 1000,
156+
Attributes: attribute.NewSet(attribute.String("tag1", "value2")),
157+
},
142158
},
143159
},
144160
}
@@ -154,11 +170,13 @@ func TestTimerHandler(t *testing.T) {
154170
Meter: meterProvider.Meter("test"),
155171
},
156172
)
157-
handlerWithTag := handler.WithTags(map[string]string{"tag1": "value1"})
158-
testTimer := handlerWithTag.Timer("testTimer")
173+
testTimer := handler.WithTags(map[string]string{"tag1": "value1"}).Timer("testTimer")
159174
testTimer.Record(time.Millisecond)
160175
testTimer.Record(time.Second)
161176
testTimer.Record(time.Hour)
177+
// Emit some values with different tags
178+
testTimer2 := handler.WithTags(map[string]string{"tag1": "value2"}).Timer("testTimer")
179+
testTimer2.Record(time.Millisecond)
162180

163181
var rm metricdata.ResourceMetrics
164182
metricReader.Collect(ctx, &rm)
@@ -180,6 +198,15 @@ func TestTimerHandler(t *testing.T) {
180198
BucketCounts: []uint64{0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
181199
Attributes: attribute.NewSet(attribute.String("tag1", "value1")),
182200
},
201+
{
202+
Count: 1,
203+
Sum: 0.001,
204+
Min: metricdata.NewExtrema(time.Millisecond.Seconds()),
205+
Max: metricdata.NewExtrema(time.Millisecond.Seconds()),
206+
Bounds: []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000},
207+
BucketCounts: []uint64{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
208+
Attributes: attribute.NewSet(attribute.String("tag1", "value2")),
209+
},
183210
},
184211
},
185212
}

‎test/go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ require (
99
github.com/pborman/uuid v1.2.1
1010
github.com/stretchr/testify v1.9.0
1111
github.com/uber-go/tally/v4 v4.1.1
12-
go.opentelemetry.io/otel v1.21.0
13-
go.opentelemetry.io/otel/sdk v1.21.0
14-
go.opentelemetry.io/otel/trace v1.21.0
12+
go.opentelemetry.io/otel v1.23.0
13+
go.opentelemetry.io/otel/sdk v1.23.0
14+
go.opentelemetry.io/otel/trace v1.23.0
1515
go.temporal.io/api v1.32.0
1616
go.temporal.io/sdk v1.12.0
1717
go.temporal.io/sdk/contrib/opentelemetry v0.1.0
@@ -25,7 +25,7 @@ require (
2525
require (
2626
github.com/davecgh/go-spew v1.1.1 // indirect
2727
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a // indirect
28-
github.com/go-logr/logr v1.3.0 // indirect
28+
github.com/go-logr/logr v1.4.1 // indirect
2929
github.com/go-logr/stdr v1.2.2 // indirect
3030
github.com/gogo/protobuf v1.3.2 // indirect
3131
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
@@ -34,7 +34,7 @@ require (
3434
github.com/robfig/cron v1.2.0 // indirect
3535
github.com/stretchr/objx v0.5.2 // indirect
3636
github.com/twmb/murmur3 v1.1.5 // indirect
37-
go.opentelemetry.io/otel/metric v1.21.0 // indirect
37+
go.opentelemetry.io/otel/metric v1.23.0 // indirect
3838
go.uber.org/atomic v1.9.0 // indirect
3939
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
4040
golang.org/x/net v0.24.0 // indirect

‎test/go.sum

+11-11
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
3030
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
3131
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
3232
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
33-
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
34-
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
33+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
34+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
3535
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
3636
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
3737
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@@ -140,15 +140,15 @@ github.com/uber-go/tally/v4 v4.1.1/go.mod h1:aXeSTDMl4tNosyf6rdU8jlgScHyjEGGtfJ/
140140
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
141141
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
142142
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
143-
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
144-
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
145-
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
146-
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
147-
go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZXQ8=
148-
go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E=
149-
go.opentelemetry.io/otel/sdk/metric v1.21.0 h1:smhI5oD714d6jHE6Tie36fPx4WDFIg+Y6RfAY4ICcR0=
150-
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
151-
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
143+
go.opentelemetry.io/otel v1.23.0 h1:Df0pqjqExIywbMCMTxkAwzjLZtRf+bBKLbUcpxO2C9E=
144+
go.opentelemetry.io/otel v1.23.0/go.mod h1:YCycw9ZeKhcJFrb34iVSkyT0iczq/zYDtZYFufObyB0=
145+
go.opentelemetry.io/otel/metric v1.23.0 h1:pazkx7ss4LFVVYSxYew7L5I6qvLXHA0Ap2pwV+9Cnpo=
146+
go.opentelemetry.io/otel/metric v1.23.0/go.mod h1:MqUW2X2a6Q8RN96E2/nqNoT+z9BSms20Jb7Bbp+HiTo=
147+
go.opentelemetry.io/otel/sdk v1.23.0 h1:0KM9Zl2esnl+WSukEmlaAEjVY5HDZANOHferLq36BPc=
148+
go.opentelemetry.io/otel/sdk v1.23.0/go.mod h1:wUscup7byToqyKJSilEtMf34FgdCAsFpFOjXnAwFfO0=
149+
go.opentelemetry.io/otel/sdk/metric v1.23.0 h1:u81lMvmK6GMgN4Fty7K7S6cSKOZhMKJMK2TB+KaTs0I=
150+
go.opentelemetry.io/otel/trace v1.23.0 h1:37Ik5Ib7xfYVb4V1UtnT97T1jI+AoIYkJyPkuL4iJgI=
151+
go.opentelemetry.io/otel/trace v1.23.0/go.mod h1:GSGTbIClEsuZrGIzoEHqsVfxgn5UkggkflQwDScNUsk=
152152
go.temporal.io/api v1.32.0 h1:Jv0FieWDq0HJVqoHRE/kRHM+tIaRtR16RbXZZl+8Qb4=
153153
go.temporal.io/api v1.32.0/go.mod h1:MClRjMCgXZTKmxyItEJPRR5NuJRBhSEpuF9wuh97N6U=
154154
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=

0 commit comments

Comments
 (0)
Please sign in to comment.