-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Age Gauge and Monotonic Counter meter types #75
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package meter | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Netflix/spectator-go/spectator/writer" | ||
) | ||
|
||
// AgeGauge represents a value that is the time in seconds since the epoch at which an event | ||
// has successfully occurred, or 0 to use the current time in epoch seconds. After an Age Gauge | ||
// has been set, it will continue reporting the number of seconds since the last time recorded, | ||
// for as long as the spectatord process runs. The purpose of this metric type is to enable users | ||
// to more easily implement the Time Since Last Success alerting pattern. | ||
// | ||
// To set `now()` as the last success, set a value of 0. | ||
type AgeGauge struct { | ||
id *Id | ||
writer writer.Writer | ||
meterTypeSymbol string | ||
} | ||
|
||
// NewAgeGauge generates a new gauge, using the provided meter identifier. | ||
func NewAgeGauge(id *Id, writer writer.Writer) *AgeGauge { | ||
return &AgeGauge{id, writer, "A"} | ||
} | ||
|
||
// MeterId returns the meter identifier. | ||
func (g *AgeGauge) MeterId() *Id { | ||
return g.id | ||
} | ||
|
||
// Set records the current value. | ||
func (g *AgeGauge) Set(value int64) { | ||
if value >= 0 { | ||
var line = fmt.Sprintf("%s:%s:%d", g.meterTypeSymbol, g.id.spectatordId, value) | ||
g.writer.Write(line) | ||
} | ||
} | ||
|
||
// Now records the current time in epoch seconds in spectatord. | ||
func (g *AgeGauge) Now() { | ||
var line = fmt.Sprintf("%s:%s:0", g.meterTypeSymbol, g.id.spectatordId) | ||
g.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,69 @@ | ||
package meter | ||
|
||
import ( | ||
"github.com/Netflix/spectator-go/spectator/writer" | ||
"testing" | ||
) | ||
|
||
func TestAgeGauge_Set(t *testing.T) { | ||
id := NewId("set", nil) | ||
w := writer.MemoryWriter{} | ||
g := NewAgeGauge(id, &w) | ||
g.Set(100) | ||
|
||
expected := "A:set:100" | ||
if w.Lines[0] != expected { | ||
t.Errorf("Expected line to be %s, got %s", expected, w.Lines[0]) | ||
} | ||
} | ||
|
||
func TestAgeGauge_SetZero(t *testing.T) { | ||
id := NewId("setZero", nil) | ||
w := writer.MemoryWriter{} | ||
g := NewAgeGauge(id, &w) | ||
g.Set(0) | ||
|
||
expected := "A:setZero:0" | ||
if w.Lines[0] != expected { | ||
t.Errorf("Expected line to be %s, got %s", expected, w.Lines[0]) | ||
} | ||
} | ||
|
||
func TestAgeGauge_Now(t *testing.T) { | ||
id := NewId("now", nil) | ||
w := writer.MemoryWriter{} | ||
g := NewAgeGauge(id, &w) | ||
g.Now() | ||
|
||
expected := "A:now:0" | ||
if w.Lines[0] != expected { | ||
t.Errorf("Expected line to be %s, got %s", expected, w.Lines[0]) | ||
} | ||
} | ||
|
||
func TestAgeGauge_SetNegative(t *testing.T) { | ||
id := NewId("setNegative", nil) | ||
w := writer.MemoryWriter{} | ||
g := NewAgeGauge(id, &w) | ||
g.Set(-100) | ||
|
||
if len(w.Lines) != 0 { | ||
t.Error("Negative values should be ignored") | ||
} | ||
} | ||
|
||
func TestAgeGauge_SetMultipleValues(t *testing.T) { | ||
id := NewId("setMultiple", nil) | ||
w := writer.MemoryWriter{} | ||
g := NewAgeGauge(id, &w) | ||
g.Set(100) | ||
g.Set(200) | ||
g.Set(300) | ||
|
||
expectedLines := []string{"A:setMultiple:100", "A:setMultiple:200", "A:setMultiple:300"} | ||
for i, line := range w.Lines { | ||
if line != expectedLines[i] { | ||
t.Errorf("Expected line to be %s, got %s", expectedLines[i], 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
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,38 @@ | ||
package meter | ||
|
||
import ( | ||
"fmt" | ||
"github.com/Netflix/spectator-go/spectator/writer" | ||
) | ||
|
||
// MonotonicCounter 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. | ||
// | ||
// 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no atlas-docs on this meter type, so I just inlined the spectatord description. |
||
type MonotonicCounter struct { | ||
id *Id | ||
writer writer.Writer | ||
meterTypeSymbol string | ||
} | ||
|
||
// NewMonotonicCounter generates a new counter, using the provided meter identifier. | ||
func NewMonotonicCounter(id *Id, writer writer.Writer) *MonotonicCounter { | ||
return &MonotonicCounter{id, writer, "C"} | ||
} | ||
|
||
// MeterId returns the meter identifier. | ||
func (c *MonotonicCounter) MeterId() *Id { | ||
return c.id | ||
} | ||
|
||
// Set is to set a specific int64 delta as the current measurement. | ||
func (c *MonotonicCounter) Set(delta int64) { | ||
if delta > 0 { | ||
var line = fmt.Sprintf("%s:%s:%d", c.meterTypeSymbol, c.id.spectatordId, delta) | ||
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,24 @@ | ||
package meter | ||
|
||
import ( | ||
"github.com/Netflix/spectator-go/spectator/writer" | ||
"testing" | ||
) | ||
|
||
func TestMonotonicCounter_Set(t *testing.T) { | ||
w := writer.MemoryWriter{} | ||
id := NewId("add", nil) | ||
c := NewMonotonicCounter(id, &w) | ||
|
||
c.Set(4) | ||
|
||
expected := "C:add:4" | ||
if w.Lines[0] != expected { | ||
t.Error("Expected ", expected, " got ", w.Lines[0]) | ||
} | ||
|
||
c.Set(-1) | ||
if len(w.Lines) != 1 { | ||
t.Error("Negative deltas should be ignored") | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no atlas-docs on this meter type, so I just inlined the spectatord description.