-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloglistener.go
181 lines (171 loc) · 4.97 KB
/
loglistener.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
package main
import (
"bytes"
"container/list"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"sync"
"github.com/gin-gonic/gin"
"github.com/nats-io/nats.go"
"github.com/tidwall/gjson"
)
type logRecord struct {
TransactionID string
Router string
Time string
Latency int
StatusCode int
body string
}
type threadLogList struct {
listOfLogs *list.List
m *sync.Mutex
visibleLogs []logRecord
length int
}
func loggerSubscriberFunc(logList *threadLogList, natsEnabled *bool) (func(msg *nats.Msg), error) {
if logList == nil {
return nil, fmt.Errorf("logList is nil")
}
if logList.listOfLogs == nil {
return nil, fmt.Errorf("logList.listOfLogs is nil")
}
if logList.m == nil {
return nil, fmt.Errorf("logList.m is nil")
}
if natsEnabled == nil {
return nil, fmt.Errorf("natsEnabled is nil")
}
return func(msg *nats.Msg) {
logger.Debug(
"receiver: received message",
slog.String("message", string(msg.Data)),
)
logList.m.Lock()
if logList.listOfLogs.Len() >= logList.length {
last := logList.listOfLogs.Back()
if last == nil {
logger.Error("There is no last object. Abort", slog.Int("list length", logList.listOfLogs.Len()))
*natsEnabled = false
return
}
logList.listOfLogs.Remove(last)
logger.Debug("removed object", slog.Int("list length", logList.listOfLogs.Len()))
}
logList.listOfLogs.PushFront(string(msg.Data))
logger.Debug("Added to list", slog.Int("list length", logList.listOfLogs.Len()))
logList.m.Unlock()
// Pop, prepend to list
}, nil
}
func logsListenerPageFunc(logList *threadLogList, natsEnabled *bool) gin.HandlerFunc {
return func(c *gin.Context) {
if logList == nil {
logger.Error("logList is nil")
c.String(http.StatusInternalServerError, "Internal error")
return
}
if logList.listOfLogs == nil {
logger.Error("logList.listOfLogs is nil")
c.String(http.StatusInternalServerError, "Internal error")
return
}
if logList.m == nil {
logger.Error("logList.m is nil")
c.String(http.StatusInternalServerError, "Internal error")
return
}
if natsEnabled == nil {
logger.Error("natsEnabled is nil")
c.String(http.StatusInternalServerError, "Internal error")
return
}
if !*natsEnabled {
c.Redirect(http.StatusFound, "mainmenu")
logger.Debug("no nats connection. Redirect to mainmenu")
return
}
noreload := c.Query("noreload")
if noreload != "true" {
// Let's reload the logs
logList.m.Lock()
logList.visibleLogs = make([]logRecord, 0, logList.listOfLogs.Len())
for e := logList.listOfLogs.Front(); e != nil; e = e.Next() {
val, ok := e.Value.(string)
if !ok {
logger.Error("invalid log loglist value", slog.Any("e.Value", e.Value))
continue
}
transactionID := gjson.Get(val, "transaction_id")
if transactionID.Type != gjson.String {
logger.Error("error fetching transactionID", slog.Any("element", transactionID))
continue
}
router := gjson.Get(val, "request.gl_path")
if router.Type != gjson.String {
logger.Error("error fetching router", slog.Any("element", router))
continue
}
timeStamp := gjson.Get(val, "ingress_egress_timer.start")
if timeStamp.Type != gjson.String {
logger.Error("error fetching timeStamp", slog.Any("element", timeStamp))
continue
}
latency := gjson.Get(val, "ingress_egress_timer.duration")
// latency
if latency.Type != gjson.Number {
logger.Error("error fetching latency", slog.Any("element", latency))
continue
}
statusCode := gjson.Get(val, "response.egress_status_code")
if statusCode.Type != gjson.Number {
logger.Error("error fetching statusCode", slog.Any("element", statusCode))
continue
}
log := logRecord{
TransactionID: transactionID.String(),
Router: router.String(),
Time: func() string {
t := timeStamp.String()
if len(t) < 23 {
return t
}
return t[:23]
}(),
Latency: int(latency.Int()),
StatusCode: int(statusCode.Int()),
body: val,
}
logList.visibleLogs = append(logList.visibleLogs, log)
}
logList.m.Unlock()
}
focusTransactionID := c.Query("transactionID")
focusTransactionBody := ""
if focusTransactionID != "" {
logList.m.Lock()
for _, log := range logList.visibleLogs {
if log.TransactionID == focusTransactionID {
var formattedJSON bytes.Buffer
err := json.Indent(&formattedJSON, []byte(log.body), "", " ")
if err != nil {
logger.Error("unable to format json", slog.Any("error", err))
c.String(http.StatusInternalServerError, "Internal error")
return
}
focusTransactionBody = formattedJSON.String()
break
}
}
logList.m.Unlock()
}
//logger.Debug("render!", slog.Any("Logs", logList.visibleLogs), slog.String("FocusID", focusTransactionID))
c.HTML(http.StatusOK, "logs.html", gin.H{
"Logs": logList.visibleLogs,
"FocusID": focusTransactionID,
"Focus": focusTransactionBody,
})
}
}