Skip to content

Commit c91ecd8

Browse files
committed
Add Instrument.IsEnabled per specs
Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 9339b21 commit c91ecd8

File tree

7 files changed

+212
-4
lines changed

7 files changed

+212
-4
lines changed

internal/global/instruments.go

+56
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ func (i *sfCounter) Add(ctx context.Context, incr float64, opts ...metric.AddOpt
229229
}
230230
}
231231

232+
func (i *sfCounter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
233+
if ctr := i.delegate.Load(); ctr != nil {
234+
ctr.(metric.Float64Counter).IsEnabled(ctx, opts...)
235+
}
236+
return false
237+
}
238+
232239
type sfUpDownCounter struct {
233240
embedded.Float64UpDownCounter
234241

@@ -255,6 +262,13 @@ func (i *sfUpDownCounter) Add(ctx context.Context, incr float64, opts ...metric.
255262
}
256263
}
257264

265+
func (i *sfUpDownCounter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
266+
if ctr := i.delegate.Load(); ctr != nil {
267+
ctr.(metric.Float64UpDownCounter).IsEnabled(ctx, opts...)
268+
}
269+
return false
270+
}
271+
258272
type sfHistogram struct {
259273
embedded.Float64Histogram
260274

@@ -281,6 +295,13 @@ func (i *sfHistogram) Record(ctx context.Context, x float64, opts ...metric.Reco
281295
}
282296
}
283297

298+
func (i *sfHistogram) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
299+
if ctr := i.delegate.Load(); ctr != nil {
300+
ctr.(metric.Float64Histogram).IsEnabled(ctx, opts...)
301+
}
302+
return false
303+
}
304+
284305
type sfGauge struct {
285306
embedded.Float64Gauge
286307

@@ -307,6 +328,13 @@ func (i *sfGauge) Record(ctx context.Context, x float64, opts ...metric.RecordOp
307328
}
308329
}
309330

331+
func (i *sfGauge) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
332+
if ctr := i.delegate.Load(); ctr != nil {
333+
ctr.(metric.Float64Gauge).IsEnabled(ctx, opts...)
334+
}
335+
return false
336+
}
337+
310338
type siCounter struct {
311339
embedded.Int64Counter
312340

@@ -333,6 +361,13 @@ func (i *siCounter) Add(ctx context.Context, x int64, opts ...metric.AddOption)
333361
}
334362
}
335363

364+
func (i *siCounter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
365+
if ctr := i.delegate.Load(); ctr != nil {
366+
ctr.(metric.Int64Counter).IsEnabled(ctx, opts...)
367+
}
368+
return false
369+
}
370+
336371
type siUpDownCounter struct {
337372
embedded.Int64UpDownCounter
338373

@@ -359,6 +394,13 @@ func (i *siUpDownCounter) Add(ctx context.Context, x int64, opts ...metric.AddOp
359394
}
360395
}
361396

397+
func (i *siUpDownCounter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
398+
if ctr := i.delegate.Load(); ctr != nil {
399+
ctr.(metric.Int64UpDownCounter).IsEnabled(ctx, opts...)
400+
}
401+
return false
402+
}
403+
362404
type siHistogram struct {
363405
embedded.Int64Histogram
364406

@@ -385,6 +427,13 @@ func (i *siHistogram) Record(ctx context.Context, x int64, opts ...metric.Record
385427
}
386428
}
387429

430+
func (i *siHistogram) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
431+
if ctr := i.delegate.Load(); ctr != nil {
432+
ctr.(metric.Int64Histogram).IsEnabled(ctx, opts...)
433+
}
434+
return false
435+
}
436+
388437
type siGauge struct {
389438
embedded.Int64Gauge
390439

@@ -410,3 +459,10 @@ func (i *siGauge) Record(ctx context.Context, x int64, opts ...metric.RecordOpti
410459
ctr.(metric.Int64Gauge).Record(ctx, x, opts...)
411460
}
412461
}
462+
463+
func (i *siGauge) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
464+
if ctr := i.delegate.Load(); ctr != nil {
465+
ctr.(metric.Int64Gauge).IsEnabled(ctx, opts...)
466+
}
467+
return false
468+
}

metric/instrument.go

+6
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,9 @@ func WithAttributes(attributes ...attribute.KeyValue) MeasurementOption {
366366
copy(cp, attributes)
367367
return attrOpt{set: attribute.NewSet(cp...)}
368368
}
369+
370+
// EnabledOption applies options to IsEnabled func.
371+
type EnabledOption interface {
372+
// Extend this to support EnabledOptionConfig when needed.
373+
applyEnabledOption()
374+
}

metric/noop/noop.go

