Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Better handle failed prometheus server #8

Merged
merged 12 commits into from
Aug 31, 2023
Merged
8 changes: 7 additions & 1 deletion internal/checks/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type exprProblem struct {
severity Severity
}

func textAndSeverityFromError(err error, reporter, prom string, s Severity) (text string, severity Severity) {
func getPromDescAndPerr(err error, prom string) (string, *promapi.FailoverGroupError) {
promDesc := fmt.Sprintf("%q", prom)
var perr *promapi.FailoverGroupError
perrOk := errors.As(err, &perr)
Expand All @@ -139,6 +139,12 @@ func textAndSeverityFromError(err error, reporter, prom string, s Severity) (tex
promDesc = promText(prom, uri)
}
}
return promDesc, perr
}

func textAndSeverityFromError(err error, reporter, prom string, s Severity) (text string, severity Severity) {
promDesc, perr := getPromDescAndPerr(err, prom)
perrOk := perr != nil

switch {
case promapi.IsQueryTooExpensive(err):
Expand Down
7 changes: 4 additions & 3 deletions internal/checks/promql_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ func (c CounterCheck) checkNode(ctx context.Context, node *parser.PromQLNode, en
}
} else {
metadata, err := c.prom.Metadata(ctx, s.Name)
if err != nil {
text, severity := textAndSeverityFromError(err, c.Reporter(), c.prom.Name(), Bug)
promDesc, _ := getPromDescAndPerr(err, c.prom.Name())
text := fmt.Sprintf("couldn't run %q checks due to missing metrics metadata. %s connection error: %s", c.Reporter(), promDesc, err)
if metadata == nil {
problems = append(problems, exprProblem{
expr: s.Name,
text: text,
severity: severity,
severity: Warning,
})
return problems
}
Expand Down
27 changes: 27 additions & 0 deletions internal/checks/promql_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,34 @@ func TestCounterCheck(t *testing.T) {
},
},
},
{
description: "500 error from Prometheus API",
content: "- record: foo\n expr: rate(foo[5m])\n",
checker: newCounterCheck,
prometheus: newSimpleProm,
problems: func(uri string) []checks.Problem {
return []checks.Problem{
{
Fragment: "foo",
Lines: []int{2},
Reporter: "promql/counter",
Text: checkCounterErrorUnableToRun(checks.CounterCheckName, "prom", uri, "server_error: internal error"),
Severity: checks.Warning,
},
}
},
mocks: []*prometheusMock{
{
conds: []requestCondition{requireMetadataPath},
resp: respondWithInternalError(),
},
},
},
}

runTests(t, testCases)
}

func checkCounterErrorUnableToRun(c, name, uri, err string) string {
return fmt.Sprintf(`couldn't run %q checks due to missing metrics metadata. prometheus %q at %s connection error: %s`, c, name, uri, err)
}
Loading