-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
317 lines (262 loc) · 6.81 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"container/ring"
"crypto/tls"
"fmt"
"net/http"
"os"
"path"
"runtime"
"time"
"code.cloudfoundry.org/cli/cf/terminal"
"code.cloudfoundry.org/cli/cf/trace"
"code.cloudfoundry.org/cli/plugin"
"github.com/cloudfoundry/noaa/consumer"
"github.com/cloudfoundry/sonde-go/events"
"github.com/gorilla/websocket"
"github.com/simonleung8/flags"
)
var usage = "cf logalyze APP_NAME --agg-window 2 --buffer-size 100 -p 8080"
// LogalyzerCmd struct for the plugin
type LogalyzerCmd struct {
ui terminal.UI
sendChan chan []byte
logCount int
errCount int
happy bool
movingAverage float64
errorRate float64
}
// GetMetadata shows metadata for the plugin
func (c *LogalyzerCmd) GetMetadata() plugin.PluginMetadata {
return plugin.PluginMetadata{
Name: "logalyzer",
Version: plugin.VersionType{
Major: 1,
Minor: 1,
Build: 0,
},
MinCliVersion: plugin.VersionType{
Major: 6,
Minor: 23,
Build: 0,
},
Commands: []plugin.Command{
{
Name: "logalyze",
HelpText: "Command to analyze the application's log output",
UsageDetails: plugin.Usage{
Usage: usage,
},
},
},
}
}
func main() {
plugin.Start(new(LogalyzerCmd))
}
// Run will be executed when cf logalyze gets invoked
func (c *LogalyzerCmd) Run(cliConnection plugin.CliConnection, args []string) {
if args[0] != "logalyze" {
return
}
port := 8080
aggregationWindow := 2 * time.Second
bufferSize := 100
traceLogger := trace.NewLogger(os.Stdout, false, os.Getenv("CF_TRACE"), "")
c.ui = terminal.NewUI(os.Stdin, os.Stdout, terminal.NewTeePrinter(os.Stdout), traceLogger)
c.sendChan = make(chan []byte, 256)
if len(args) < 2 {
c.ui.Say("Usage: %s\n", usage)
c.ui.Failed("No App Name given")
return
}
loggedIn, err := cliConnection.IsLoggedIn()
if err != nil {
c.ui.Failed(err.Error())
return
}
if !loggedIn {
c.ui.Failed("You have to be logged in")
return
}
fc := flags.New()
fc.NewStringFlag("password", "p", "flag for password") //name, short_name and usage of the string flag
fc.NewIntFlag("buffer-size", "b", "How many past values should be retained for the moving average, default 100")
fc.NewIntFlag("agg-window", "a", "Over how many seconds should the current error rate be calculated, default 2")
fc.NewIntFlag("port", "p", "Port to use, default: 8080")
err = fc.Parse(args[2:]...)
if err != nil {
c.ui.Failed(err.Error())
return
}
if fc.IsSet("p") {
port = fc.Int("p")
}
if fc.IsSet("a") {
aggregationWindow = time.Duration(fc.Int("a")) * time.Second
}
if fc.IsSet("b") {
bufferSize = fc.Int("b")
}
appName := args[1]
dopplerEndpoint, err := cliConnection.DopplerEndpoint()
if err != nil {
c.ui.Failed(err.Error())
return
}
appModel, err := cliConnection.GetApp(appName)
if err != nil {
c.ui.Failed(err.Error())
return
}
authToken, err := cliConnection.AccessToken()
if err != nil {
c.ui.Failed(err.Error())
return
}
isSSLDisabled, err := cliConnection.IsSSLDisabled()
if err != nil {
c.ui.Failed(err.Error())
return
}
consumer := consumer.New(dopplerEndpoint, &tls.Config{InsecureSkipVerify: isSSLDisabled}, nil)
consumer.SetDebugPrinter(ConsoleDebugPrinter{traceLogger})
outputChan, errorChan := consumer.Stream(appModel.Guid, authToken)
go c.happyCalcer(aggregationWindow, bufferSize)
go c.startServer(c.getAssetsDir(), port)
c.ui.Say("Webserver is started at http://localhost:%d", port)
for {
select {
case log := <-outputChan:
if log.GetEventType() == events.Envelope_LogMessage {
if log.GetLogMessage().GetMessageType() == events.LogMessage_ERR {
c.errCount++
}
c.logCount++
c.sendChan <- log.GetLogMessage().GetMessage()
}
case err := <-errorChan:
c.ui.Failed(err.Error())
}
}
}
func (c *LogalyzerCmd) happyCalcer(aggregationWindow time.Duration, bufferSize int) {
ticker := time.NewTicker(aggregationWindow)
r := ring.New(bufferSize)
r.Value = float64(0)
r = r.Next()
oldTotal := 0
oldError := 0
for {
select {
case <-ticker.C:
currentTotal := c.logCount
currentError := c.errCount
totalDiff := float64(currentTotal - oldTotal)
errorDiff := float64(currentError - oldError)
if totalDiff != 0 {
r.Value = errorDiff / totalDiff
} else {
r.Value = nil
}
var movingSum float64
var numSamples int
r.Do(func(o interface{}) {
if o == nil {
return
}
numSamples++
movingSum += o.(float64)
})
var movingAverage float64
if numSamples != 0 {
movingAverage = movingSum / float64(numSamples)
} else {
movingAverage = 0
}
if r.Value != nil {
c.happy = (r.Value.(float64) <= movingAverage)
c.errorRate = r.Value.(float64)
}
c.movingAverage = movingAverage
r = r.Next()
oldTotal = currentTotal
oldError = currentError
}
}
}
type connection struct {
// The websocket connection.
ws *websocket.Conn
// Buffered channel of outbound messages.
send chan []byte
}
func (c *LogalyzerCmd) serveWs(rw http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(rw, "Method not allowed", 405)
return
}
upgrader := websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
ws, err := upgrader.Upgrade(rw, r, nil)
if err != nil {
c.ui.Failed(err.Error())
return
}
conn := &connection{send: c.sendChan, ws: ws}
conn.writePump()
}
func (c *LogalyzerCmd) startServer(filename string, port int) {
http.HandleFunc("/ws", c.serveWs)
http.HandleFunc("/counts", func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte(fmt.Sprintf("{\"logCount\": %d, \"errCount\": %d, \"happy\": %t, \"movingAverage\": %f, \"errorRate\": %f}", c.logCount, c.errCount, c.happy, c.movingAverage, c.errorRate)))
})
p := path.Join(path.Dir(filename), "assets")
http.Handle("/", http.FileServer(http.Dir(p)))
address := fmt.Sprintf(":%d", port)
err := http.ListenAndServe(address, nil)
c.ui.Failed(err.Error())
}
func (c *LogalyzerCmd) getAssetsDir() string {
_, filename, _, _ := runtime.Caller(1)
return filename
}
func (c *connection) writePump() {
ticker := time.NewTicker(5 * time.Second)
defer func() {
ticker.Stop()
c.ws.Close()
}()
for {
select {
case message, ok := <-c.send:
if !ok {
c.write(websocket.CloseMessage, []byte{})
return
}
if err := c.write(websocket.TextMessage, message); err != nil {
return
}
case <-ticker.C:
if err := c.write(websocket.PingMessage, []byte{}); err != nil {
return
}
}
}
}
func (c *connection) write(mt int, payload []byte) error {
c.ws.SetWriteDeadline(time.Now().Add(10 * time.Second))
return c.ws.WriteMessage(mt, payload)
}
// ConsoleDebugPrinter struct for printing to Console
type ConsoleDebugPrinter struct {
logger trace.Printer
}
// Print prints logs to the console
func (c ConsoleDebugPrinter) Print(title, dump string) {
c.logger.Print(title)
c.logger.Print(dump)
}