Skip to content

Commit

Permalink
Merge pull request prometheus#8485 from hryniuk/promtool-query-errors…
Browse files Browse the repository at this point in the history
…-details

Print details of API errors received by promtool
  • Loading branch information
roidelapluie authored Feb 16, 2021
2 parents 5f92a82 + ab41de6 commit a419b75
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cmd/promtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a419b75

Please sign in to comment.