Skip to content

Commit

Permalink
Merge pull request #3 from aereal/consider-problematic-status
Browse files Browse the repository at this point in the history
feat: add ConsiderProblemStatusCode option
  • Loading branch information
aereal authored Sep 21, 2021
2 parents b582676 + 066d65c commit 372ff36
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
15 changes: 14 additions & 1 deletion sentry/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ type Options struct {

// FlushTimeout for the delivery events. Defaults to 2s. Only relevant when WaitForDelivery is true.
FlushTimeout time.Duration

// ConsiderProblematicStatusCode tells the middleware what status code will have the problem details.
//
// Defaults to 500-599 considered problematic status code.
ConsiderProblematicStatusCode func(statusCode int) bool
}

var OnlyServerError = func(statusCode int) bool {
return statusCode >= 500 && statusCode < 600
}

// New returns new middleware that reports problems to Sentry.
Expand All @@ -46,12 +55,16 @@ func New(opts Options) httputil.Middleware {
if timeout == 0 {
timeout = time.Second * 2
}
check := opts.ConsiderProblematicStatusCode
if check == nil {
check = OnlyServerError
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
buf := new(bytes.Buffer)
tw := httputil.NewTeeResponseWriter(rw, buf)
next.ServeHTTP(tw, r)
if !isValidContentType(tw.Header().Get("content-type")) {
if !(isValidContentType(tw.Header().Get("content-type")) && check(tw.StatusCode())) {
return
}
hub := sentrysdk.GetHubFromContext(r.Context())
Expand Down
34 changes: 30 additions & 4 deletions sentry/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ func Test_ok(t *testing.T) {
withInstanceProblem.Instance = "http://instance.example/"

testCases := []struct {
name string
problem *problems.DefaultProblem
wantEvents []*sentrysdk.Event
name string
problem *problems.DefaultProblem
wantEvents []*sentrysdk.Event
considerProblem func(statusCode int) bool
}{
{
"minimal",
Expand All @@ -36,6 +37,7 @@ func Test_ok(t *testing.T) {
},
},
},
nil,
},
{
"with detail",
Expand All @@ -50,6 +52,7 @@ func Test_ok(t *testing.T) {
},
},
},
nil,
},
{
"with instance",
Expand All @@ -64,6 +67,29 @@ func Test_ok(t *testing.T) {
},
},
},
nil,
},
{
"client error",
problems.NewStatusProblem(http.StatusBadRequest),
nil,
nil,
},
{
"capture client error",
problems.NewStatusProblem(http.StatusBadRequest),
[]*sentrysdk.Event{
{
Level: sentrysdk.LevelInfo,
Message: http.StatusText(http.StatusBadRequest),
Extra: map[string]interface{}{
"problem.status": http.StatusBadRequest,
},
},
},
func(statusCode int) bool {
return statusCode >= 400 && statusCode < 500
},
},
}
for _, tc := range testCases {
Expand All @@ -74,7 +100,7 @@ func Test_ok(t *testing.T) {
}
problems.StatusProblemHandler(tc.problem).ServeHTTP(rw, r)
})
srv := httptest.NewServer(withSentryHub()(New(Options{WaitForDelivery: true})(h)))
srv := httptest.NewServer(withSentryHub()(New(Options{WaitForDelivery: true, ConsiderProblematicStatusCode: tc.considerProblem})(h)))
defer srv.Close()

var mux sync.Mutex
Expand Down

0 comments on commit 372ff36

Please sign in to comment.