mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-21 11:37:07 +00:00
Add some metric history
The count of connections and count of delivered messages now have 50 minutes of history available in the /about sparklines.
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"github.com/jhillyerd/inbucket/config"
|
||||
"github.com/jhillyerd/inbucket/log"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Real server code starts here
|
||||
@@ -19,7 +21,12 @@ type Server struct {
|
||||
|
||||
var expConnectsTotal = new(expvar.Int)
|
||||
var expConnectsCurrent = new(expvar.Int)
|
||||
var expConnectsHist = new(expvar.String)
|
||||
var expDeliveredTotal = new(expvar.Int)
|
||||
var expDeliveredHist = new(expvar.String)
|
||||
|
||||
var deliveredHist = list.New()
|
||||
var connectsHist = list.New()
|
||||
|
||||
// Init a new Server object
|
||||
func New() *Server {
|
||||
@@ -60,9 +67,34 @@ func (s *Server) Start() {
|
||||
}
|
||||
}
|
||||
|
||||
// When the provided Ticker ticks, we update our metrics history
|
||||
func metricsTicker(t *time.Ticker) {
|
||||
ok := true
|
||||
for ok {
|
||||
_, ok = <-t.C
|
||||
expDeliveredHist.Set(pushMetric(deliveredHist, expDeliveredTotal))
|
||||
expConnectsHist.Set(pushMetric(connectsHist, expConnectsTotal))
|
||||
}
|
||||
}
|
||||
|
||||
// pushMetric adds the metric to the end of the list and returns a comma
|
||||
// separated string of the previous 50 entries
|
||||
func pushMetric(history *list.List, ev expvar.Var) string {
|
||||
history.PushBack(ev.String())
|
||||
if history.Len() > 50 {
|
||||
history.Remove(history.Front())
|
||||
}
|
||||
return JoinStringList(history)
|
||||
}
|
||||
|
||||
func init() {
|
||||
m := expvar.NewMap("smtp")
|
||||
m.Set("connectsTotal", expConnectsTotal)
|
||||
m.Set("connectsHist", expConnectsHist)
|
||||
m.Set("connectsCurrent", expConnectsCurrent)
|
||||
m.Set("deliveredTotal", expDeliveredTotal)
|
||||
m.Set("deliveredHist", expDeliveredHist)
|
||||
|
||||
t := time.NewTicker(time.Minute)
|
||||
go metricsTicker(t)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package smtpd
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -25,3 +26,15 @@ func HashMailboxName(mailbox string) string {
|
||||
io.WriteString(h, mailbox)
|
||||
return fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
||||
|
||||
// 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, ",")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user