+40
Original file line numberDiff line numberDiff line change
@@ -176,55 +176,95 @@ type Int64Counter struct{ embedded.Int64Counter }
176176
// Add performs no operation.
177177
func (Int64Counter) Add(context.Context, int64, ...metric.AddOption) {}
178178

179+
// IsEnabled returns false always.
180+
func (Int64Counter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
181+
return false
182+
}
183+
179184
// Float64Counter is an OpenTelemetry Counter used to record float64
180185
// measurements. It produces no telemetry.
181186
type Float64Counter struct{ embedded.Float64Counter }
182187

183188
// Add performs no operation.
184189
func (Float64Counter) Add(context.Context, float64, ...metric.AddOption) {}
185190

191+
// IsEnabled returns false always.
192+
func (Float64Counter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
193+
return false
194+
}
195+
186196
// Int64UpDownCounter is an OpenTelemetry UpDownCounter used to record int64
187197
// measurements. It produces no telemetry.
188198
type Int64UpDownCounter struct{ embedded.Int64UpDownCounter }
189199

190200
// Add performs no operation.
191201
func (Int64UpDownCounter) Add(context.Context, int64, ...metric.AddOption) {}
192202

203+
// IsEnabled returns false always.
204+
func (Int64UpDownCounter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
205+
return false
206+
}
207+
193208
// Float64UpDownCounter is an OpenTelemetry UpDownCounter used to record
194209
// float64 measurements. It produces no telemetry.
195210
type Float64UpDownCounter struct{ embedded.Float64UpDownCounter }
196211

197212
// Add performs no operation.
198213
func (Float64UpDownCounter) Add(context.Context, float64, ...metric.AddOption) {}
199214

215+
// IsEnabled returns false always.
216+
func (Float64UpDownCounter) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
217+
return false
218+
}
219+
200220
// Int64Histogram is an OpenTelemetry Histogram used to record int64
201221
// measurements. It produces no telemetry.
202222
type Int64Histogram struct{ embedded.Int64Histogram }
203223

204224
// Record performs no operation.
205225
func (Int64Histogram) Record(context.Context, int64, ...metric.RecordOption) {}
206226

227+
// IsEnabled returns false always.
228+
func (Int64Histogram) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
229+
return false
230+
}
231+
207232
// Float64Histogram is an OpenTelemetry Histogram used to record float64
208233
// measurements. It produces no telemetry.
209234
type Float64Histogram struct{ embedded.Float64Histogram }
210235

211236
// Record performs no operation.
212237
func (Float64Histogram) Record(context.Context, float64, ...metric.RecordOption) {}
213238

239+
// IsEnabled returns false always.
240+
func (Float64Histogram) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
241+
return false
242+
}
243+
214244
// Int64Gauge is an OpenTelemetry Gauge used to record instantaneous int64
215245
// measurements. It produces no telemetry.
216246
type Int64Gauge struct{ embedded.Int64Gauge }
217247

218248
// Record performs no operation.
219249
func (Int64Gauge) Record(context.Context, int64, ...metric.RecordOption) {}
220250

251+
// IsEnabled returns false always.
252+
func (Int64Gauge) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
253+
return false
254+
}
255+
221256
// Float64Gauge is an OpenTelemetry Gauge used to record instantaneous float64
222257
// measurements. It produces no telemetry.
223258
type Float64Gauge struct{ embedded.Float64Gauge }
224259

225260
// Record performs no operation.
226261
func (Float64Gauge) Record(context.Context, float64, ...metric.RecordOption) {}
227262

263+
// IsEnabled returns false always.
264+
func (Float64Gauge) IsEnabled(ctx context.Context, opts ...metric.EnabledOption) bool {
265+
return false
266+
}
267+
228268
// Int64ObservableCounter is an OpenTelemetry ObservableCounter used to record
229269
// int64 measurements. It produces no telemetry.
230270
type Int64ObservableCounter struct {

metric/syncfloat64.go

+28
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ 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+
// IsEnabled returns true if this instrument is enabled.
30+
//
31+
// This helps users avoid performing computationally expensive operations when
32+
// recording measurements. The returned value cannot be cached since it
33+
// may change over time.
34+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
2835
}
2936

3037
// Float64CounterConfig contains options for synchronous counter instruments that
@@ -78,6 +85,13 @@ type Float64UpDownCounter interface {
7885
// Use the WithAttributeSet (or, if performance is not a concern,
7986
// the WithAttributes) option to include measurement attributes.
8087
Add(ctx context.Context, incr float64, options ...AddOption)
88+
89+
// IsEnabled returns true if this instrument is enabled.
90+
//
91+
// This helps users avoid performing computationally expensive operations when
92+
// recording measurements. The returned value cannot be cached since it
93+
// may change over time.
94+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
8195
}
8296

