Skip to content

Commit

Permalink
metric: new pkg refactored from log for #90
Browse files Browse the repository at this point in the history
  • Loading branch information
jhillyerd committed Mar 31, 2018
1 parent 779b1e6 commit 6601d15
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
33 changes: 17 additions & 16 deletions pkg/log/metrics.go → pkg/metric/metric.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package log
package metric

import (
"container/list"
Expand All @@ -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)
Expand All @@ -22,29 +22,18 @@ 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())
}
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)
Expand All @@ -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, ",")
}
14 changes: 7 additions & 7 deletions pkg/server/smtp/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/storage/retention.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/metric"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6601d15

Please sign in to comment.