-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry.go
161 lines (142 loc) · 3.73 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"encoding/hex"
"fmt"
"hash"
"io"
stdlog "log"
"net/http"
"os"
"strings"
"time"
"github.com/getsentry/sentry-go"
"github.com/rs/zerolog/log"
)
type (
Reporter struct {
sentryDSN string
start time.Time
hostname string
cmd string
hash hash.Hash
combined *bytes.Buffer
stderr *bytes.Buffer
}
)
var SentryTimeout = 30 * time.Second
// newReporter creates a new Sentry client
func newReporter(cr *CmdRequest) (*Reporter, error) {
sentryDSN, ok := os.LookupEnv("CRONGUARD_SENTRY_DSN")
if !ok && cr.Config != nil {
sentryDSN = cr.Config.SentryDSN
}
if sentryDSN == "" {
return nil, fmt.Errorf("no config provided")
}
// data
hostname, _ := os.Hostname()
if hostname == "" {
hostname = "no-hostname"
}
hostname = strings.SplitN(hostname, ".", 2)[0]
hash := sha256.New()
hash.Write([]byte(cr.Command))
hash.Write([]byte(hostname))
cmd := cr.Command
if len(cmd) > 32 {
cmd = fmt.Sprintf("%s%s", cmd[0:30], "...")
}
// setup sentry
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
sentryErr := sentry.Init(sentry.ClientOptions{
Debug: cr.Debug,
HTTPTransport: transport,
BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
log.Debug().Interface("event", event).Msg("sending event")
return event
},
Dsn: sentryDSN,
})
if sentryErr != nil {
fmt.Fprintf(cr.Status.Stderr, "cronguard: unable to connect to sentry: %s\n", sentryErr)
fmt.Fprintf(cr.Status.Stderr, "cronguard: running cron anyways\n")
return nil, fmt.Errorf("unable to connect to sentry")
}
// wrap buffers
start := time.Now()
combined := bytes.NewBuffer([]byte{})
stderr := bytes.NewBuffer([]byte{})
cr.Status.Stderr = io.MultiWriter(stderr, combined, cr.Status.Stderr)
cr.Status.Stdout = io.MultiWriter(combined, cr.Status.Stdout)
// set known sentry extras
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetExtra("time_start", start)
scope.SetExtra("command", cr.Command)
})
if cr.Debug {
sentry.Logger = stdlog.Default()
}
return &Reporter{
sentryDSN: sentryDSN,
start: start,
hostname: hostname,
cmd: cmd,
hash: hash,
combined: combined,
stderr: stderr,
}, nil
}
// Finish reports the final status to sentry if err != nil
func (r *Reporter) Finish(err error) error {
if err == nil {
return nil
}
return r.report(err, finishLevel)
}
// Info reports a Info status to sentry
func (r *Reporter) Info(err error) error {
return r.report(err, infoLevel)
}
// reportLevel is used by reporter to disingques
type reportLevel = string
const (
// infoLevel is an information that will be send to sentry
infoLevel reportLevel = "info"
// finishLevel is used to tell the reporter that the cron has finished
finishLevel = "finish"
)
// report reports any error message to sentry
func (r *Reporter) report(err error, level reportLevel) error {
// prepare sentry information
name := ""
extra := map[string]interface{}{}
if level == finishLevel {
name = fmt.Sprintf("%s: %s (%s)", r.hostname, r.cmd, err.Error())
extra["time_end"] = time.Now()
extra["time_duration"] = time.Since(r.start).String()
extra["out_combined"] = r.combined.String()
extra["out_stderr"] = r.stderr.String()
} else {
name = fmt.Sprintf("%s (%s): %s (%s)", r.hostname, level, r.cmd, err.Error())
}
// sentry
hash := hex.EncodeToString(r.hash.Sum([]byte(level)))
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetFingerprint([]string{hash})
scope.SetExtras(extra)
})
_ = sentry.CaptureMessage(name)
// hide error if messages are successfully flushed to sentry
flushed := sentry.Flush(SentryTimeout)
if !flushed {
return err
}
return nil
}