From bf0d6a6be5e0a75f031a46aa133fbea712a87cdc Mon Sep 17 00:00:00 2001
From: James Hillyerd
Date: Tue, 23 Oct 2012 19:48:59 -0700
Subject: [PATCH] Add some metric history
The count of connections and count of delivered messages now have 50
minutes of history available in the /about sparklines.
---
smtpd/listener.go | 32 +++++++++++++++++++++
smtpd/utils.go | 13 +++++++++
themes/integral/public/main.css | 9 ++++++
themes/integral/templates/root/about.html | 35 +++++++++++++++++++----
4 files changed, 84 insertions(+), 5 deletions(-)
diff --git a/smtpd/listener.go b/smtpd/listener.go
index 4b6cd24..0deb38b 100644
--- a/smtpd/listener.go
+++ b/smtpd/listener.go
@@ -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)
}
diff --git a/smtpd/utils.go b/smtpd/utils.go
index 1bef50d..e0d93a8 100644
--- a/smtpd/utils.go
+++ b/smtpd/utils.go
@@ -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, ",")
+}
diff --git a/themes/integral/public/main.css b/themes/integral/public/main.css
index f77ea22..0d2d3ae 100644
--- a/themes/integral/public/main.css
+++ b/themes/integral/public/main.css
@@ -317,4 +317,13 @@ table.metrics {
.metrics th {
text-align: left;
+ width: 15em;
+}
+
+.metrics td {
+ width: 10em;
+}
+
+.metrics td.sparkline {
+ width: 170px;
}
diff --git a/themes/integral/templates/root/about.html b/themes/integral/templates/root/about.html
index 93bc195..6d411cf 100644
--- a/themes/integral/templates/root/about.html
+++ b/themes/integral/templates/root/about.html
@@ -53,6 +53,20 @@
}
}
+ function setHistory(name, value) {
+ var h = value.split(",")
+ var prev = parseInt(h[0])
+ for (i=0; i
| System Memory: |
. |
- . |
+ . |
+ (5s) |
| Heap Capacity: |
. |
- . |
+ . |
+ (5s) |
| Heap In-Use: |
. |
- . |
+ . |
+ (5s) |
| Heap # Objects: |
. |
- . |
+ . |
+ (5s) |
@@ -142,15 +162,20 @@ address and make it available to view without a password.
| Current Connections: |
. |
- . |
+ . |
+ (5s) |
| Total Connections: |
. |
+ . |
+ (60s) |
| Messages Delivered: |
. |
+ . |
+ (60s) |