Skip to content

Commit 5cc76d9

Browse files
authored
don't re-instantiate nop instruments everywhere (#610)
1 parent 29d3973 commit 5cc76d9

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"id": "ae0ef34a-003c-4214-9a07-8ca1288c1360",
33
"type": "bugfix",
4-
"description": "Avoid unnecessary allocation overhead from httptrace.WithClientTrace when the client isn't using a metrics sink.",
4+
"description": "Avoid unnecessary allocation overhead from the metrics system when not in use.",
55
"modules": [
66
"."
77
]
8-
}
8+
}

metrics/nop.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,72 +19,72 @@ var _ Meter = (*NopMeter)(nil)
1919

2020
// Int64Counter creates a no-op instrument.
2121
func (NopMeter) Int64Counter(string, ...InstrumentOption) (Int64Counter, error) {
22-
return nopInstrument[int64]{}, nil
22+
return nopInstrumentInt64, nil
2323
}
2424

2525
// Int64UpDownCounter creates a no-op instrument.
2626
func (NopMeter) Int64UpDownCounter(string, ...InstrumentOption) (Int64UpDownCounter, error) {
27-
return nopInstrument[int64]{}, nil
27+
return nopInstrumentInt64, nil
2828
}
2929

3030
// Int64Gauge creates a no-op instrument.
3131
func (NopMeter) Int64Gauge(string, ...InstrumentOption) (Int64Gauge, error) {
32-
return nopInstrument[int64]{}, nil
32+
return nopInstrumentInt64, nil
3333
}
3434

3535
// Int64Histogram creates a no-op instrument.
3636
func (NopMeter) Int64Histogram(string, ...InstrumentOption) (Int64Histogram, error) {
37-
return nopInstrument[int64]{}, nil
37+
return nopInstrumentInt64, nil
3838
}
3939

4040
// Int64AsyncCounter creates a no-op instrument.
4141
func (NopMeter) Int64AsyncCounter(string, Int64Callback, ...InstrumentOption) (AsyncInstrument, error) {
42-
return nopInstrument[int64]{}, nil
42+
return nopInstrumentInt64, nil
4343
}
4444

4545
// Int64AsyncUpDownCounter creates a no-op instrument.
4646
func (NopMeter) Int64AsyncUpDownCounter(string, Int64Callback, ...InstrumentOption) (AsyncInstrument, error) {
47-
return nopInstrument[int64]{}, nil
47+
return nopInstrumentInt64, nil
4848
}
4949

5050
// Int64AsyncGauge creates a no-op instrument.
5151
func (NopMeter) Int64AsyncGauge(string, Int64Callback, ...InstrumentOption) (AsyncInstrument, error) {
52-
return nopInstrument[int64]{}, nil
52+
return nopInstrumentInt64, nil
5353
}
5454

5555
// Float64Counter creates a no-op instrument.
5656
func (NopMeter) Float64Counter(string, ...InstrumentOption) (Float64Counter, error) {
57-
return nopInstrument[float64]{}, nil
57+
return nopInstrumentFloat64, nil
5858
}
5959

6060
// Float64UpDownCounter creates a no-op instrument.
6161
func (NopMeter) Float64UpDownCounter(string, ...InstrumentOption) (Float64UpDownCounter, error) {
62-
return nopInstrument[float64]{}, nil
62+
return nopInstrumentFloat64, nil
6363
}
6464

6565
// Float64Gauge creates a no-op instrument.
6666
func (NopMeter) Float64Gauge(string, ...InstrumentOption) (Float64Gauge, error) {
67-
return nopInstrument[float64]{}, nil
67+
return nopInstrumentFloat64, nil
6868
}
6969

7070
// Float64Histogram creates a no-op instrument.
7171
func (NopMeter) Float64Histogram(string, ...InstrumentOption) (Float64Histogram, error) {
72-
return nopInstrument[float64]{}, nil
72+
return nopInstrumentFloat64, nil
7373
}
7474

7575
// Float64AsyncCounter creates a no-op instrument.
7676
func (NopMeter) Float64AsyncCounter(string, Float64Callback, ...InstrumentOption) (AsyncInstrument, error) {
77-
return nopInstrument[float64]{}, nil
77+
return nopInstrumentFloat64, nil
7878
}
7979

8080
// Float64AsyncUpDownCounter creates a no-op instrument.
8181
func (NopMeter) Float64AsyncUpDownCounter(string, Float64Callback, ...InstrumentOption) (AsyncInstrument, error) {
82-
return nopInstrument[float64]{}, nil
82+
return nopInstrumentFloat64, nil
8383
}
8484

8585
// Float64AsyncGauge creates a no-op instrument.
8686
func (NopMeter) Float64AsyncGauge(string, Float64Callback, ...InstrumentOption) (AsyncInstrument, error) {
87-
return nopInstrument[float64]{}, nil
87+
return nopInstrumentFloat64, nil
8888
}
8989

9090
type nopInstrument[N any] struct{}
@@ -93,3 +93,6 @@ func (nopInstrument[N]) Add(context.Context, N, ...RecordMetricOption) {}
9393
func (nopInstrument[N]) Sample(context.Context, N, ...RecordMetricOption) {}
9494
func (nopInstrument[N]) Record(context.Context, N, ...RecordMetricOption) {}
9595
func (nopInstrument[_]) Stop() {}
96+
97+
var nopInstrumentInt64 = nopInstrument[int64]{}
98+
var nopInstrumentFloat64 = nopInstrument[float64]{}

0 commit comments

Comments
 (0)