From d07085e800429c6d76f7ae6fb7027a6fa9df2855 Mon Sep 17 00:00:00 2001 From: Adam Mckaig Date: Fri, 6 Mar 2015 19:39:53 -0500 Subject: [PATCH 1/4] Add standard (global) statsd client --- std.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ std_test.go | 1 + 2 files changed, 50 insertions(+) create mode 100644 std.go create mode 100644 std_test.go diff --git a/std.go b/std.go new file mode 100644 index 0000000..89f7c08 --- /dev/null +++ b/std.go @@ -0,0 +1,49 @@ +package statsd + +import "sync" + +var std Statsd +var mu sync.Mutex + +func Init() { + std = &NoopClient{} +} + +// Configure creates a global StatsD client. +// TODO: Use a buffered client instead! +func Configure(host string, prefix string) error { + mu.Lock() + defer mu.Unlock() + + client := NewStatsdClient(host, prefix) + err := client.CreateSocket() + if err != nil { + return err + } + + std = client + return nil +} + +// These functions write to the global StatsD client if one has been configured, +// otherwise do nothing. + +func Incr(stat string, count int64) error { + return std.Incr(stat, count) +} + +func Decr(stat string, count int64) error { + return std.Decr(stat, count) +} + +func Timing(stat string, delta int64) error { + return std.Timing(stat, delta) +} + +func Absolute(stat string, value int64) error { + return std.Absolute(stat, value) +} + +func Total(stat string, value int64) error { + return std.Total(stat, value) +} diff --git a/std_test.go b/std_test.go new file mode 100644 index 0000000..872efc4 --- /dev/null +++ b/std_test.go @@ -0,0 +1 @@ +package statsd From 425af4620f4290f691c3ec5a14a76b494bb123d4 Mon Sep 17 00:00:00 2001 From: Adam Mckaig Date: Mon, 9 Mar 2015 16:01:34 -0400 Subject: [PATCH 2/4] Fix std initialization --- std.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std.go b/std.go index 89f7c08..2808b8b 100644 --- a/std.go +++ b/std.go @@ -5,7 +5,7 @@ import "sync" var std Statsd var mu sync.Mutex -func Init() { +func init() { std = &NoopClient{} } From 9b1d0795abba562b614df8db9f23dbe00375808b Mon Sep 17 00:00:00 2001 From: Adam Mckaig Date: Mon, 9 Mar 2015 16:05:32 -0400 Subject: [PATCH 3/4] Add nasty Configure test --- std.go | 8 +++++++- std_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/std.go b/std.go index 2808b8b..89d80f6 100644 --- a/std.go +++ b/std.go @@ -6,7 +6,7 @@ var std Statsd var mu sync.Mutex func init() { - std = &NoopClient{} + Unconfigure() } // Configure creates a global StatsD client. @@ -25,6 +25,12 @@ func Configure(host string, prefix string) error { return nil } +// Unconfigure resets the global StatsD client to its default state, which is +// to silently drop all events. +func Unconfigure() { + std = &NoopClient{} +} + // These functions write to the global StatsD client if one has been configured, // otherwise do nothing. diff --git a/std_test.go b/std_test.go index 872efc4..aa40e56 100644 --- a/std_test.go +++ b/std_test.go @@ -1 +1,29 @@ package statsd + +import ( + "testing" +) + +func TestConfigure(t *testing.T) { + + // assert that global is a noop client + _, ok := std.(*NoopClient) + if !ok { + t.Errorf("expected std to be a NoopClient, got a %#v", std) + } + + // assert that after calling Configure, the global is a StatsdClient + Configure("localhost:8888", "prefix") + _, ok = std.(*StatsdClient) + if !ok { + t.Errorf("expected std to be a StatsdClient, got a %#v", std) + } + + // assert that after calling Unconfigure, the global is back to being a + // NoopClient + Unconfigure() + _, ok = std.(*NoopClient) + if !ok { + t.Errorf("expected std to be a NoopClient, got a %#v", std) + } +} From 929d0a7f976acd4eccab11c8a5b92a5ae60040c2 Mon Sep 17 00:00:00 2001 From: Adam Mckaig Date: Mon, 9 Mar 2015 16:12:35 -0400 Subject: [PATCH 4/4] go fmt --- std_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/std_test.go b/std_test.go index aa40e56..54e7c73 100644 --- a/std_test.go +++ b/std_test.go @@ -8,7 +8,7 @@ func TestConfigure(t *testing.T) { // assert that global is a noop client _, ok := std.(*NoopClient) - if !ok { + if !ok { t.Errorf("expected std to be a NoopClient, got a %#v", std) }