Skip to content

Commit 17d1ac3

Browse files
alin-at-dfinityfree
authored andcommitted
Better Jira error handling (prometheus-community#140)
* Better Jira error handling * Return HTTP 400 Bad Request for non-retriable errors. It is inaccurate, but will prevent alertmanager from retrying. * Turns out go-jira does actually produce useful error messages (and it consumes the response body in the process). Log that instead of the empty body. Signed-off-by: Alin Sinpalean <[email protected]> * Also include HTTP response status 429 among retriable errors. Signed-off-by: Alin Sinpalean <[email protected]> * Include both the go-jira error and the response body in errors. Sometimes go-jira consumes the body and includes it in its error, sometimes it doesn't. Signed-off-by: Alin Sinpalean <[email protected]> --------- Signed-off-by: Alin Sinpalean <[email protected]> Co-authored-by: Alin Sinpalean <[email protected]>
1 parent 2201daf commit 17d1ac3

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

cmd/jiralert/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717
"encoding/json"
1818
"flag"
1919
"fmt"
20-
"github.com/andygrunwald/go-jira"
2120
"net/http"
2221
"os"
2322
"runtime"
2423
"strconv"
2524

25+
"github.com/andygrunwald/go-jira"
2626
"github.com/go-kit/log"
2727
"github.com/go-kit/log/level"
2828
"github.com/prometheus-community/jiralert/pkg/alertmanager"
@@ -127,7 +127,8 @@ func main() {
127127
// Instruct Alertmanager to retry.
128128
status = http.StatusServiceUnavailable
129129
} else {
130-
status = http.StatusInternalServerError
130+
// Inaccurate, just letting Alertmanager know that it should not retry.
131+
status = http.StatusBadRequest
131132
}
132133
errorHandler(w, status, err, conf.Name, &data, logger)
133134
return

pkg/notify/notify.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"time"
2424

2525
"github.com/andygrunwald/go-jira"
26-
2726
"github.com/go-kit/log"
2827
"github.com/go-kit/log/level"
2928
"github.com/pkg/errors"
@@ -383,10 +382,11 @@ func handleJiraErrResponse(api string, resp *jira.Response, err error, logger lo
383382
}
384383

385384
if resp != nil && resp.StatusCode/100 != 2 {
386-
retry := resp.StatusCode == 500 || resp.StatusCode == 503
385+
retry := resp.StatusCode == 500 || resp.StatusCode == 503 || resp.StatusCode == 429
386+
// Sometimes go-jira consumes the body (e.g. in `Search`) and includes it in the error message;
387+
// sometimes (e.g. in `Create`) it doesn't. Include both the error and the body, just in case.
387388
body, _ := io.ReadAll(resp.Body)
388-
// go-jira error message is not particularly helpful, replace it
389-
return retry, errors.Errorf("JIRA request %s returned status %s, body %q", resp.Request.URL, resp.Status, string(body))
389+
return retry, errors.Errorf("JIRA request %s returned status %s, error %q, body %q", resp.Request.URL, resp.Status, err, body)
390390
}
391391
return false, errors.Wrapf(err, "JIRA request %s failed", api)
392392
}

0 commit comments

Comments
 (0)