-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook.go
117 lines (97 loc) · 2.63 KB
/
hook.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
package logrus_rollbar
import (
"bytes"
"errors"
"fmt"
"net/http"
"gopkg.in/errgo.v1"
"github.com/rollbar/rollbar-go"
"github.com/sirupsen/logrus"
errgorollbar "github.com/Scalingo/errgo-rollbar"
)
var (
SeverityCritical = "critical"
ErrgoStackSkip = 15
)
type hook struct {
Sender
}
func New() logrus.Hook {
return hook{Sender: RollbarSender{}}
}
func (h hook) Fire(entry *logrus.Entry) error {
var req *http.Request
if r, ok := entry.Data["req"]; ok {
upstreamReq, ok := r.(*http.Request)
if ok {
req, _ = http.NewRequest(upstreamReq.Method, upstreamReq.URL.String(), nil)
req.RemoteAddr = upstreamReq.RemoteAddr
for key, val := range upstreamReq.Header {
// We don't want to log credentials
if key == "Authorization" {
continue
}
req.Header[key] = val
}
// Replacing the request struct by something simpler in the entry fields
entry.Data["req"] = fmt.Sprintf(
"%s %s %s",
req.Method, req.URL, req.RemoteAddr,
)
}
}
// All the fields which aren't level|msg|error|time|req are added
// to the headers of the request which will be sent to Rollbar
// The main goal is to be able to see all the values on Rollbar dashboard
fields := map[string]interface{}{}
for val, key := range entry.Data {
if val != "level" && val != "msg" && val != "error" && val != "time" && val != "req" {
fields[val] = key
}
}
// If there is an error field, we want it to be part of Rollbar ticket name
var (
err error
)
msg := entry.Message
if entry.Data["msg"] != nil {
msg = fmt.Sprintf("%s - %v", msg, entry.Data["msg"])
}
if entry.Data["error"] != nil {
err = entry.Data["error"].(error)
errorTxt := new(bytes.Buffer)
errorTxt.WriteString(err.Error())
errorTxt.WriteString(" - " + msg)
msg := errorTxt.String()
switch err.(type) {
case *errgo.Err:
// SkipLevel is set to 15, why: the stack is generated by the rollbar lib
// just before sending the error. So when the Stack() method of the
// wrapped Error is called, it is deep in the code of 1. logrus, 2. this
// hook, 3. rollbar, so there are 15 levels of stack trace we want to
// hide in order to have a clean error.
err = errgorollbar.Wrap(msg, err, ErrgoStackSkip)
default:
err = Wrap(msg, err)
}
} else {
err = errors.New(msg)
}
severity := rollbar.ERR
if entry.Data["severity"] == SeverityCritical {
severity = rollbar.CRIT
}
if req == nil {
h.Sender.Error(severity, err, fields)
} else {
h.Sender.RequestError(severity, req, err, fields)
}
return nil
}
func (h hook) Levels() []logrus.Level {
return []logrus.Level{
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
}
}