Skip to content

Commit 3e5bbab

Browse files
committed
Support remove on synchronous instruments
1 parent c9b7ecf commit 3e5bbab

19 files changed

+460
-41
lines changed

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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,27 @@ type testCountingFloatInstrument struct {
158158
count int
159159

160160
metric.Float64Observable
161-
embedded.Float64Counter
162161
embedded.Float64UpDownCounter
163162
embedded.Float64Histogram
164163
embedded.Float64Gauge
165-
embedded.Float64ObservableCounter
166164
embedded.Float64ObservableUpDownCounter
167165
embedded.Float64ObservableGauge
166+
embedded.Float64Counter
167+
embedded.Float64ObservableCounter
168168
}
169169

170170
func (i *testCountingFloatInstrument) observe() {
171171
i.count++
172172
}
173173

174-
func (i *testCountingFloatInstrument) Add(context.Context, float64, ...metric.AddOption) {
174+
func (i *testCountingFloatInstrument) Record(context.Context, float64, ...metric.RecordOption) {
175175
i.count++
176176
}
177177

178-
func (i *testCountingFloatInstrument) Record(context.Context, float64, ...metric.RecordOption) {
178+
func (*testCountingFloatInstrument) Remove(context.Context, ...metric.MeasurementOption) {
179+
}
180+
181+
func (i *testCountingFloatInstrument) Add(context.Context, float64, ...metric.AddOption) {
179182
i.count++
180183
}
181184

@@ -192,11 +195,14 @@ type testCountingIntInstrument struct {
192195
embedded.Int64ObservableGauge
193196
}
194197

195-
func (i *testCountingIntInstrument) observe() {
198+
func (i *testCountingIntInstrument) Add(context.Context, int64, ...metric.AddOption) {
196199
i.count++
197200
}
198201

199-
func (i *testCountingIntInstrument) Add(context.Context, int64, ...metric.AddOption) {
202+
func (*testCountingIntInstrument) Remove(context.Context, ...metric.MeasurementOption) {
203+
}
204+
205+
func (i *testCountingIntInstrument) observe() {
200206
i.count++
201207
}
202208

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 {

metric/syncfloat64.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type Float64Counter interface {
2525
// Use the WithAttributeSet (or, if performance is not a concern,
2626
// the WithAttributes) option to include measurement attributes.
2727
Add(ctx context.Context, incr float64, options ...AddOption)
28+
29+
// Remove unregisters an instrument.
30+
//
31+
// Use the WithAttributeSet (or, if performance is not a concern,
32+
// the WithAttributes) option to include measurement attributes.
33+
Remove(ctx context.Context, options ...MeasurementOption)
2834
}
2935

3036
// Float64CounterConfig contains options for synchronous counter instruments that
@@ -78,6 +84,12 @@ type Float64UpDownCounter interface {
7884
// Use the WithAttributeSet (or, if performance is not a concern,
7985
// the WithAttributes) option to include measurement attributes.
8086
Add(ctx context.Context, incr float64, options ...AddOption)
87+
88+
// Remove unregisters an instrument.
89+
//
90+
// Use the WithAttributeSet (or, if performance is not a concern,
91+
// the WithAttributes) option to include measurement attributes.
92+
Remove(ctx context.Context, options ...MeasurementOption)
8193
}
8294

8395
// Float64UpDownCounterConfig contains options for synchronous counter
@@ -131,6 +143,12 @@ type Float64Histogram interface {
131143
// Use the WithAttributeSet (or, if performance is not a concern,
132144
// the WithAttributes) option to include measurement attributes.
133145
Record(ctx context.Context, incr float64, options ...RecordOption)
146+
147+
// Remove unregisters an instrument.
148+
//
149+
// Use the WithAttributeSet (or, if performance is not a concern,
150+
// the WithAttributes) option to include measurement attributes.
151+
Remove(ctx context.Context, options ...MeasurementOption)
134152
}
135153

136154
// Float64HistogramConfig contains options for synchronous histogram
@@ -189,6 +207,12 @@ type Float64Gauge interface {
189207
// Use the WithAttributeSet (or, if performance is not a concern,
190208
// the WithAttributes) option to include measurement attributes.
191209
Record(ctx context.Context, value float64, options ...RecordOption)
210+
211+
// Remove unregisters an instrument.
212+
//
213+
// Use the WithAttributeSet (or, if performance is not a concern,
214+
// the WithAttributes) option to include measurement attributes.
215+
Remove(ctx context.Context, options ...MeasurementOption)
192216
}
193217

194218
// Float64GaugeConfig contains options for synchronous gauge instruments that

metric/syncint64.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type Int64Counter interface {
2525
// Use the WithAttributeSet (or, if performance is not a concern,
2626
// the WithAttributes) option to include measurement attributes.
2727
Add(ctx context.Context, incr int64, options ...AddOption)
28+
29+
// Remove unregisters an instrument.
30+
//
31+
// Use the WithAttributeSet (or, if performance is not a concern,
32+
// the WithAttributes) option to include measurement attributes.
33+
Remove(ctx context.Context, options ...MeasurementOption)
2834
}
2935

3036
// Int64CounterConfig contains options for synchronous counter instruments that
@@ -78,6 +84,12 @@ type Int64UpDownCounter interface {
7884
// Use the WithAttributeSet (or, if performance is not a concern,
7985
// the WithAttributes) option to include measurement attributes.
8086
Add(ctx context.Context, incr int64, options ...AddOption)
87+
88+
// Remove unregisters an instrument.
89+
//
90+
// Use the WithAttributeSet (or, if performance is not a concern,
91+
// the WithAttributes) option to include measurement attributes.
92+
Remove(ctx context.Context, options ...MeasurementOption)
8193
}
8294

8395
// Int64UpDownCounterConfig contains options for synchronous counter
@@ -131,6 +143,12 @@ type Int64Histogram interface {
131143
// Use the WithAttributeSet (or, if performance is not a concern,
132144
// the WithAttributes) option to include measurement attributes.
133145
Record(ctx context.Context, incr int64, options ...RecordOption)
146+
147+
// Remove unregisters an instrument.
148+
//
149+
// Use the WithAttributeSet (or, if performance is not a concern,
150+
// the WithAttributes) option to include measurement attributes.
151+
Remove(ctx context.Context, options ...MeasurementOption)
134152
}
135153

136154
// Int64HistogramConfig contains options for synchronous histogram instruments
@@ -189,6 +207,12 @@ type Int64Gauge interface {
189207
// Use the WithAttributeSet (or, if performance is not a concern,
190208
// the WithAttributes) option to include measurement attributes.
191209
Record(ctx context.Context, value int64, options ...RecordOption)
210+
211+
// Remove unregisters an instrument.
212+
//
213+
// Use the WithAttributeSet (or, if performance is not a concern,
214+
// the WithAttributes) option to include measurement attributes.
215+
Remove(ctx context.Context, options ...MeasurementOption)
192216
}
193217

194218
// Int64GaugeConfig contains options for synchronous gauge instruments that

0 commit comments

Comments
 (0)