-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (127 loc) · 3.99 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bufio"
"dd-logstats/engine"
"dd-logstats/ui"
"flag"
"net/http"
"os"
"syscall"
"time"
"github.com/golang/glog"
"github.com/zenazn/goji/graceful"
"github.com/zenazn/goji/web"
)
const (
logEntryBufferSize = 1024
hitTreshold = 50
)
var flagAlarmTimeFrame time.Duration
var flagAlarmThreshold uint64
var flagStatsPeriod time.Duration
var flagServeAddr string
func init() {
flag.DurationVar(&flagAlarmTimeFrame, "alarm-timeframe", time.Duration(2*time.Minute), "consider the total hits for this period of time when raising an alarm")
flag.Uint64Var(&flagAlarmThreshold, "alarm-threshold", 100, "raise an alarm when the average hit count between [now - alarm_timeframe; now] exceeds the given value")
flag.DurationVar(&flagStatsPeriod, "stats-period", time.Duration(10*time.Second), "statistics aggregation period")
flag.StringVar(&flagServeAddr, "serve", ":8080", "address to serve requests")
}
func readLogs(out chan *engine.LogEntry) {
glog.Info("Reading log lines from standard input")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
obj, err := engine.NewLogEntry(line)
if err != nil {
glog.Errorf("Error decoding line from stdin: %s", err)
continue
}
out <- obj
}
err := scanner.Err()
if err != nil {
glog.Errorf("Error reading line from stdin (%s)", err)
}
}
func runTrackers(logs chan *engine.LogEntry, context ui.Renderer, quit, done chan bool) {
stats := engine.NewStats()
uiState := ui.State{}
ht := engine.NewHitTracker(uint64(flagAlarmTimeFrame/flagStatsPeriod), flagAlarmThreshold)
ticker := time.NewTicker(flagStatsPeriod)
for {
select {
case <-ticker.C:
ht.AddHits(stats.TotalHits)
stats.Finalize()
// Update state
uiState.Update(stats, ht.IsAboveThreshold(), ht.AverageHitCount())
// Render template
err := context.Render(&uiState)
if err != nil {
glog.Errorf("Error rendering template: %s", err)
}
stats = engine.NewStats()
case l := <-logs:
stats.AddLogEntry(l)
case <-quit:
done <- true
}
}
}
type appHandler struct {
ui.Renderer
Handle func(ui.Renderer, http.ResponseWriter, *http.Request) (int, error)
}
func logger(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
glog.Infoln(r.RemoteAddr, r.Method, r.RequestURI)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (ah appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
status, err := ah.Handle(ah.Renderer, w, r)
if err != nil {
glog.Errorf("HTTP %d: %q", status, err)
switch status {
case http.StatusNotFound:
http.NotFound(w, r)
case http.StatusInternalServerError:
http.Error(w, http.StatusText(status), status)
default:
http.Error(w, http.StatusText(status), status)
}
}
}
func indexHandler(renderer ui.Renderer, w http.ResponseWriter, r *http.Request) (int, error) {
return w.Write([]byte(renderer.Result()))
}
func main() {
// force logging to stderr (glog)
flag.Set("logtostderr", "true")
flag.Parse()
glog.Infoln("Hits statistics are refreshed every", flagStatsPeriod)
glog.Infoln("Alarm is raised when average hit count over the last", flagAlarmTimeFrame, "exceeds", flagAlarmThreshold, "hits")
logs := make(chan *engine.LogEntry, logEntryBufferSize)
quit, done := make(chan bool), make(chan bool)
context, err := ui.NewRenderer("ui/assets", flagStatsPeriod, flagAlarmThreshold, flagAlarmTimeFrame)
if err != nil {
glog.Errorf("Failed to setup template renderer (%s)", err)
os.Exit(1)
}
go runTrackers(logs, context, quit, done)
go readLogs(logs)
mux := web.New()
mux.Use(logger)
mux.Get("/", appHandler{context, indexHandler})
mux.Get("/assets/*", http.FileServer(http.Dir("ui")))
glog.Infoln("User interface served on", flagServeAddr, "(HTTP)")
graceful.AddSignal(syscall.SIGTERM, syscall.SIGINT)
err = graceful.ListenAndServe(flagServeAddr, mux)
if err != nil {
glog.Errorf("Unable to start webserver (%s)", err)
}
glog.Infoln("Shutting down. Waiting for goroutines to end.")
close(quit)
<-done
}