8397
// Float64UpDownCounterConfig contains options for synchronous counter
@@ -131,6 +145,13 @@ type Float64Histogram interface {
131145
// Use the WithAttributeSet (or, if performance is not a concern,
132146
// the WithAttributes) option to include measurement attributes.
133147
Record(ctx context.Context, incr float64, options ...RecordOption)
148+
149+
// IsEnabled returns true if this instrument is enabled.
150+
//
151+
// This helps users avoid performing computationally expensive operations when
152+
// recording measurements. The returned value cannot be cached since it
153+
// may change over time.
154+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
134155
}
135156

136157
// Float64HistogramConfig contains options for synchronous histogram
@@ -189,6 +210,13 @@ type Float64Gauge interface {
189210
// Use the WithAttributeSet (or, if performance is not a concern,
190211
// the WithAttributes) option to include measurement attributes.
191212
Record(ctx context.Context, value float64, options ...RecordOption)
213+
214+
// IsEnabled returns true if this instrument is enabled.
215+
//
216+
// This helps users avoid performing computationally expensive operations when
217+
// recording measurements. The returned value cannot be cached since it
218+
// may change over time.
219+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
192220
}
193221

194222
// Float64GaugeConfig contains options for synchronous gauge instruments that

metric/syncint64.go

+28
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ 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+
// IsEnabled returns true if this instrument is enabled.
30+
//
31+
// This helps users avoid performing computationally expensive operations when
32+
// recording measurements. The returned value cannot be cached since it
33+
// may change over time.
34+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
2835
}
2936

3037
// Int64CounterConfig contains options for synchronous counter instruments that
@@ -78,6 +85,13 @@ type Int64UpDownCounter interface {
7885
// Use the WithAttributeSet (or, if performance is not a concern,
7986
// the WithAttributes) option to include measurement attributes.
8087
Add(ctx context.Context, incr int64, options ...AddOption)
88+
89+
// IsEnabled returns true if this instrument is enabled.
90+
//
91+
// This helps users avoid performing computationally expensive operations when
92+
// recording measurements. The returned value cannot be cached since it
93+
// may change over time.
94+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
8195
}
8296

8397
// Int64UpDownCounterConfig contains options for synchronous counter
@@ -131,6 +145,13 @@ type Int64Histogram interface {
131145
// Use the WithAttributeSet (or, if performance is not a concern,
132146
// the WithAttributes) option to include measurement attributes.
133147
Record(ctx context.Context, incr int64, options ...RecordOption)
148+
149+
// IsEnabled returns true if this instrument is enabled.
150+
//
151+
// This helps users avoid performing computationally expensive operations when
152+
// recording measurements. The returned value cannot be cached since it
153+
// may change over time.
154+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
134155
}
135156

136157
// Int64HistogramConfig contains options for synchronous histogram instruments
@@ -189,6 +210,13 @@ type Int64Gauge interface {
189210
// Use the WithAttributeSet (or, if performance is not a concern,
190211
// the WithAttributes) option to include measurement attributes.
191212
Record(ctx context.Context, value int64, options ...RecordOption)
213+
214+
// IsEnabled returns true if this instrument is enabled.
215+
//
216+
// This helps users avoid performing computationally expensive operations when
217+
// recording measurements. The returned value cannot be cached since it
218+
// may change over time.
219+
IsEnabled(ctx context.Context, options ...EnabledOption) bool
192220
}
193221

194222
// Int64GaugeConfig contains options for synchronous gauge instruments that

sdk/metric/instrument.go

+8
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func (i *int64Inst) Record(ctx context.Context, val int64, opts ...metric.Record
196196
i.aggregate(ctx, val, c.Attributes())
197197
}
198198

199+
func (i *int64Inst) IsEnabled(context.Context, ...metric.EnabledOption) bool {
200+
return len(i.measures) != 0
201+
}
202+
199203
func (i *int64Inst) aggregate(ctx context.Context, val int64, s attribute.Set) { // nolint:revive // okay to shadow pkg with method.
200204
for _, in := range i.measures {
201205
in(ctx, val, s)
@@ -228,6 +232,10 @@ func (i *float64Inst) Record(ctx context.Context, val float64, opts ...metric.Re
228232
i.aggregate(ctx, val, c.Attributes())
229233
}
230234

235+
func (i *float64Inst) IsEnabled(context.Context, ...metric.EnabledOption) bool {
236+
return len(i.measures) != 0
237+
}
238+
231239
func (i *float64Inst) aggregate(ctx context.Context, val float64, s attribute.Set) {
232240
for _, in := range i.measures {
233241
in(ctx, val, s)

0 commit comments

Comments
 (0)