Skip to content

Commit

Permalink
handle sentry.Request properly in sentrylogrus (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribice authored Jan 26, 2025
1 parent 9e8583d commit 2e92c62
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
11 changes: 9 additions & 2 deletions logrus/logrusentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,16 @@ func (h *Hook) entryToEvent(l *logrus.Entry) *sentry.Event {
}

key := h.key(FieldRequest)
if req, ok := s.Extra[key].(*http.Request); ok {
switch request := s.Extra[key].(type) {
case *http.Request:
delete(s.Extra, key)
s.Request = sentry.NewRequest(req)
s.Request = sentry.NewRequest(request)
case sentry.Request:
delete(s.Extra, key)
s.Request = &request
case *sentry.Request:
delete(s.Extra, key)
s.Request = request
}

if err, ok := s.Extra[logrus.ErrorKey].(error); ok {
Expand Down
38 changes: 38 additions & 0 deletions logrus/logrusentry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,44 @@ func Test_entryToEvent(t *testing.T) {
Logger: "logrus",
},
},
"sentry request": {
entry: &logrus.Entry{
Data: map[string]any{
FieldRequest: sentry.Request{
URL: "http://example.com/",
Method: http.MethodGet,
},
},
},
want: &sentry.Event{
Level: "fatal",
Extra: map[string]any{},
Request: &sentry.Request{
URL: "http://example.com/",
Method: http.MethodGet,
},
Logger: "logrus",
},
},
"sentry pointer to request": {
entry: &logrus.Entry{
Data: map[string]any{
FieldRequest: &sentry.Request{
URL: "http://example.com/",
Method: http.MethodGet,
},
},
},
want: &sentry.Event{
Level: "fatal",
Extra: map[string]any{},
Request: &sentry.Request{
URL: "http://example.com/",
Method: http.MethodGet,
},
Logger: "logrus",
},
},
"error": {
entry: &logrus.Entry{
Data: map[string]any{
Expand Down

0 comments on commit 2e92c62

Please sign in to comment.