forked from sirupsen/logrus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp_formatter.go
148 lines (136 loc) · 4.48 KB
/
gcp_formatter.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
package logrus
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
const (
FieldKeyRequest = "request"
FieldKeyResponseStatus = "responseStatus"
FieldKeyResponseSize = "responseSize"
FieldKeyStartAt = "startAt"
FieldKeyTrace = "trace"
FieldKeySpan = "span"
FieldKeyOperation = "operation"
FieldKeyRemoteIP = "remoteIP"
FieldKeyFullPath = "fullPath"
FieldKeyAuthIsAdmin = "authIsAdmin"
FieldKeyAuthProvider = "authProvider"
FieldKeyAuthUserID = "authUserID"
FieldKeyAuthEmail = "authEmail"
FieldKeyAppVersion = "appVersion"
FieldKeyDeviceOS = "deviceOS"
FieldKeyTestName = "testName"
)
type GCPFormatter struct {
GoogleProjectID string
}
func (f *GCPFormatter) Format(entry *Entry) ([]byte, error) {
data := make(Fields)
data["severity"] = levelToGCPSeverity(entry.Level)
data["message"] = entry.Message
data["time"] = entry.Time.Format(time.RFC3339Nano)
// All contextual info stored in the entry
if entry.Context != nil {
if prefix := entry.Context.Value(FieldKeyPrefix); prefix != nil {
data["message"] = prefix.(string) + " " + data["message"].(string)
}
// All HTTP info stored in the entry
httpRequest := make(map[string]interface{})
if reqI := entry.Context.Value(FieldKeyRequest); reqI != nil {
req := reqI.(*http.Request)
httpRequest["requestMethod"] = req.Method
httpRequest["requestUrl"] = req.URL.String()
httpRequest["requestSize"] = req.ContentLength
httpRequest["userAgent"] = req.UserAgent()
httpRequest["referer"] = req.Referer()
httpRequest["protocol"] = req.Proto
}
if ripI := entry.Context.Value(FieldKeyRemoteIP); ripI != nil {
rIP := ripI.(string)
httpRequest["remoteIp"] = rIP
}
if respI := entry.Context.Value(FieldKeyResponseStatus); respI != nil {
resp := respI.(int)
httpRequest["status"] = resp
if respSizeI := entry.Context.Value(FieldKeyResponseSize); respSizeI != nil {
respSize := respSizeI.(int)
httpRequest["responseSize"] = respSize
}
if startAtI := entry.Context.Value(FieldKeyStartAt); startAtI != nil {
dur := time.Since(startAtI.(time.Time))
httpRequest["latency"] = fmt.Sprintf("%.9fs", float64(dur)/float64(time.Second))
}
}
if len(httpRequest) > 0 {
data["httpRequest"] = httpRequest
}
// All Google Tracing info stored in the entry
if traceI := entry.Context.Value(FieldKeyTrace); traceI != nil {
data["logging.googleapis.com/trace"] = fmt.Sprintf("projects/%s/traces/%s", f.GoogleProjectID, traceI.(string))
if spanI := entry.Context.Value(FieldKeySpan); spanI != nil {
data["logging.googleapis.com/spanId"] = spanI.(string)
}
}
// All Operation info stored in the entry
if opI := entry.Context.Value(FieldKeyOperation); opI != nil {
data["logging.googleapis.com/operation"] = opI.(map[string]interface{})
}
// Info about the authentication of the call
jsonPayload := make(Fields)
if authI := entry.Context.Value(FieldKeyAuthProvider); authI != nil {
authProvider := authI.(string)
jsonPayload["auth_provider"] = authProvider
if authEmailI := entry.Context.Value(FieldKeyAuthEmail); authEmailI != nil {
authEmail := authEmailI.(string)
jsonPayload["auth_email"] = authEmail
}
if authIsAdminI := entry.Context.Value(FieldKeyAuthIsAdmin); authIsAdminI != nil {
authIsAdmin := authIsAdminI.(bool)
jsonPayload["auth_is_admin"] = authIsAdmin
}
if authUserIDI := entry.Context.Value(FieldKeyAuthUserID); authUserIDI != nil {
authUserID := authUserIDI.(string)
jsonPayload["auth_user_id"] = authUserID
}
}
if appVersion := entry.Context.Value(FieldKeyAppVersion); appVersion != nil {
jsonPayload["app_version"] = appVersion.(string)
}
if deviceOS := entry.Context.Value(FieldKeyDeviceOS); deviceOS != nil {
jsonPayload["device_os"] = deviceOS.(string)
}
if len(jsonPayload) > 0 {
data["labels"] = jsonPayload
}
}
b, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("failed to marshal fields to JSON, %w", err)
}
return append(b, '\n'), nil
}
// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
func levelToGCPSeverity(level Level) string {
switch level {
case TraceLevel:
return "trace"
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warning"
case ErrorLevel:
return "error"
case CriticalLevel:
return "critical"
case FatalLevel:
return "alert"
case PanicLevel:
return "emergency"
default:
return "error"
}
}