forked from evergreen-ci/logkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
192 lines (157 loc) · 4.82 KB
/
log.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
package logkeeper
import (
"net/http"
"os"
"strings"
"time"
"github.com/mongodb/amboy"
"github.com/mongodb/amboy/logger"
"github.com/mongodb/grip"
"github.com/mongodb/grip/level"
"github.com/mongodb/grip/message"
"github.com/mongodb/grip/send"
"github.com/pkg/errors"
"github.com/urfave/negroni"
)
const remoteAddrHeaderName = "X-Cluster-Client-Ip"
// is a middleware handler that logs the request as it goes in and the response as it goes out.
type Logger struct {
// ids is a channel producing unique, autoincrementing request ids that are included in logs.
ids chan int
}
// NewLogger returns a new Logger instance
func NewLogger() *Logger {
ids := make(chan int, 100)
go func() {
reqId := 0
for {
ids <- reqId
reqId++
}
}()
return &Logger{ids}
}
func getLevel(l int) level.Priority {
if l == http.StatusOK {
return level.Info
}
return level.Warning
}
func (l *Logger) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
reqID := <-l.ids
r = SetCtxRequestId(reqID, r)
remote := r.Header.Get(remoteAddrHeaderName)
if remote == "" {
remote = r.RemoteAddr
}
defer func() {
if err := recover(); err != nil {
if rw.Header().Get("Content-Type") == "" {
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
}
rw.WriteHeader(http.StatusInternalServerError)
grip.Critical(message.Fields{
"stack": message.NewStack(2, "").Raw(),
"panic": err,
"action": "aborted",
"request": reqID,
"duration": time.Since(start),
"span": time.Since(start).String(),
"remote": remote,
"path": r.URL.Path,
})
}
}()
next(rw, r)
res := rw.(negroni.ResponseWriter)
code := res.Status()
grip.Log(getLevel(code), message.Fields{
"method": r.Method,
"request": reqID,
"path": r.URL.Path,
"duration": time.Since(start),
"action": "completed",
"status": code,
"remote": remote,
"outcome": http.StatusText(code),
"span": time.Since(start).String(),
})
}
func GetSender(queue amboy.Queue, fn string) (send.Sender, error) {
const (
name = "logkeeper"
interval = 20 * time.Second
bufferCount = 100
)
var (
err error
sender send.Sender
senders []send.Sender
baseLevel = send.LevelInfo{Default: level.Info, Threshold: level.Debug}
)
// configure remote logging services first if the environment
// variables are specified.
if splunk := send.GetSplunkConnectionInfo(); splunk.Populated() {
sender, err = send.NewSplunkLogger(name, splunk, baseLevel)
if err != nil {
return nil, errors.Wrap(err, "problem creating the splunk logger")
}
senders = append(senders, send.NewBufferedSender(sender, interval, bufferCount))
}
if endpoint := os.Getenv("GRIP_SUMO_ENDPOINT"); endpoint != "" {
sender, err = send.NewSumo(name, endpoint)
if err != nil {
return nil, errors.Wrap(err, "problem creating the sumo logic sender")
}
if err = sender.SetLevel(baseLevel); err != nil {
return nil, errors.Wrap(err, "problem setting level for alert remote object")
}
senders = append(senders, send.NewBufferedSender(sender, interval, bufferCount))
}
// configure slack logger for alerts and panics
if token := os.Getenv("GRIP_SLACK_CLIENT_TOKEN"); token != "" {
channel := os.Getenv("GRIP_SLACK_CHANNEL")
if channel == "" {
channel = "#evergreen-ops-alerts"
}
if !strings.HasPrefix(channel, "#") {
channel = "#" + channel
}
opts := &send.SlackOptions{
Name: name,
Channel: channel,
AllFields: true,
BasicMetadata: true,
}
sender, err = send.NewSlackLogger(opts, token, send.LevelInfo{Default: level.Error, Threshold: level.Critical})
if err != nil {
return nil, errors.Wrap(err, "problem creating the slack sender")
}
// TODO use the amboy.Queue backed sender in this case.
senders = append(senders, logger.MakeQueueSender(queue, sender))
}
// setup file logger, defaulting first to the system logger,
// to standard output (or not at all) if specified, and
// finally to the file as specified.
if fn == "" {
sender = getSystemLogger()
if err = sender.SetLevel(send.LevelInfo{Default: level.Info, Threshold: level.Debug}); err != nil {
return nil, errors.Wrap(err, "problem setting level for local system sender")
}
senders = append(senders, sender)
} else if fn == "LOCAL" || fn == "--" || fn == "stdout" {
sender, err = send.NewNativeLogger(name, baseLevel)
if err != nil {
return nil, errors.Wrap(err, "problem creating a native console logger")
}
senders = append(senders, sender)
} else if (fn != "NONE") && (fn != "SKIP") {
sender, err = send.NewFileLogger("logkeeper", fn, baseLevel)
if err != nil {
return nil, errors.Wrap(err, "problem creating a file logger")
}
senders = append(senders, sender)
}
return send.NewConfiguredMultiSender(senders...), nil
}