Merge pull request #8485 from hryniuk/promtool-query-errors-details

Print details of API errors received by promtool
This commit is contained in:
Julien Pivotto 2021-02-16 22:47:08 +01:00 committed by GitHub
commit a419b75abd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 9 deletions

View File

@ -497,8 +497,7 @@ func QueryInstant(url *url.URL, query, evalTime string, p printer) int {
val, _, err := api.Query(ctx, query, eTime) // Ignoring warnings for now.
cancel()
if err != nil {
fmt.Fprintln(os.Stderr, "query error:", err)
return 1
return handleAPIError(err)
}
p.printValue(val)
@ -572,8 +571,7 @@ func QueryRange(url *url.URL, headers map[string]string, query, start, end strin
cancel()
if err != nil {
fmt.Fprintln(os.Stderr, "query error:", err)
return 1
return handleAPIError(err)
}
p.printValue(val)
@ -609,8 +607,7 @@ func QuerySeries(url *url.URL, matchers []string, start, end string, p printer)
cancel()
if err != nil {
fmt.Fprintln(os.Stderr, "query error:", err)
return 1
return handleAPIError(err)
}
p.printSeries(val)
@ -648,16 +645,25 @@ func QueryLabels(url *url.URL, name string, start, end string, p printer) int {
for _, v := range warn {
fmt.Fprintln(os.Stderr, "query warning:", v)
}
if err != nil {
fmt.Fprintln(os.Stderr, "query error:", err)
return 1
return handleAPIError(err)
}
p.printLabelValues(val)
return 0
}
func handleAPIError(err error) int {
var apiErr *v1.Error
if errors.As(err, &apiErr) && apiErr.Detail != "" {
fmt.Fprintf(os.Stderr, "query error: %v (detail: %s)\n", apiErr, strings.TrimSpace(apiErr.Detail))
} else {
fmt.Fprintln(os.Stderr, "query error:", err)
}
return 1
}
func parseStartTimeAndEndTime(start, end string) (time.Time, time.Time, error) {
var (
minTime = time.Now().Add(-9999 * time.Hour)