Skip to content
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 librato tagging support #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func isNegative(v interface{}) bool {
func (c *conn) appendBucket(prefix, bucket string, tags string) {
c.appendString(prefix)
c.appendString(bucket)
if c.tagFormat == InfluxDB {
if c.tagFormat == InfluxDB || c.tagFormat == Librato {
c.appendString(tags)
}
c.appendByte(':')
Expand Down Expand Up @@ -247,13 +247,12 @@ func (c *conn) flush(n int) {
n = len(c.buf)
}

// Trim the last \n, StatsD does not like it.
_, err := c.w.Write(c.buf[:n-1])
total, err := c.w.Write(c.buf[:n])
c.handleError(err)
if n < len(c.buf) {
copy(c.buf, c.buf[n:])
if total < len(c.buf) {
copy(c.buf, c.buf[total:])
}
c.buf = c.buf[:len(c.buf)-n]
c.buf = c.buf[:len(c.buf)-total]
}

func (c *conn) handleError(err error) {
Expand Down
25 changes: 25 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ const (
// Datadog tag format.
// See http://docs.datadoghq.com/guides/metrics/#tags
Datadog
Librato
)

var (
Expand Down Expand Up @@ -224,6 +225,20 @@ var (
}
return buf.String()
},
Librato: func(tags []tag) string {
var buf bytes.Buffer
for i, tag := range tags {
if i == 0 {
_ = buf.WriteByte('#')
} else {
_ = buf.WriteByte(',')
}
_, _ = buf.WriteString(tag.K)
_ = buf.WriteByte('=')
_, _ = buf.WriteString(tag.V)
}
return buf.String()
},
}
splitFuncs = map[TagFormat]func(string) []tag{
InfluxDB: func(s string) []tag {
Expand All @@ -246,5 +261,15 @@ var (
}
return tags
},
Librato: func(s string) []tag {
s = s[1:]
pairs := strings.Split(s, ",")
tags := make([]tag, len(pairs))
for i, pair := range pairs {
kv := strings.Split(pair, ":")
tags[i] = tag{K: kv[0], V: kv[1]}
}
return tags
},
}
)
6 changes: 6 additions & 0 deletions statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ func TestDatadogTags(t *testing.T) {
}, TagsFormat(Datadog), Tags("tag1", "value1", "tag2", "value2"))
}

func TestLibratoTags(t *testing.T) {
testOutput(t, "test_key#tag1=value1,tag2=value2:1|c", func(c *Client) {
c.Increment(testKey)
}, TagsFormat(Librato), Tags("tag1", "value1", "tag2", "value2"))
}

func TestNoTagFormat(t *testing.T) {
testOutput(t, "test_key:1|c", func(c *Client) {
c.Increment(testKey)
Expand Down