Skip to content

Commit

Permalink
Merge pull request #15 from filippog/float_counter
Browse files Browse the repository at this point in the history
Add float counter methods
  • Loading branch information
smira authored Dec 21, 2018
2 parents 133607a + 5d3b15f commit 62ddc7a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
29 changes: 29 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,35 @@ func (c *Client) Decr(stat string, count int64, tags ...Tag) {
c.Incr(stat, -count, tags...)
}

// FIncr increments a float counter metric
func (c *Client) FIncr(stat string, count float64, tags ...Tag) {
if 0 != count {
c.trans.bufLock.Lock()
lastLen := len(c.trans.buf)

c.trans.buf = append(c.trans.buf, []byte(c.metricPrefix)...)
c.trans.buf = append(c.trans.buf, []byte(stat)...)
if c.trans.tagFormat.Placement == TagPlacementName {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, ':')
c.trans.buf = strconv.AppendFloat(c.trans.buf, count, 'f', -1, 64)
c.trans.buf = append(c.trans.buf, []byte("|c")...)
if c.trans.tagFormat.Placement == TagPlacementSuffix {
c.trans.buf = c.formatTags(c.trans.buf, tags)
}
c.trans.buf = append(c.trans.buf, '\n')

c.trans.checkBuf(lastLen)
c.trans.bufLock.Unlock()
}
}

// FDecr decrements a float counter metric
func (c *Client) FDecr(stat string, count float64, tags ...Tag) {
c.FIncr(stat, -count, tags...)
}

// Timing tracks a duration event, the time delta must be given in milliseconds
func (c *Client) Timing(stat string, delta int64, tags ...Tag) {
c.trans.bufLock.Lock()
Expand Down
8 changes: 8 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ func TestCommands(t *testing.T) {
func() { client.Decr("req.count", 30) },
[]string{"foo.req.count:-30|c"}))

t.Run("FIncr", compareOutput(
func() { client.FIncr("req.count", 0.3) },
[]string{"foo.req.count:0.3|c"}))

t.Run("FDecr", compareOutput(
func() { client.FDecr("req.count", 0.3) },
[]string{"foo.req.count:-0.3|c"}))

t.Run("Timing", compareOutput(
func() { client.Timing("req.duration", 100) },
[]string{"foo.req.duration:100|ms"}))
Expand Down

0 comments on commit 62ddc7a

Please sign in to comment.