Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

remove Set method for cumulatives. #1120

Merged
merged 1 commit into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions metric/cumulative.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,6 @@ func (c *Float64Cumulative) GetEntry(labelVals ...metricdata.LabelValue) (*Float
return entry.(*Float64CumulativeEntry), nil
}

// Set sets the cumulative entry value to provided val. It returns without updating if the value is
// negative or lower than previously stored value.
func (e *Float64CumulativeEntry) Set(val float64) {
var swapped, equalOrLess bool
if val <= 0.0 {
return
}
for !swapped && !equalOrLess {
oldBits := atomic.LoadUint64(&e.val)
oldVal := math.Float64frombits(oldBits)
if val > oldVal {
valBits := math.Float64bits(val)
swapped = atomic.CompareAndSwapUint64(&e.val, oldBits, valBits)
} else {
equalOrLess = true
}
}
}

// Inc increments the cumulative entry value by val. It returns without incrementing if the val
// is negative.
func (e *Float64CumulativeEntry) Inc(val float64) {
Expand Down Expand Up @@ -129,23 +110,6 @@ func (c *Int64Cumulative) GetEntry(labelVals ...metricdata.LabelValue) (*Int64Cu
return entry.(*Int64CumulativeEntry), nil
}

// Set sets the value of the cumulative entry to the provided value. It returns without updating
// if the val is negative or if the val is lower than previously stored value.
func (e *Int64CumulativeEntry) Set(val int64) {
var swapped, equalOrLess bool
if val <= 0 {
return
}
for !swapped && !equalOrLess {
old := atomic.LoadInt64(&e.val)
if val > old {
swapped = atomic.CompareAndSwapInt64(&e.val, old, val)
} else {
equalOrLess = true
}
}
}

// Inc increments the current cumulative entry value by val. It returns without incrementing if
// the val is negative.
func (e *Int64CumulativeEntry) Inc(val int64) {
Expand Down
18 changes: 3 additions & 15 deletions metric/cumulative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCumulative(t *testing.T) {
f, _ := r.AddFloat64Cumulative("TestCumulative",
WithLabelKeys("k1", "k2"))
e, _ := f.GetEntry(metricdata.LabelValue{}, metricdata.LabelValue{})
e.Set(5)
e.Inc(5)
e, _ = f.GetEntry(metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
e.Inc(1)
e, _ = f.GetEntry(metricdata.NewLabelValue("k1v1"), metricdata.LabelValue{})
Expand Down Expand Up @@ -103,20 +103,14 @@ func readAndCompareInt64Val(testname string, r *Registry, want int64, t *testing
}
}

func TestInt64CumulativeEntry_IncAndSet(t *testing.T) {
func TestInt64CumulativeEntry_IncNegative(t *testing.T) {
r := NewRegistry()
g, _ := r.AddInt64Cumulative("bm")
e, _ := g.GetEntry()
e.Inc(5)
readAndCompareInt64Val("inc", r, 5, t)
e.Inc(-2)
readAndCompareInt64Val("inc negative", r, 5, t)
e.Set(-2)
readAndCompareInt64Val("set negative", r, 5, t)
e.Set(4)
readAndCompareInt64Val("set lower", r, 5, t)
e.Set(9)
readAndCompareInt64Val("set higher", r, 9, t)
}

func readAndCompareFloat64Val(testname string, r *Registry, want float64, t *testing.T) {
Expand All @@ -126,20 +120,14 @@ func readAndCompareFloat64Val(testname string, r *Registry, want float64, t *tes
}
}

func TestFloat64CumulativeEntry_IncAndSet(t *testing.T) {
func TestFloat64CumulativeEntry_IncNegative(t *testing.T) {
r := NewRegistry()
g, _ := r.AddFloat64Cumulative("bm")
e, _ := g.GetEntry()
e.Inc(5.0)
readAndCompareFloat64Val("inc", r, 5.0, t)
e.Inc(-2.0)
readAndCompareFloat64Val("inc negative", r, 5.0, t)
e.Set(-2.0)
readAndCompareFloat64Val("set negative", r, 5.0, t)
e.Set(4.0)
readAndCompareFloat64Val("set lower", r, 5.0, t)
e.Set(9.9)
readAndCompareFloat64Val("set higher", r, 9.9, t)
}

func TestCumulativeWithSameNameDiffType(t *testing.T) {
Expand Down