From 6601d156be2d066bcf1551d0953cf207f4da6b81 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sat, 31 Mar 2018 12:16:54 -0700 Subject: [PATCH] metric: new pkg refactored from log for #90 --- pkg/{log/metrics.go => metric/metric.go} | 33 ++++++++++++------------ pkg/server/smtp/listener.go | 14 +++++----- pkg/storage/retention.go | 12 ++++----- 3 files changed, 30 insertions(+), 29 deletions(-) rename pkg/{log/metrics.go => metric/metric.go} (74%) diff --git a/pkg/log/metrics.go b/pkg/metric/metric.go similarity index 74% rename from pkg/log/metrics.go rename to pkg/metric/metric.go index c16f1e8..566445d 100644 --- a/pkg/log/metrics.go +++ b/pkg/metric/metric.go @@ -1,4 +1,4 @@ -package log +package metric import ( "container/list" @@ -7,7 +7,7 @@ import ( "time" ) -// TickerFunc is the type of metrics function accepted by AddTickerFunc +// TickerFunc is the function signature accepted by AddTickerFunc, will be called once per minute. type TickerFunc func() var tickerFuncChan = make(chan TickerFunc) @@ -22,10 +22,10 @@ func AddTickerFunc(f TickerFunc) { tickerFuncChan <- f } -// PushMetric adds the metric to the end of the list and returns a comma separated string of the +// Push adds the metric to the end of the list and returns a comma separated string of the // previous 61 entries. We return 61 instead of 60 (an hour) because the chart on the client // tracks deltas between these values - there is nothing to compare the first value against. -func PushMetric(history *list.List, ev expvar.Var) string { +func Push(history *list.List, ev expvar.Var) string { history.PushBack(ev.String()) if history.Len() > 61 { history.Remove(history.Front()) @@ -33,18 +33,7 @@ func PushMetric(history *list.List, ev expvar.Var) string { return joinStringList(history) } -// joinStringList joins a List containing strings by commas -func joinStringList(listOfStrings *list.List) string { - if listOfStrings.Len() == 0 { - return "" - } - s := make([]string, 0, listOfStrings.Len()) - for e := listOfStrings.Front(); e != nil; e = e.Next() { - s = append(s, e.Value.(string)) - } - return strings.Join(s, ",") -} - +// metricsTicker calls the current list of TickerFuncs once per minute. func metricsTicker() { funcs := make([]TickerFunc, 0) ticker := time.NewTicker(time.Minute) @@ -60,3 +49,15 @@ func metricsTicker() { } } } + +// joinStringList joins a List containing strings by commas. +func joinStringList(listOfStrings *list.List) string { + if listOfStrings.Len() == 0 { + return "" + } + s := make([]string, 0, listOfStrings.Len()) + for e := listOfStrings.Front(); e != nil; e = e.Next() { + s = append(s, e.Value.(string)) + } + return strings.Join(s, ",") +} diff --git a/pkg/server/smtp/listener.go b/pkg/server/smtp/listener.go index d566e15..e7fd095 100644 --- a/pkg/server/smtp/listener.go +++ b/pkg/server/smtp/listener.go @@ -11,6 +11,7 @@ import ( "github.com/jhillyerd/inbucket/pkg/config" "github.com/jhillyerd/inbucket/pkg/message" + "github.com/jhillyerd/inbucket/pkg/metric" "github.com/jhillyerd/inbucket/pkg/policy" "github.com/rs/zerolog/log" ) @@ -27,13 +28,12 @@ func init() { m.Set("WarnsTotal", expWarnsTotal) m.Set("WarnsHist", expWarnsHist) - // TODO #90 move elsewhere - // log.AddTickerFunc(func() { - // expReceivedHist.Set(log.PushMetric(deliveredHist, expReceivedTotal)) - // expConnectsHist.Set(log.PushMetric(connectsHist, expConnectsTotal)) - // expErrorsHist.Set(log.PushMetric(errorsHist, expErrorsTotal)) - // expWarnsHist.Set(log.PushMetric(warnsHist, expWarnsTotal)) - // }) + metric.AddTickerFunc(func() { + expReceivedHist.Set(metric.Push(deliveredHist, expReceivedTotal)) + expConnectsHist.Set(metric.Push(connectsHist, expConnectsTotal)) + expErrorsHist.Set(metric.Push(errorsHist, expErrorsTotal)) + expWarnsHist.Set(metric.Push(warnsHist, expWarnsTotal)) + }) } // Server holds the configuration and state of our SMTP server diff --git a/pkg/storage/retention.go b/pkg/storage/retention.go index 6a0b714..166da6b 100644 --- a/pkg/storage/retention.go +++ b/pkg/storage/retention.go @@ -7,6 +7,7 @@ import ( "time" "github.com/jhillyerd/inbucket/pkg/config" + "github.com/jhillyerd/inbucket/pkg/metric" "github.com/rs/zerolog/log" ) @@ -42,12 +43,11 @@ func init() { rm.Set("RetainedSize", expRetainedSize) rm.Set("SizeHist", expSizeHist) - // TODO #90 move - // log.AddTickerFunc(func() { - // expRetentionDeletesHist.Set(log.PushMetric(retentionDeletesHist, expRetentionDeletesTotal)) - // expRetainedHist.Set(log.PushMetric(retainedHist, expRetainedCurrent)) - // expSizeHist.Set(log.PushMetric(sizeHist, expRetainedSize)) - // }) + metric.AddTickerFunc(func() { + expRetentionDeletesHist.Set(metric.Push(retentionDeletesHist, expRetentionDeletesTotal)) + expRetainedHist.Set(metric.Push(retainedHist, expRetainedCurrent)) + expSizeHist.Set(metric.Push(sizeHist, expRetainedSize)) + }) } // RetentionScanner looks for messages older than the configured retention period and deletes them.