Skip to content

Commit 4d63611

Browse files
committed
Support remove on synchronous instruments
1 parent c9b7ecf commit 4d63611

22 files changed

+676
-205
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2121
- Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp`. (#7486)
2222
- Add experimental observability metrics for simple span processor in `go.opentelemetry.io/otel/sdk/trace`. (#7374)
2323
- Add experimental observability metrics in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#7512)
24+
- Add support to remove synchronous instrumentats in `go.opentelemetry.io/otel/sdk/metric`. (#7541)
2425

2526
### Fixed
2627

internal/global/instruments.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ func (i *sfCounter) Add(ctx context.Context, incr float64, opts ...metric.AddOpt
229229
}
230230
}
231231

232+
func (i *sfCounter) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
233+
if ctr := i.delegate.Load(); ctr != nil {
234+
ctr.(metric.Float64Counter).Remove(ctx, opts...)
235+
}
236+
}
237+
232238
type sfUpDownCounter struct {
233239
embedded.Float64UpDownCounter
234240

@@ -255,6 +261,12 @@ func (i *sfUpDownCounter) Add(ctx context.Context, incr float64, opts ...metric.
255261
}
256262
}
257263

264+
func (i *sfUpDownCounter) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
265+
if ctr := i.delegate.Load(); ctr != nil {
266+
ctr.(metric.Float64UpDownCounter).Remove(ctx, opts...)
267+
}
268+
}
269+
258270
type sfHistogram struct {
259271
embedded.Float64Histogram
260272

@@ -281,6 +293,12 @@ func (i *sfHistogram) Record(ctx context.Context, x float64, opts ...metric.Reco
281293
}
282294
}
283295

296+
func (i *sfHistogram) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
297+
if ctr := i.delegate.Load(); ctr != nil {
298+
ctr.(metric.Float64Histogram).Remove(ctx, opts...)
299+
}
300+
}
301+
284302
type sfGauge struct {
285303
embedded.Float64Gauge
286304

@@ -307,6 +325,12 @@ func (i *sfGauge) Record(ctx context.Context, x float64, opts ...metric.RecordOp
307325
}
308326
}
309327

328+
func (i *sfGauge) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
329+
if ctr := i.delegate.Load(); ctr != nil {
330+
ctr.(metric.Float64Gauge).Remove(ctx, opts...)
331+
}
332+
}
333+
310334
type siCounter struct {
311335
embedded.Int64Counter
312336

@@ -333,6 +357,12 @@ func (i *siCounter) Add(ctx context.Context, x int64, opts ...metric.AddOption)
333357
}
334358
}
335359

360+
func (i *siCounter) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
361+
if ctr := i.delegate.Load(); ctr != nil {
362+
ctr.(metric.Int64Counter).Remove(ctx, opts...)
363+
}
364+
}
365+
336366
type siUpDownCounter struct {
337367
embedded.Int64UpDownCounter
338368

@@ -359,6 +389,12 @@ func (i *siUpDownCounter) Add(ctx context.Context, x int64, opts ...metric.AddOp
359389
}
360390
}
361391

392+
func (i *siUpDownCounter) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
393+
if ctr := i.delegate.Load(); ctr != nil {
394+
ctr.(metric.Int64UpDownCounter).Remove(ctx, opts...)
395+
}
396+
}
397+
362398
type siHistogram struct {
363399
embedded.Int64Histogram
364400

@@ -385,6 +421,12 @@ func (i *siHistogram) Record(ctx context.Context, x int64, opts ...metric.Record
385421
}
386422
}
387423

424+
func (i *siHistogram) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
425+
if ctr := i.delegate.Load(); ctr != nil {
426+
ctr.(metric.Int64Histogram).Remove(ctx, opts...)
427+
}
428+
}
429+
388430
type siGauge struct {
389431
embedded.Int64Gauge
390432

@@ -410,3 +452,9 @@ func (i *siGauge) Record(ctx context.Context, x int64, opts ...metric.RecordOpti
410452
ctr.(metric.Int64Gauge).Record(ctx, x, opts...)
411453
}
412454
}
455+
456+
func (i *siGauge) Remove(ctx context.Context, opts ...metric.MeasurementOption) {
457+
if ctr := i.delegate.Load(); ctr != nil {
458+
ctr.(metric.Int64Gauge).Remove(ctx, opts...)
459+
}
460+
}

internal/global/instruments_test.go

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"testing"
99

10+
"go.opentelemetry.io/otel/attribute"
1011
"go.opentelemetry.io/otel/metric"
1112
"go.opentelemetry.io/otel/metric/embedded"
1213
"go.opentelemetry.io/otel/metric/noop"
@@ -98,29 +99,50 @@ func TestAsyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
9899
}
99100

100101
func TestSyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
102+
alice := attribute.NewSet(attribute.String("name", "alice"))
103+
bob := attribute.NewSet(attribute.String("name", "bob"))
104+
aliceOpt := metric.WithAttributeSet(alice)
105+
bobOpt := metric.WithAttributeSet(bob)
106+
101107
// Float64 Instruments
102108
t.Run("Float64", func(*testing.T) {
103109
t.Run("Counter", func(*testing.T) {
104110
delegate := &sfCounter{}
105-
f := func(v float64) { delegate.Add(t.Context(), v) }
111+
f := func(v float64) {
112+
delegate.Add(t.Context(), v, bobOpt)
113+
delegate.Add(t.Context(), v, aliceOpt)
114+
delegate.Remove(t.Context(), bobOpt)
115+
}
106116
testFloat64ConcurrentSafe(f, delegate.setDelegate)
107117
})
108118

109119
t.Run("UpDownCounter", func(*testing.T) {
110120
delegate := &sfUpDownCounter{}
111-
f := func(v float64) { delegate.Add(t.Context(), v) }
121+
f := func(v float64) {
122+
delegate.Add(t.Context(), v, aliceOpt)
123+
delegate.Add(t.Context(), v, bobOpt)
124+
delegate.Remove(t.Context(), aliceOpt)
125+
}
112126
testFloat64ConcurrentSafe(f, delegate.setDelegate)
113127
})
114128

115129
t.Run("Histogram", func(*testing.T) {
116130
delegate := &sfHistogram{}
117-
f := func(v float64) { delegate.Record(t.Context(), v) }
131+
f := func(v float64) {
132+
delegate.Record(t.Context(), v, aliceOpt)
133+
delegate.Record(t.Context(), v, bobOpt)
134+
delegate.Remove(t.Context(), aliceOpt)
135+
}
118136
testFloat64ConcurrentSafe(f, delegate.setDelegate)
119137
})
120138

121139
t.Run("Gauge", func(*testing.T) {
122140
delegate := &sfGauge{}
123-
f := func(v float64) { delegate.Record(t.Context(), v) }
141+
f := func(v float64) {
142+
delegate.Record(t.Context(), v, bobOpt)
143+
delegate.Record(t.Context(), v, aliceOpt)
144+
delegate.Remove(t.Context(), bobOpt)
145+
}
124146
testFloat64ConcurrentSafe(f, delegate.setDelegate)
125147
})
126148
})
@@ -130,25 +152,41 @@ func TestSyncInstrumentSetDelegateConcurrentSafe(t *testing.T) {
130152
t.Run("Int64", func(*testing.T) {
131153
t.Run("Counter", func(*testing.T) {
132154
delegate := &siCounter{}
133-
f := func(v int64) { delegate.Add(t.Context(), v) }
155+
f := func(v int64) {
156+
delegate.Add(t.Context(), v, aliceOpt)
157+
delegate.Add(t.Context(), v, bobOpt)
158+
delegate.Remove(t.Context(), aliceOpt)
159+
}
134160
testInt64ConcurrentSafe(f, delegate.setDelegate)
135161
})
136162

137163
t.Run("UpDownCounter", func(*testing.T) {
138164
delegate := &siUpDownCounter{}
139-
f := func(v int64) { delegate.Add(t.Context(), v) }
165+
f := func(v int64) {
166+
delegate.Add(t.Context(), v, aliceOpt)
167+
delegate.Add(t.Context(), v, bobOpt)
168+
delegate.Remove(t.Context(), aliceOpt)
169+
}
140170
testInt64ConcurrentSafe(f, delegate.setDelegate)
141171
})
142172

143173
t.Run("Histogram", func(*testing.T) {
144174
delegate := &siHistogram{}
145-
f := func(v int64) { delegate.Record(t.Context(), v) }
175+
f := func(v int64) {
176+
delegate.Record(t.Context(), v, aliceOpt)
177+
delegate.Record(t.Context(), v, bobOpt)
178+
delegate.Remove(t.Context(), aliceOpt)
179+
}
146180
testInt64ConcurrentSafe(f, delegate.setDelegate)
147181
})
148182

149183
t.Run("Gauge", func(*testing.T) {
150184
delegate := &siGauge{}
151-
f := func(v int64) { delegate.Record(t.Context(), v) }
185+
f := func(v int64) {
186+
delegate.Record(t.Context(), v, aliceOpt)
187+
delegate.Record(t.Context(), v, bobOpt)
188+
delegate.Remove(t.Context(), aliceOpt)
189+
}
152190
testInt64ConcurrentSafe(f, delegate.setDelegate)
153191
})
154192
})
@@ -158,24 +196,27 @@ type testCountingFloatInstrument struct {
158196
count int
159197

160198
metric.Float64Observable
161-
embedded.Float64Counter
162199
embedded.Float64UpDownCounter
163200
embedded.Float64Histogram
164201
embedded.Float64Gauge
165-
embedded.Float64ObservableCounter
166202
embedded.Float64ObservableUpDownCounter
167203
embedded.Float64ObservableGauge
204+
embedded.Float64Counter
205+
embedded.Float64ObservableCounter
168206
}
169207

170208
func (i *testCountingFloatInstrument) observe() {
171209
i.count++
172210
}
173211

174-
func (i *testCountingFloatInstrument) Add(context.Context, float64, ...metric.AddOption) {
212+
func (i *testCountingFloatInstrument) Record(context.Context, float64, ...metric.RecordOption) {
175213
i.count++
176214
}
177215

178-
func (i *testCountingFloatInstrument) Record(context.Context, float64, ...metric.RecordOption) {
216+
func (*testCountingFloatInstrument) Remove(context.Context, ...metric.MeasurementOption) {
217+
}
218+
219+
func (i *testCountingFloatInstrument) Add(context.Context, float64, ...metric.AddOption) {
179220
i.count++
180221
}
181222

@@ -192,11 +233,14 @@ type testCountingIntInstrument struct {
192233
embedded.Int64ObservableGauge
193234
}
194235

195-
func (i *testCountingIntInstrument) observe() {
236+
func (i *testCountingIntInstrument) Add(context.Context, int64, ...metric.AddOption) {
196237
i.count++
197238
}
198239

199-
func (i *testCountingIntInstrument) Add(context.Context, int64, ...metric.AddOption) {
240+
func (*testCountingIntInstrument) Remove(context.Context, ...metric.MeasurementOption) {
241+
}
242+
243+
func (i *testCountingIntInstrument) observe() {
200244
i.count++
201245
}
202246

metric/example_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ func ExampleMeter_upDownCounter() {
137137

138138
// Remove an item from the collection.
139139
itemsCounter.Add(context.Background(), -1)
140+
141+
// Add an item to the collection with some attributes
142+
itemsCounter.Add(context.Background(), 1, metric.WithAttributes(attribute.String("location", "kitchen")))
143+
// Add another item to the collection with different attributes
144+
itemsCounter.Add(context.Background(), 1, metric.WithAttributes(attribute.String("location", "living room")))
145+
146+
// Remove a counter with some attributes
147+
itemsCounter.Remove(context.Background(), metric.WithAttributes(attribute.String("location", "kitchen")))
140148
}
141149

142150
// Gauges can be used to record non-additive values when changes occur.

metric/noop/noop.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,55 +191,79 @@ type Int64Counter struct{ embedded.Int64Counter }
191191
// Add performs no operation.
192192
func (Int64Counter) Add(context.Context, int64, ...metric.AddOption) {}
193193

194+
// Remove performs no operation.
195+
func (Int64Counter) Remove(context.Context, ...metric.MeasurementOption) {}
196+
194197
// Float64Counter is an OpenTelemetry Counter used to record float64
195198
// measurements. It produces no telemetry.
196199
type Float64Counter struct{ embedded.Float64Counter }
197200

198201
// Add performs no operation.
199202
func (Float64Counter) Add(context.Context, float64, ...metric.AddOption) {}
200203

204+
// Remove performs no operation.
205+
func (Float64Counter) Remove(context.Context, ...metric.MeasurementOption) {}
206+
201207
// Int64UpDownCounter is an OpenTelemetry UpDownCounter used to record int64
202208
// measurements. It produces no telemetry.
203209
type Int64UpDownCounter struct{ embedded.Int64UpDownCounter }
204210

205211
// Add performs no operation.
206212
func (Int64UpDownCounter) Add(context.Context, int64, ...metric.AddOption) {}
207213

214+
// Remove performs no operation.
215+
func (Int64UpDownCounter) Remove(context.Context, ...metric.MeasurementOption) {}
216+
208217
// Float64UpDownCounter is an OpenTelemetry UpDownCounter used to record
209218
// float64 measurements. It produces no telemetry.
210219
type Float64UpDownCounter struct{ embedded.Float64UpDownCounter }
211220

212221
// Add performs no operation.
213222
func (Float64UpDownCounter) Add(context.Context, float64, ...metric.AddOption) {}
214223

224+
// Remove performs no operation.
225+
func (Float64UpDownCounter) Remove(context.Context, ...metric.MeasurementOption) {}
226+
215227
// Int64Histogram is an OpenTelemetry Histogram used to record int64
216228
// measurements. It produces no telemetry.
217229
type Int64Histogram struct{ embedded.Int64Histogram }
218230

219231
// Record performs no operation.
220232
func (Int64Histogram) Record(context.Context, int64, ...metric.RecordOption) {}
221233

234+
// Remove performs no operation.
235+
func (Int64Histogram) Remove(context.Context, ...metric.MeasurementOption) {}
236+
222237
// Float64Histogram is an OpenTelemetry Histogram used to record float64
223238
// measurements. It produces no telemetry.
224239
type Float64Histogram struct{ embedded.Float64Histogram }
225240

226241
// Record performs no operation.
227242
func (Float64Histogram) Record(context.Context, float64, ...metric.RecordOption) {}
228243

244+
// Remove performs no operation.
245+
func (Float64Histogram) Remove(context.Context, ...metric.MeasurementOption) {}
246+
229247
// Int64Gauge is an OpenTelemetry Gauge used to record instantaneous int64
230248
// measurements. It produces no telemetry.
231249
type Int64Gauge struct{ embedded.Int64Gauge }
232250

233251
// Record performs no operation.
234252
func (Int64Gauge) Record(context.Context, int64, ...metric.RecordOption) {}
235253

254+
// Remove performs no operation.
255+
func (Int64Gauge) Remove(context.Context, ...metric.MeasurementOption) {}
256+
236257
// Float64Gauge is an OpenTelemetry Gauge used to record instantaneous float64
237258
// measurements. It produces no telemetry.
238259
type Float64Gauge struct{ embedded.Float64Gauge }
239260

240261
// Record performs no operation.
241262
func (Float64Gauge) Record(context.Context, float64, ...metric.RecordOption) {}
242263

264+
// Remove performs no operation.
265+
func (Float64Gauge) Remove(context.Context, ...metric.MeasurementOption) {}
266+
243267
// Int64ObservableCounter is an OpenTelemetry ObservableCounter used to record
244268
// int64 measurements. It produces no telemetry.
245269
type Int64ObservableCounter struct {

0 commit comments

Comments
 (0)