mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +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, ",")
|
||||
}
|
||||
|
||||
@@ -317,4 +317,13 @@ table.metrics {
|
||||
|
||||
.metrics th {
|
||||
text-align: left;
|
||||
width: 15em;
|
||||
}
|
||||
|
||||
.metrics td {
|
||||
width: 10em;
|
||||
}
|
||||
|
||||
.metrics td.sparkline {
|
||||
width: 170px;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,20 @@
|
||||
}
|
||||
}
|
||||
|
||||
function setHistory(name, value) {
|
||||
var h = value.split(",")
|
||||
var prev = parseInt(h[0])
|
||||
for (i=0; i<h.length; i++) {
|
||||
var t = parseInt(h[i])
|
||||
h[i] = t-prev
|
||||
prev = t
|
||||
}
|
||||
el = $('#s-' + name)
|
||||
if (el) {
|
||||
el.sparkline(h)
|
||||
}
|
||||
}
|
||||
|
||||
function metric(name, value, filter, chartable) {
|
||||
if (chartable) {
|
||||
appendHistory(name, value)
|
||||
@@ -77,6 +91,8 @@
|
||||
metric('smtpConnectsTotal', data.smtp.connectsTotal, numberFilter, false)
|
||||
metric('smtpConnectsCurrent', data.smtp.connectsCurrent, numberFilter, true)
|
||||
metric('smtpDeliveredTotal', data.smtp.deliveredTotal, numberFilter, false)
|
||||
setHistory('smtpConnectsTotal', data.smtp.connectsHist)
|
||||
setHistory('smtpDeliveredTotal', data.smtp.deliveredHist)
|
||||
}
|
||||
|
||||
function loadMetrics() {
|
||||
@@ -116,22 +132,26 @@ address and make it available to view without a password.</p>
|
||||
<tr>
|
||||
<th>System Memory:</th>
|
||||
<td><span id="m-memstatsSys">.</span></td>
|
||||
<td><span id="s-memstatsSys">.</span></td>
|
||||
<td class="sparkline"><span id="s-memstatsSys">.</span></td>
|
||||
<td>(5s)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Heap Capacity:</th>
|
||||
<td><span id="m-memstatsHeapSys">.</span></td>
|
||||
<td><span id="s-memstatsHeapSys">.</span></td>
|
||||
<td class="sparkline"><span id="s-memstatsHeapSys">.</span></td>
|
||||
<td>(5s)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Heap In-Use:</th>
|
||||
<td><span id="m-memstatsHeapAlloc">.</span></td>
|
||||
<td><span id="s-memstatsHeapAlloc">.</span></td>
|
||||
<td class="sparkline"><span id="s-memstatsHeapAlloc">.</span></td>
|
||||
<td>(5s)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Heap # Objects:</th>
|
||||
<td><span id="m-memstatsHeapObjects">.</span></td>
|
||||
<td><span id="s-memstatsHeapObjects">.</span></td>
|
||||
<td class="sparkline"><span id="s-memstatsHeapObjects">.</span></td>
|
||||
<td>(5s)</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p class="last"> </p>
|
||||
@@ -142,15 +162,20 @@ address and make it available to view without a password.</p>
|
||||
<tr>
|
||||
<th>Current Connections:</th>
|
||||
<td><span id="m-smtpConnectsCurrent">.</span></td>
|
||||
<td><span id="s-smtpConnectsCurrent">.</span></td>
|
||||
<td class="sparkline"><span id="s-smtpConnectsCurrent">.</span></td>
|
||||
<td>(5s)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Total Connections:</th>
|
||||
<td><span id="m-smtpConnectsTotal">.</span></td>
|
||||
<td class="sparkline"><span id="s-smtpConnectsTotal">.</span></td>
|
||||
<td>(60s)</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>Messages Delivered:</th>
|
||||
<td><span id="m-smtpDeliveredTotal">.</span></td>
|
||||
<td class="sparkline"><span id="s-smtpDeliveredTotal">.</span></td>
|
||||
<td>(60s)</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p class="last"> </p>
|
||||
|
||||
Reference in New Issue
Block a user