-
Notifications
You must be signed in to change notification settings - Fork 3
/
breadcrumb_transport.go
58 lines (46 loc) · 1.35 KB
/
breadcrumb_transport.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
package logger
import (
"net/http"
"time"
"github.com/getsentry/sentry-go"
)
type breadcrumbTransport struct {
Transport http.RoundTripper
Level sentry.Level
}
func NewBreadcrumbTransport(level sentry.Level, transport http.RoundTripper) http.RoundTripper {
if transport == nil {
transport = http.DefaultTransport
}
return &breadcrumbTransport{
Transport: transport,
Level: level,
}
}
//nolint:contextcheck
func (b breadcrumbTransport) RoundTrip(req *http.Request) (*http.Response, error) {
span := sentry.StartSpan(req.Context(), req.URL.String(), sentry.ContinueFromRequest(req))
defer span.Finish()
//nolint:canonicalheader
req.Header.Add("sentry-trace", span.ToSentryTrace())
breadcrumb := sentry.Breadcrumb{
Data: map[string]interface{}{
BreadcrumbDataURL: req.URL.String(),
BreadcrumbDataMethod: req.Method,
},
Level: b.Level,
Timestamp: time.Now().UTC(),
Type: BreadcrumbTypeHTTP,
}
resp, err := b.Transport.RoundTrip(req.WithContext(span.Context()))
if err == nil {
span.Status = SpanStatus(resp.StatusCode)
breadcrumb.Data[BreadcrumbDataStatusCode] = resp.StatusCode
breadcrumb.Data[BreadcrumbDataReason] = resp.Status
} else {
span.Status = sentry.SpanStatusAborted
breadcrumb.Message = err.Error()
}
Hub(span.Context()).AddBreadcrumb(&breadcrumb, nil)
return resp, err //nolint:wrapcheck
}