-
Notifications
You must be signed in to change notification settings - Fork 24
/
hb2.go
77 lines (63 loc) · 1.74 KB
/
hb2.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 hb2 is a Go package for sending errors to Honeybadger
// using the official client library
package hb2
import (
"net/http"
"strings"
"github.com/honeybadger-io/honeybadger-go"
"github.com/remind101/pkg/reporter"
"golang.org/x/net/context"
)
// Headers that won't be sent to honeybadger.
var IgnoredHeaders = map[string]struct{}{
"Authorization": struct{}{},
}
type Config struct {
ApiKey string
Environment string
Endpoint string
}
type hbReporter struct {
client *honeybadger.Client
}
// NewReporter returns a new Reporter instance.
func NewReporter(cfg Config) *hbReporter {
hbCfg := honeybadger.Configuration{}
hbCfg.APIKey = cfg.ApiKey
hbCfg.Env = cfg.Environment
hbCfg.Endpoint = cfg.Endpoint
return &hbReporter{honeybadger.New(hbCfg)}
}
// Report reports the error to honeybadger.
func (r *hbReporter) Report(ctx context.Context, err error) error {
extras := []interface{}{}
if e, ok := err.(*reporter.Error); ok {
extras = append(extras, getContextData(e))
if r := e.Request; r != nil {
extras = append(extras, honeybadger.Params(r.Form), getRequestData(r), *r.URL)
}
err = e.Err
}
_, clientErr := r.client.Notify(err, extras...)
return clientErr
}
func getRequestData(r *http.Request) honeybadger.CGIData {
cgiData := honeybadger.CGIData{}
replacer := strings.NewReplacer("-", "_")
for header, values := range r.Header {
if _, ok := IgnoredHeaders[header]; ok {
continue
}
key := "HTTP_" + replacer.Replace(strings.ToUpper(header))
cgiData[key] = strings.Join(values, ",")
}
cgiData["REQUEST_METHOD"] = r.Method
return cgiData
}
func getContextData(err *reporter.Error) honeybadger.Context {
ctx := honeybadger.Context{}
for key, value := range err.Context {
ctx[key] = value
}
return ctx
}