forked from kisielk/gostatsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webconsole.go
77 lines (71 loc) · 1.72 KB
/
webconsole.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package statsd
import (
"html/template"
"net/http"
)
// WebConsoleServer is an object that listens for HTTP connection on a TCP address Addr
// and provides a web interface for its MetricAggregator
type WebConsoleServer struct {
Addr string
Aggregator *MetricAggregator
}
const tempText = `
<html>
<head>
<title>gostatsd</title>
<style>
table
{
border-collapse:collapse;
}
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<h1>gostatsd</h1>
<p>Bad lines received: {{.Stats.BadLines}}</p>
<p>Last messsage received: {{.Stats.LastMessage}}</p>
<p>Last flush to graphite: {{.Stats.LastFlush}}</p>
<p>Last error flushing to graphite: {{.Stats.LastFlushError}}</p>
<h2>Counters</h2>
<table>
<tr><th>Bucket</th><th>Value</th></tr>
{{range $bucket, $value := .Counters}}
<tr><td>{{$bucket}}</td><td>{{$value}}</td></tr>
{{end}}
</table>
<h2>Gauges</h2>
<table>
<tr><th>Bucket</th><th>Value</th></tr>
{{range $bucket, $value := .Gauges}}
<tr><td>{{$bucket}}</td><td>{{$value}}</td></tr>
{{end}}
</table>
<h2>Timers</h2>
<table>
<tr><th>Bucket</th><th>Value</th></tr>
{{range $bucket, $value := .Timers}}
<tr><td>{{$bucket}}</td><td>{{$value}}</td></tr>
{{end}}
</table>
</body>
</html>
`
var temp = template.Must(template.New("temp").Parse(tempText))
func (s *WebConsoleServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer s.Aggregator.Unlock()
s.Aggregator.Lock()
err := temp.Execute(w, s.Aggregator)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// ListenAndServe listens on the ConsoleServer's TCP network address and then calls Serve
func (s *WebConsoleServer) ListenAndServe() error {
if s.Addr == "" {
s.Addr = DefaultConsoleAddr
}
return http.ListenAndServe(s.Addr, s)
}