diff --git a/conn.go b/conn.go index 4dbda63..4cfcc09 100644 --- a/conn.go +++ b/conn.go @@ -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(':') @@ -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) { diff --git a/options.go b/options.go index ef95bb8..7c696c2 100644 --- a/options.go +++ b/options.go @@ -191,6 +191,7 @@ const ( // Datadog tag format. // See http://docs.datadoghq.com/guides/metrics/#tags Datadog + Librato ) var ( @@ -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 { @@ -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 + }, } ) diff --git a/statsd_test.go b/statsd_test.go index c97650c..13bdef2 100644 --- a/statsd_test.go +++ b/statsd_test.go @@ -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)