1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

metric: new pkg refactored from log for #90

This commit is contained in:
James Hillyerd
2018-03-31 12:16:54 -07:00
parent 779b1e63af
commit 6601d156be
3 changed files with 30 additions and 29 deletions

View File

@@ -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, ",")
}

View File

@@ -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

View File

@@ -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.