-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix monotonic counter implementations (#100)
This change is a companion to the fix for monotonic counter implementations in spectatord. Netflix-Skunkworks/spectatord#90 The original monotonic counter (`C`) was always intended to be used for the case where a monotonic data source needs to be transformed into base units for recording data. For example, transforming nanoseconds into seconds. This requires division, which results in floats. There is a valid use case for handling uints in monotonic counters, if the data source is already in a base unit, such as bytes. Thus, a new meter type `U` is added to spectatord which supports this use case.
- Loading branch information
1 parent
9a2b37c
commit 22102a0
Showing
6 changed files
with
144 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package meter | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Netflix/spectator-go/v2/spectator/writer" | ||
) | ||
|
||
// MonotonicCounterUint is used to measure the rate at which some event is occurring. This | ||
// type is safe for concurrent use. | ||
// | ||
// The value is a monotonically increasing number. A minimum of two samples must be received | ||
// in order for spectatord to calculate a delta value and report it to the backend. | ||
// | ||
// This version of the monotonic counter is intended to support use cases where a data source value | ||
// can be sampled as-is, because it is already in base units, such as bytes, and thus, the data type | ||
// is uint64. | ||
// | ||
// A variety of networking metrics may be reported monotonically and this metric type provides a | ||
// convenient means of recording these values, at the expense of a slower time-to-first metric. | ||
type MonotonicCounterUint struct { | ||
id *Id | ||
writer writer.Writer | ||
meterTypeSymbol string | ||
} | ||
|
||
// NewMonotonicCounterUint generates a new counter, using the provided meter identifier. | ||
func NewMonotonicCounterUint(id *Id, writer writer.Writer) *MonotonicCounterUint { | ||
return &MonotonicCounterUint{id, writer, "U"} | ||
} | ||
|
||
// MeterId returns the meter identifier. | ||
func (c *MonotonicCounterUint) MeterId() *Id { | ||
return c.id | ||
} | ||
|
||
// Set sets a value as the current measurement; spectatord calculates the delta. | ||
func (c *MonotonicCounterUint) Set(value uint64) { | ||
var line = fmt.Sprintf("%s:%s:%d", c.meterTypeSymbol, c.id.spectatordId, value) | ||
c.writer.Write(line) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package meter | ||
|
||
import ( | ||
"github.com/Netflix/spectator-go/v2/spectator/writer" | ||
"testing" | ||
) | ||
|
||
func TestMonotonicCounterUint_Set(t *testing.T) { | ||
w := writer.MemoryWriter{} | ||
id := NewId("set", nil) | ||
c := NewMonotonicCounterUint(id, &w) | ||
|
||
c.Set(4) | ||
|
||
expected := "U:set:4" | ||
if w.Lines()[0] != expected { | ||
t.Error("Expected ", expected, " got ", w.Lines()[0]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters