From b2343c5ce3a08e0b8224eaba535fad342df4fafc Mon Sep 17 00:00:00 2001 From: Michael Baird Date: Tue, 16 May 2017 10:52:10 -0500 Subject: [PATCH] Stats uses the same logger output as the cluster --- datadog/datadog.go | 18 +++++++++++------- server/server.go | 2 ++ stats.go | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/datadog/datadog.go b/datadog/datadog.go index 8fefd78de..17b0d817f 100644 --- a/datadog/datadog.go +++ b/datadog/datadog.go @@ -40,10 +40,9 @@ var _ pilosa.StatsClient = &StatsClient{} // StatsClient represents a DataDog implementation of pilosa.StatsClient. type StatsClient struct { - client *statsd.Client - tags []string - - LogOutput io.Writer + client *statsd.Client + tags []string + logOutput io.Writer } // NewStatsClient returns a new instance of StatsClient. @@ -56,7 +55,7 @@ func NewStatsClient(host string) (*StatsClient, error) { return &StatsClient{ client: c, - LogOutput: ioutil.Discard, + logOutput: ioutil.Discard, }, nil } @@ -75,7 +74,7 @@ func (c *StatsClient) WithTags(tags ...string) pilosa.StatsClient { return &StatsClient{ client: c.client, tags: pilosa.UnionStringSlice(c.tags, tags), - LogOutput: c.LogOutput, + logOutput: c.logOutput, } } @@ -122,7 +121,12 @@ func (c *StatsClient) Timing(name string, value time.Duration, rate float64) { } } +// SetLogger has no logger +func (c *StatsClient) SetLogger(logger io.Writer) { + c.logOutput = logger +} + // logger returns a logger that writes to LogOutput func (c *StatsClient) logger() *log.Logger { - return log.New(c.LogOutput, "", log.LstdFlags) + return log.New(c.logOutput, "", log.LstdFlags) } diff --git a/server/server.go b/server/server.go index a3a6d060e..ff208bb4a 100644 --- a/server/server.go +++ b/server/server.go @@ -141,6 +141,8 @@ func (m *Command) SetupServer() error { return err } + m.Server.Holder.Stats.SetLogger(m.Server.LogOutput) + // Copy configuration flags. m.Server.MaxWritesPerRequest = m.Config.MaxWritesPerRequest diff --git a/stats.go b/stats.go index e6130ca62..1c85382a6 100644 --- a/stats.go +++ b/stats.go @@ -16,6 +16,7 @@ package pilosa import ( "expvar" + "io" "sort" "strings" "sync" @@ -54,6 +55,9 @@ type StatsClient interface { // Tracks timing information for a metric. Timing(name string, value time.Duration, rate float64) + + // SetLogger Set the logger output type + SetLogger(logger io.Writer) } // NopStatsClient represents a client that doesn't do anything. @@ -69,6 +73,7 @@ func (c *nopStatsClient) Gauge(name string, value float64, rate float64) func (c *nopStatsClient) Histogram(name string, value float64, rate float64) {} func (c *nopStatsClient) Set(name string, value string, rate float64) {} func (c *nopStatsClient) Timing(name string, value time.Duration, rate float64) {} +func (c *nopStatsClient) SetLogger(logger io.Writer) {} // ExpvarStatsClient writes stats out to expvars. type ExpvarStatsClient struct { @@ -138,6 +143,10 @@ func (c *ExpvarStatsClient) Timing(name string, value time.Duration, rate float6 c.mu.Unlock() } +// SetLogger has no logger +func (c *ExpvarStatsClient) SetLogger(logger io.Writer) { +} + // MultiStatsClient joins multiple stats clients together. type MultiStatsClient []StatsClient @@ -200,6 +209,13 @@ func (a MultiStatsClient) Timing(name string, value time.Duration, rate float64) } } +// SetLogger Sets the StatsD logger output type +func (a MultiStatsClient) SetLogger(logger io.Writer) { + for _, c := range a { + c.SetLogger(logger) + } +} + // UnionStringSlice returns a sorted set of tags which combine a & b. func UnionStringSlice(a, b []string) []string { // Sort both sets first.