From 493e13f834d4f263e665ade0507aa6a6be49048d Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Tue, 4 Feb 2020 10:27:03 -0800 Subject: [PATCH] Update metric types to not use pointer receivers (#462) The methods on the `Float64Gauge`, `Int64Gauge`, `Float64Counter`, `Int64Counter`, `Float64Measure`, and `Int64Measure` `struct`s do not need to mutate the internal state of the `struct` and can therefore be defined with value receivers instead. This aligns closer to the function signatures of each instruments constructor function. Additionally, this change means calls to these methods do not need an allocation to the heap. Resolves #440 Co-authored-by: Rahul Patel --- api/metric/counter.go | 16 ++++++++-------- api/metric/gauge.go | 16 ++++++++-------- api/metric/measure.go | 16 ++++++++-------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/api/metric/counter.go b/api/metric/counter.go index d4ff42bac5f..9d085a232a4 100644 --- a/api/metric/counter.go +++ b/api/metric/counter.go @@ -51,7 +51,7 @@ type BoundInt64Counter struct { // If the labels do not contain a value for the key specified in the // counter with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Float64Counter) Bind(labels LabelSet) (h BoundFloat64Counter) { +func (c Float64Counter) Bind(labels LabelSet) (h BoundFloat64Counter) { h.commonBoundInstrument = c.bind(labels) return } @@ -63,20 +63,20 @@ func (c *Float64Counter) Bind(labels LabelSet) (h BoundFloat64Counter) { // If the labels do not contain a value for the key specified in the // counter with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Int64Counter) Bind(labels LabelSet) (h BoundInt64Counter) { +func (c Int64Counter) Bind(labels LabelSet) (h BoundInt64Counter) { h.commonBoundInstrument = c.bind(labels) return } // Measurement creates a Measurement object to use with batch // recording. -func (c *Float64Counter) Measurement(value float64) Measurement { +func (c Float64Counter) Measurement(value float64) Measurement { return c.float64Measurement(value) } // Measurement creates a Measurement object to use with batch // recording. -func (c *Int64Counter) Measurement(value int64) Measurement { +func (c Int64Counter) Measurement(value int64) Measurement { return c.int64Measurement(value) } @@ -87,7 +87,7 @@ func (c *Int64Counter) Measurement(value int64) Measurement { // If the labels do not contain a value for the key specified in the // counter with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Float64Counter) Add(ctx context.Context, value float64, labels LabelSet) { +func (c Float64Counter) Add(ctx context.Context, value float64, labels LabelSet) { c.directRecord(ctx, core.NewFloat64Number(value), labels) } @@ -98,16 +98,16 @@ func (c *Float64Counter) Add(ctx context.Context, value float64, labels LabelSet // If the labels do not contain a value for the key specified in the // counter with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Int64Counter) Add(ctx context.Context, value int64, labels LabelSet) { +func (c Int64Counter) Add(ctx context.Context, value int64, labels LabelSet) { c.directRecord(ctx, core.NewInt64Number(value), labels) } // Add adds the value to the counter's sum. -func (b *BoundFloat64Counter) Add(ctx context.Context, value float64) { +func (b BoundFloat64Counter) Add(ctx context.Context, value float64) { b.directRecord(ctx, core.NewFloat64Number(value)) } // Add adds the value to the counter's sum. -func (b *BoundInt64Counter) Add(ctx context.Context, value int64) { +func (b BoundInt64Counter) Add(ctx context.Context, value int64) { b.directRecord(ctx, core.NewInt64Number(value)) } diff --git a/api/metric/gauge.go b/api/metric/gauge.go index f69980481c5..0b32cb73660 100644 --- a/api/metric/gauge.go +++ b/api/metric/gauge.go @@ -51,7 +51,7 @@ type BoundInt64Gauge struct { // If the labels do not contain a value for the key specified in the // gauge with the WithKeys option, then the missing value will be // treated as unspecified. -func (g *Float64Gauge) Bind(labels LabelSet) (h BoundFloat64Gauge) { +func (g Float64Gauge) Bind(labels LabelSet) (h BoundFloat64Gauge) { h.commonBoundInstrument = g.bind(labels) return } @@ -63,20 +63,20 @@ func (g *Float64Gauge) Bind(labels LabelSet) (h BoundFloat64Gauge) { // If the labels do not contain a value for the key specified in the // gauge with the WithKeys option, then the missing value will be // treated as unspecified. -func (g *Int64Gauge) Bind(labels LabelSet) (h BoundInt64Gauge) { +func (g Int64Gauge) Bind(labels LabelSet) (h BoundInt64Gauge) { h.commonBoundInstrument = g.bind(labels) return } // Measurement creates a Measurement object to use with batch // recording. -func (g *Float64Gauge) Measurement(value float64) Measurement { +func (g Float64Gauge) Measurement(value float64) Measurement { return g.float64Measurement(value) } // Measurement creates a Measurement object to use with batch // recording. -func (g *Int64Gauge) Measurement(value int64) Measurement { +func (g Int64Gauge) Measurement(value int64) Measurement { return g.int64Measurement(value) } @@ -87,7 +87,7 @@ func (g *Int64Gauge) Measurement(value int64) Measurement { // If the labels do not contain a value for the key specified in the // gauge with the WithKeys option, then the missing value will be // treated as unspecified. -func (g *Float64Gauge) Set(ctx context.Context, value float64, labels LabelSet) { +func (g Float64Gauge) Set(ctx context.Context, value float64, labels LabelSet) { g.directRecord(ctx, core.NewFloat64Number(value), labels) } @@ -98,16 +98,16 @@ func (g *Float64Gauge) Set(ctx context.Context, value float64, labels LabelSet) // If the labels do not contain a value for the key specified in the // gauge with the WithKeys option, then the missing value will be // treated as unspecified. -func (g *Int64Gauge) Set(ctx context.Context, value int64, labels LabelSet) { +func (g Int64Gauge) Set(ctx context.Context, value int64, labels LabelSet) { g.directRecord(ctx, core.NewInt64Number(value), labels) } // Set assigns the passed value to the value of the gauge. -func (b *BoundFloat64Gauge) Set(ctx context.Context, value float64) { +func (b BoundFloat64Gauge) Set(ctx context.Context, value float64) { b.directRecord(ctx, core.NewFloat64Number(value)) } // Set assigns the passed value to the value of the gauge. -func (b *BoundInt64Gauge) Set(ctx context.Context, value int64) { +func (b BoundInt64Gauge) Set(ctx context.Context, value int64) { b.directRecord(ctx, core.NewInt64Number(value)) } diff --git a/api/metric/measure.go b/api/metric/measure.go index b82687f89c2..c6881fec4de 100644 --- a/api/metric/measure.go +++ b/api/metric/measure.go @@ -51,7 +51,7 @@ type BoundInt64Measure struct { // If the labels do not contain a value for the key specified in the // measure with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Float64Measure) Bind(labels LabelSet) (h BoundFloat64Measure) { +func (c Float64Measure) Bind(labels LabelSet) (h BoundFloat64Measure) { h.commonBoundInstrument = c.bind(labels) return } @@ -63,20 +63,20 @@ func (c *Float64Measure) Bind(labels LabelSet) (h BoundFloat64Measure) { // If the labels do not contain a value for the key specified in the // measure with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Int64Measure) Bind(labels LabelSet) (h BoundInt64Measure) { +func (c Int64Measure) Bind(labels LabelSet) (h BoundInt64Measure) { h.commonBoundInstrument = c.bind(labels) return } // Measurement creates a Measurement object to use with batch // recording. -func (c *Float64Measure) Measurement(value float64) Measurement { +func (c Float64Measure) Measurement(value float64) Measurement { return c.float64Measurement(value) } // Measurement creates a Measurement object to use with batch // recording. -func (c *Int64Measure) Measurement(value int64) Measurement { +func (c Int64Measure) Measurement(value int64) Measurement { return c.int64Measurement(value) } @@ -87,7 +87,7 @@ func (c *Int64Measure) Measurement(value int64) Measurement { // If the labels do not contain a value for the key specified in the // measure with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Float64Measure) Record(ctx context.Context, value float64, labels LabelSet) { +func (c Float64Measure) Record(ctx context.Context, value float64, labels LabelSet) { c.directRecord(ctx, core.NewFloat64Number(value), labels) } @@ -98,16 +98,16 @@ func (c *Float64Measure) Record(ctx context.Context, value float64, labels Label // If the labels do not contain a value for the key specified in the // measure with the WithKeys option, then the missing value will be // treated as unspecified. -func (c *Int64Measure) Record(ctx context.Context, value int64, labels LabelSet) { +func (c Int64Measure) Record(ctx context.Context, value int64, labels LabelSet) { c.directRecord(ctx, core.NewInt64Number(value), labels) } // Record adds a new value to the list of measure's records. -func (b *BoundFloat64Measure) Record(ctx context.Context, value float64) { +func (b BoundFloat64Measure) Record(ctx context.Context, value float64) { b.directRecord(ctx, core.NewFloat64Number(value)) } // Record adds a new value to the list of measure's records. -func (b *BoundInt64Measure) Record(ctx context.Context, value int64) { +func (b BoundInt64Measure) Record(ctx context.Context, value int64) { b.directRecord(ctx, core.NewInt64Number(value)) }