/
client_log_collector.go
77 lines (66 loc) · 2.25 KB
/
client_log_collector.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 handlers
import (
"encoding/json"
"io"
"net/http"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/auth"
)
type ClientLoggerStats struct {
DroppedLogsCount int `json:"droppedLogsCount"`
FailedSendCount int `json:"failedSendCount"`
FailedTimerCount int `json:"failedTimerCount"`
}
type ClientLogMessage []interface{}
type ClientLogEntry struct {
Level string `json:"level"`
Args ClientLogMessage `json:"args"`
}
type ClientLogUpload struct {
App string `json:"app"`
LoggerStats ClientLoggerStats `json:"loggerStats"`
LogEntries []ClientLogEntry `json:"logEntries"`
}
// NewClientLogHandler creates a handler for receiving client logs
func NewClientLogHandler(appCtx appcontext.AppContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data, err := io.ReadAll(r.Body)
if err != nil {
appCtx.Logger().Error("client logs handler error", zap.Error(err))
return
}
var logUpload ClientLogUpload
err = json.Unmarshal(data, &logUpload)
if err != nil {
appCtx.Logger().Error("Error unmarshalling ClientLogUpload", zap.Error(err))
// really no need to tell the client the server had
// problems in this case, it can't do anything about it
return
}
// use the appCtx logger and not the one associated with the
// request so that client logs have only the attributes
// configured here
clientLogger := appCtx.Logger().With(
zap.String("app", logUpload.App),
)
if sessionID := auth.SessionIDFromContext(r.Context()); sessionID != "" {
clientLogger = clientLogger.With(zap.String("session_id", sessionID))
}
clientLogger.Info("client log upload stats",
zap.String("source", "client_stats"),
zap.Int("logEntryCount", len(logUpload.LogEntries)),
zap.Int("droppedLogCount", logUpload.LoggerStats.DroppedLogsCount),
zap.Int("failedSendCount", logUpload.LoggerStats.FailedSendCount),
zap.Int("failedTimerCount", logUpload.LoggerStats.FailedTimerCount),
)
for i := range logUpload.LogEntries {
logEntry := logUpload.LogEntries[i]
clientLogger.Info("client log entry",
zap.String("source", "client_log_entry"),
zap.String("logLevel", logEntry.Level),
zap.Any("args", logEntry.Args),
)
}
}
}