-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
98 lines (84 loc) · 2.08 KB
/
service.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
package sentry
import (
"bytes"
"fmt"
"github.com/getsentry/raven-go"
"github.com/spiral/roadrunner"
rr "github.com/spiral/roadrunner/cmd/rr/cmd"
rrhttp "github.com/spiral/roadrunner/service/http"
"net/http"
)
const ID = "sentry"
type Service struct {
}
func (s *Service) Init(cfg *Config) (bool, error) {
err := raven.SetDSN(cfg.DSN)
if err != nil {
return false, err
}
svc, _ := rr.Container.Get(rrhttp.ID)
if svc, ok := svc.(*rrhttp.Service); ok {
svc.AddListener(systemListener)
svc.AddListener(httpListener)
}
return true, nil
}
// listener listens to http events and generates nice looking output.
func httpListener(event int, ctx interface{}) {
// http events
switch event {
case rrhttp.EventError:
e := ctx.(*rrhttp.ErrorEvent)
buf := new(bytes.Buffer)
buf.ReadFrom(e.Request.Body)
body := buf.String()
meta := map[string]string{
"event": "EventError",
"code": "500",
"method": e.Request.Method,
"site": e.Request.Host,
"uri": uri(e.Request),
"requestBody": body,
}
raven.CaptureErrorAndWait(e.Error, meta)
return
}
}
func systemListener(event int, ctx interface{}) {
switch event {
case roadrunner.EventWorkerError:
e := ctx.(roadrunner.WorkerError)
meta := map[string]string{
"event": "EventWorkerError",
}
raven.CaptureErrorAndWait(e.Caused, meta)
return
case roadrunner.EventWorkerDead:
e := ctx.(roadrunner.WorkerError)
meta := map[string]string{
"event": "EventWorkerDead",
}
raven.CaptureErrorAndWait(e.Caused, meta)
return
case roadrunner.EventPoolError:
e := ctx.(roadrunner.WorkerError)
meta := map[string]string{
"event": "EventPoolError",
}
raven.CaptureErrorAndWait(e.Caused, meta)
return
case roadrunner.EventWorkerKill:
e := ctx.(roadrunner.WorkerError)
meta := map[string]string{
"event": "EventWorkerKill",
}
raven.CaptureErrorAndWait(e.Caused, meta)
return
}
}
func uri(r *http.Request) string {
if r.TLS != nil {
return fmt.Sprintf("https://%s%s", r.Host, r.URL.String())
}
return fmt.Sprintf("http://%s%s", r.Host, r.URL.String())
}