forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentry.go
62 lines (53 loc) · 1.65 KB
/
sentry.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
package serviceability
import (
"errors"
"fmt"
"time"
"github.com/getsentry/raven-go"
"github.com/openshift/origin/pkg/version"
)
// SentryMonitor encapsulates a Sentry client and set of default tags
type SentryMonitor struct {
client *raven.Client
tags map[string]string
}
// NewSentryMonitor creates a class that can capture panics and errors from OpenShift
// and Kubernetes that can roll up to a Sentry server.
func NewSentryMonitor(url string) (*SentryMonitor, error) {
client, err := raven.NewClient(url, nil)
if err != nil {
return nil, err
}
client.SetRelease(version.Get().GitCommit)
return &SentryMonitor{
client: client,
}, nil
}
func (m *SentryMonitor) capturePanic(capture interface{}) chan error {
var packet *raven.Packet
switch rval := capture.(type) {
case error:
packet = raven.NewPacket(rval.Error(), raven.NewException(rval, raven.NewStacktrace(2, 3, nil)))
default:
rvalStr := fmt.Sprint(rval)
packet = raven.NewPacket(rvalStr, raven.NewException(errors.New(rvalStr), raven.NewStacktrace(2, 3, nil)))
}
_, ch := m.client.Capture(packet, m.tags)
return ch
}
// CapturePanic is used by the Sentry client to capture panics
func (m *SentryMonitor) CapturePanic(capture interface{}) {
m.capturePanic(capture)
}
// CapturePanicAndWait waits until either the Sentry client captures a panic or
// the provided time expires
func (m *SentryMonitor) CapturePanicAndWait(capture interface{}, until time.Duration) {
select {
case <-m.capturePanic(capture):
case <-time.After(until):
}
}
// CaptureError is used by the Sentry client to capture errors
func (m *SentryMonitor) CaptureError(err error) {
m.client.CaptureError(err, m.tags)
}