-
Notifications
You must be signed in to change notification settings - Fork 7
/
sentry_client.go
60 lines (49 loc) · 1.23 KB
/
sentry_client.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
package main
import (
"fmt"
"github.com/cockroachdb/errors"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"net/http"
"time"
)
// SentryClient wraps sentry
type SentryClient struct {
dsn string
handler *sentryhttp.Handler
}
// Close must be called after NewSentryClient
type Close func()
// NewSentryClient returns new SentryClient instance
func NewSentryClient(dsn string, debug bool) (*SentryClient, Close, error) {
if dsn != "" {
err := sentry.Init(sentry.ClientOptions{
Dsn: dsn,
Release: fmt.Sprintf("gitpanda@%s", Version),
Debug: debug,
})
if err != nil {
return nil, nil, errors.WithStack(err)
}
}
sentryHandler := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
return &SentryClient{dsn: dsn, handler: sentryHandler}, releaseSentry, nil
}
func releaseSentry() {
sentry.Flush(2 * time.Second)
}
// HandleFunc wraps handler if necessary
func (s *SentryClient) HandleFunc(handler http.HandlerFunc) http.HandlerFunc {
if s.dsn == "" {
return handler
}
return s.handler.HandleFunc(handler)
}
// CaptureException send error to sentry if necessary
func (s *SentryClient) CaptureException(err error) {
if s.dsn != "" {
sentry.CaptureException(err)
}
}