Skip to content

Commit

Permalink
Remove NewZeroNumber in favor of Number(0) (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacd authored Oct 30, 2019
1 parent 320c62a commit 88dafbb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
13 changes: 0 additions & 13 deletions api/core/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,6 @@ type Number uint64

// - constructors

// NewZeroNumber
func NewZeroNumber(kind NumberKind) Number {
switch kind {
case Int64NumberKind:
return NewInt64Number(0)
case Float64NumberKind:
return NewFloat64Number(0.)
case Uint64NumberKind:
return NewUint64Number(0)
}
return Number(0)
}

// NewNumberFromRaw creates a new Number from a raw value.
func NewNumberFromRaw(r uint64) Number {
return Number(r)
Expand Down
11 changes: 11 additions & 0 deletions api/core/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,14 @@ func TestNumber(t *testing.T) {
}
}
}

func TestNumberZero(t *testing.T) {
zero := Number(0)
zerof := NewFloat64Number(0)
zeroi := NewInt64Number(0)
zerou := NewUint64Number(0)

if zero != zerof || zero != zeroi || zero != zerou {
t.Errorf("Invalid zero representations")
}
}
5 changes: 1 addition & 4 deletions sdk/metric/aggregator/counter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ func (c *Aggregator) AsNumber() core.Number {

// Collect checkpoints the current value (atomically) and exports it.
func (c *Aggregator) Collect(ctx context.Context, rec export.MetricRecord, exp export.MetricBatcher) {
desc := rec.Descriptor()
kind := desc.NumberKind()
zero := core.NewZeroNumber(kind)
c.checkpoint = c.current.SwapNumberAtomic(zero)
c.checkpoint = c.current.SwapNumberAtomic(core.Number(0))

exp.Export(ctx, rec, c)
}
Expand Down
8 changes: 2 additions & 6 deletions sdk/metric/aggregator/maxsumcount/msc.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,15 @@ func (c *Aggregator) Max() core.Number {

// Collect saves the current value (atomically) and exports it.
func (c *Aggregator) Collect(ctx context.Context, rec export.MetricRecord, exp export.MetricBatcher) {
desc := rec.Descriptor()
kind := desc.NumberKind()
zero := core.NewZeroNumber(kind)

// N.B. There is no atomic operation that can update all three
// values at once, so there are races between Update() and
// Collect(). Therefore, atomically swap fields independently,
// knowing that individually the three parts of this aggregation
// could be spread across multiple collections in rare cases.

c.save.count.SetUint64(c.live.count.SwapUint64Atomic(0))
c.save.sum = c.live.sum.SwapNumberAtomic(zero)
c.save.max = c.live.max.SwapNumberAtomic(zero)
c.save.sum = c.live.sum.SwapNumberAtomic(core.Number(0))
c.save.max = c.live.max.SwapNumberAtomic(core.Number(0))

exp.Export(ctx, rec, c)
}
Expand Down

0 comments on commit 88dafbb

Please sign in to comment.