From 47f55d88c1ad4e0530e36efcd58bd221cb94f641 Mon Sep 17 00:00:00 2001 From: Shachar Tal Date: Sat, 5 Aug 2017 18:20:05 +0300 Subject: [PATCH 1/3] add librato tagging support --- conn.go | 2 +- options.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/conn.go b/conn.go index 4dbda63..15a3208 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(':') 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 + }, } ) From 7fd299103393af237f6c30aafbbac54f03ef92f9 Mon Sep 17 00:00:00 2001 From: Shachar Tal Date: Sat, 5 Aug 2017 18:27:37 +0300 Subject: [PATCH 2/3] tests updated --- statsd_test.go | 6 ++++++ 1 file changed, 6 insertions(+) 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) From b9b0e75fcc22e6a84fe246463415d6f6e28dbfe7 Mon Sep 17 00:00:00 2001 From: Shachar Tal Date: Sun, 3 Sep 2017 18:45:45 +0300 Subject: [PATCH 3/3] bugfix - did not support partial UDP/TCP writes --- conn.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/conn.go b/conn.go index 15a3208..4cfcc09 100644 --- a/conn.go +++ b/conn.go @@ -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) {