Skip to content

Commit

Permalink
Update metric types to not use pointer receivers (#462)
Browse files Browse the repository at this point in the history
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 <rghetia@yahoo.com>
  • Loading branch information
MrAlias and rghetia authored Feb 4, 2020
1 parent 942713a commit 493e13f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
16 changes: 8 additions & 8 deletions api/metric/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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))
}
16 changes: 8 additions & 8 deletions api/metric/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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))
}
16 changes: 8 additions & 8 deletions api/metric/measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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))
}

0 comments on commit 493e13f

Please sign in to comment.