1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-05-13 21:56:48 +02:00

Remove NewZeroNumber in favor of Number(0) (#255)

This commit is contained in:
Joshua MacDonald 2019-10-30 11:22:14 -07:00 committed by GitHub
parent 320c62a780
commit 88dafbbb16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 23 deletions

View File

@ -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)

View File

@ -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")
}
}

View File

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

View File

@ -60,10 +60,6 @@ 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,
@ -71,8 +67,8 @@ func (c *Aggregator) Collect(ctx context.Context, rec export.MetricRecord, exp e
// 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)
}