diff --git a/conn.go b/conn.go index 4cfcc09..9f34545 100644 --- a/conn.go +++ b/conn.go @@ -45,18 +45,6 @@ func newConn(conf connConfig, muted bool) (*conn, error) { if err != nil { return c, err } - // When using UDP do a quick check to see if something is listening on the - // given port to return an error as soon as possible. - if c.network[:3] == "udp" { - for i := 0; i < 2; i++ { - _, err = c.w.Write(nil) - if err != nil { - _ = c.w.Close() - c.w = nil - return c, err - } - } - } // To prevent a buffer overflow add some capacity to the buffer to allow for // an additional metric. @@ -166,30 +154,21 @@ func isNegative(v interface{}) bool { switch n := v.(type) { case int: return n < 0 - case uint: - return n < 0 case int64: return n < 0 - case uint64: - return n < 0 case int32: return n < 0 - case uint32: - return n < 0 case int16: return n < 0 - case uint16: - return n < 0 case int8: return n < 0 - case uint8: - return n < 0 case float64: return n < 0 case float32: return n < 0 + default: + return false } - return false } func (c *conn) appendBucket(prefix, bucket string, tags string) { diff --git a/examples_test.go b/examples_test.go index dc6c2f3..0576bc4 100644 --- a/examples_test.go +++ b/examples_test.go @@ -5,7 +5,7 @@ import ( "runtime" "time" - "gopkg.in/alexcesaro/statsd.v2" + "github.com/andriyko/statsd" ) var ( diff --git a/statsd.go b/statsd.go index 8c26f90..fc3f7b7 100644 --- a/statsd.go +++ b/statsd.go @@ -84,7 +84,7 @@ func (c *Client) Count(bucket string, n interface{}) { } func (c *Client) skip() bool { - return c.muted || (c.rate != 1 && randFloat() > c.rate) + return nil == c || c.muted || (c.rate != 1 && randFloat() > c.rate) } // Increment increment the given bucket. It is equivalent to Count(bucket, 1). @@ -108,7 +108,7 @@ func (c *Client) Timing(bucket string, value interface{}) { c.conn.metric(c.prefix, bucket, value, "ms", c.rate, c.tags) } -// TimingWithTags ... +// TimingWithTags sends a timing value with tags to a bucket. func (c *Client) TimingWithTags(bucket string, value interface{}, tags map[string]string) { if c.skip() { return @@ -125,32 +125,6 @@ func (c *Client) Histogram(bucket string, value interface{}) { c.conn.metric(c.prefix, bucket, value, "h", c.rate, c.tags) } -// A Timing is an helper object that eases sending timing values. -type Timing struct { - start time.Time - c *Client -} - -// NewTiming creates a new Timing. -func (c *Client) NewTiming() Timing { - return Timing{start: now(), c: c} -} - -// Send sends the time elapsed since the creation of the Timing. -func (t Timing) Send(bucket string) { - t.c.Timing(bucket, int(t.Duration()/time.Millisecond)) -} - -// SendWithTags sends the time elapsed since the creation of the Timing -func (t Timing) SendWithTags(bucket string, tags map[string]string) { - t.c.TimingWithTags(bucket, float64(time.Since(t.start))/float64(time.Millisecond), tags) -} - -// Duration returns the time elapsed since the creation of the Timing. -func (t Timing) Duration() time.Duration { - return now().Sub(t.start) -} - // Unique sends the given value to a set bucket. func (c *Client) Unique(bucket string, value string) { if c.skip() { @@ -159,6 +133,11 @@ func (c *Client) Unique(bucket string, value string) { c.conn.unique(c.prefix, bucket, value, c.tags) } +// Muted tells whether the Clent is muted +func (c *Client) Muted() bool { + return c.muted +} + // Flush flushes the Client's buffer. func (c *Client) Flush() { if c.muted { @@ -181,3 +160,29 @@ func (c *Client) Close() { c.conn.closed = true c.conn.mu.Unlock() } + +// A Timing is an helper object that eases sending timing values. +type Timing struct { + start time.Time + c *Client +} + +// NewTiming creates a new Timing. +func (c *Client) NewTiming() Timing { + return Timing{start: now(), c: c} +} + +// Send sends the time elapsed since the creation of the Timing. +func (t Timing) Send(bucket string) { + t.c.Timing(bucket, int(t.Duration()/time.Millisecond)) +} + +// SendWithTags sends the time elapsed since the creation of the Timing +func (t Timing) SendWithTags(bucket string, tags map[string]string) { + t.c.TimingWithTags(bucket, float64(t.Duration())/float64(time.Millisecond), tags) +} + +// Duration returns the time elapsed since the creation of the Timing. +func (t Timing) Duration() time.Duration { + return time.Since(t.start) +} diff --git a/statsd_test.go b/statsd_test.go index 13bdef2..0b11841 100644 --- a/statsd_test.go +++ b/statsd_test.go @@ -6,6 +6,7 @@ import ( "io" "io/ioutil" "net" + "strings" "sync" "testing" "time" @@ -261,7 +262,7 @@ func TestMaxPacketSize(t *testing.T) { } c.Increment(testKey) - got = conn.buf.String() + got = strings.TrimSuffix(conn.buf.String(), "\n") want := "test_key:1|c" if got != want { t.Errorf("Invalid output, got %q, want %q", got, want) @@ -269,7 +270,7 @@ func TestMaxPacketSize(t *testing.T) { conn.buf.Reset() c.Close() - got = conn.buf.String() + got = strings.TrimSuffix(conn.buf.String(), "\n") if got != want { t.Errorf("Invalid output, got %q, want %q", got, want) } @@ -342,10 +343,7 @@ func TestDialError(t *testing.T) { } defer func() { dialTimeout = net.DialTimeout }() - c, err := New() - if c == nil || !c.muted { - t.Error("New() did not return a muted client") - } + _, err := New() if err == nil { t.Error("New() did not return an error") } @@ -370,11 +368,11 @@ func TestUDPNotListening(t *testing.T) { defer func() { dialTimeout = net.DialTimeout }() c, err := New() - if c == nil || !c.muted { - t.Error("New() did not return a muted client") + if c.muted { + t.Error("client should not mute when attempting to connect to a non-listening port") } - if err == nil { - t.Error("New should return an error") + if err != nil { + t.Error("attempting to connect to a non-listening port should not return an error") } } @@ -461,7 +459,8 @@ func getOutput(c *Client) string { if c.conn.w == nil { return "" } - return getBuffer(c).buf.String() + out := getBuffer(c).buf.String() + return strings.TrimSuffix(out, "\n") } func mockDial(string, string, time.Duration) (net.Conn, error) { @@ -479,7 +478,7 @@ func TestTCP(t *testing.T) { func testNetwork(t *testing.T, network string) { received := make(chan bool) server := newServer(t, network, testAddr, func(p []byte) { - s := string(p) + s := strings.TrimSuffix(string(p), "\n") if s != "test_key:1|c" { t.Errorf("invalid output: %q", s) }