-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapm.go
64 lines (54 loc) · 1.55 KB
/
apm.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
package apm
import (
"context"
"fmt"
"strings"
"time"
"github.com/getsentry/sentry-go"
)
// Error captures the error and sends it to sentry and healthchecks
func Error(ctx context.Context, err error) {
if err == nil {
return
}
GetHub(ctx).CaptureException(err)
HealthcheckFail(strings.NewReader("error: " + err.Error()))
}
// StartSpan starts a new span, and if there is no transaction, it starts a new transaction
func StartSpan(ctx context.Context, operation string) *sentry.Span {
if transaction := sentry.TransactionFromContext(ctx); transaction == nil {
ctx = sentry.StartTransaction(ctx, operation, sentry.WithDescription(operation)).Context()
}
return sentry.StartSpan(ctx, operation, sentry.WithDescription(operation))
}
// GetHub returns the hub from the context (if context is provided and has a hub) or the current hub
func GetHub(ctx ...context.Context) *sentry.Hub {
if len(ctx) == 0 {
return sentry.CurrentHub().Clone()
}
if hub := sentry.GetHubFromContext(ctx[0]); hub != nil {
return hub
}
return sentry.CurrentHub().Clone()
}
// Flush sends the events to sentry
func Flush(ctx ...context.Context) {
GetHub(ctx...).Flush(5 * time.Second)
}
// Recover sends the error to sentry
func Recover(err any, repanic bool, ctx ...context.Context) {
if err == nil {
Flush(ctx...)
return
}
HealthcheckFail(strings.NewReader(fmt.Sprintf("panic recovered: %+v", err)))
if len(ctx) > 0 && ctx[0] != nil {
GetHub(ctx...).RecoverWithContext(ctx[0], err)
} else {
GetHub(ctx...).Recover(err)
}
Flush(ctx...)
if repanic {
panic(err)
}
}