Skip to content

Commit

Permalink
handle sentry.Request properly in sentrylogrus
Browse files Browse the repository at this point in the history
  • Loading branch information
ribice committed Jan 25, 2025
1 parent f2d4348 commit c67c464
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions logrus/logrusentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ func (h *Hook) entryToEvent(l *logrus.Entry) *sentry.Event {
}

key := h.key(FieldRequest)
switch request := s.Extra[key].(type) {
case *http.Request:
delete(s.Extra, key)
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 req, ok := s.Extra[key].(*http.Request); ok {
delete(s.Extra, key)
s.Request = sentry.NewRequest(req)
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 c67c464

Please sign in to comment.