mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
metric: new pkg refactored from log for #90
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package log
|
package metric
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/list"
|
"container/list"
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"time"
|
"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()
|
type TickerFunc func()
|
||||||
|
|
||||||
var tickerFuncChan = make(chan TickerFunc)
|
var tickerFuncChan = make(chan TickerFunc)
|
||||||
@@ -22,10 +22,10 @@ func AddTickerFunc(f TickerFunc) {
|
|||||||
tickerFuncChan <- f
|
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
|
// 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.
|
// 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())
|
history.PushBack(ev.String())
|
||||||
if history.Len() > 61 {
|
if history.Len() > 61 {
|
||||||
history.Remove(history.Front())
|
history.Remove(history.Front())
|
||||||
@@ -33,18 +33,7 @@ func PushMetric(history *list.List, ev expvar.Var) string {
|
|||||||
return joinStringList(history)
|
return joinStringList(history)
|
||||||
}
|
}
|
||||||
|
|
||||||
// joinStringList joins a List containing strings by commas
|
// metricsTicker calls the current list of TickerFuncs once per minute.
|
||||||
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, ",")
|
|
||||||
}
|
|
||||||
|
|
||||||
func metricsTicker() {
|
func metricsTicker() {
|
||||||
funcs := make([]TickerFunc, 0)
|
funcs := make([]TickerFunc, 0)
|
||||||
ticker := time.NewTicker(time.Minute)
|
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, ",")
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/jhillyerd/inbucket/pkg/config"
|
"github.com/jhillyerd/inbucket/pkg/config"
|
||||||
"github.com/jhillyerd/inbucket/pkg/message"
|
"github.com/jhillyerd/inbucket/pkg/message"
|
||||||
|
"github.com/jhillyerd/inbucket/pkg/metric"
|
||||||
"github.com/jhillyerd/inbucket/pkg/policy"
|
"github.com/jhillyerd/inbucket/pkg/policy"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
@@ -27,13 +28,12 @@ func init() {
|
|||||||
m.Set("WarnsTotal", expWarnsTotal)
|
m.Set("WarnsTotal", expWarnsTotal)
|
||||||
m.Set("WarnsHist", expWarnsHist)
|
m.Set("WarnsHist", expWarnsHist)
|
||||||
|
|
||||||
// TODO #90 move elsewhere
|
metric.AddTickerFunc(func() {
|
||||||
// log.AddTickerFunc(func() {
|
expReceivedHist.Set(metric.Push(deliveredHist, expReceivedTotal))
|
||||||
// expReceivedHist.Set(log.PushMetric(deliveredHist, expReceivedTotal))
|
expConnectsHist.Set(metric.Push(connectsHist, expConnectsTotal))
|
||||||
// expConnectsHist.Set(log.PushMetric(connectsHist, expConnectsTotal))
|
expErrorsHist.Set(metric.Push(errorsHist, expErrorsTotal))
|
||||||
// expErrorsHist.Set(log.PushMetric(errorsHist, expErrorsTotal))
|
expWarnsHist.Set(metric.Push(warnsHist, expWarnsTotal))
|
||||||
// expWarnsHist.Set(log.PushMetric(warnsHist, expWarnsTotal))
|
})
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server holds the configuration and state of our SMTP server
|
// Server holds the configuration and state of our SMTP server
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jhillyerd/inbucket/pkg/config"
|
"github.com/jhillyerd/inbucket/pkg/config"
|
||||||
|
"github.com/jhillyerd/inbucket/pkg/metric"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,12 +43,11 @@ func init() {
|
|||||||
rm.Set("RetainedSize", expRetainedSize)
|
rm.Set("RetainedSize", expRetainedSize)
|
||||||
rm.Set("SizeHist", expSizeHist)
|
rm.Set("SizeHist", expSizeHist)
|
||||||
|
|
||||||
// TODO #90 move
|
metric.AddTickerFunc(func() {
|
||||||
// log.AddTickerFunc(func() {
|
expRetentionDeletesHist.Set(metric.Push(retentionDeletesHist, expRetentionDeletesTotal))
|
||||||
// expRetentionDeletesHist.Set(log.PushMetric(retentionDeletesHist, expRetentionDeletesTotal))
|
expRetainedHist.Set(metric.Push(retainedHist, expRetainedCurrent))
|
||||||
// expRetainedHist.Set(log.PushMetric(retainedHist, expRetainedCurrent))
|
expSizeHist.Set(metric.Push(sizeHist, expRetainedSize))
|
||||||
// expSizeHist.Set(log.PushMetric(sizeHist, expRetainedSize))
|
})
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RetentionScanner looks for messages older than the configured retention period and deletes them.
|
// RetentionScanner looks for messages older than the configured retention period and deletes them.
|
||||||
|
|||||||
Reference in New Issue
Block a user