cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/errors.go
67lines · modecode
| 1 | package promapi |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "net" |
| 7 | "strings" |
| 8 | "syscall" |
| 9 | "time" |
| 10 | |
| 11 | v1 "github.com/prometheus/client_golang/api/prometheus/v1" |
| 12 | ) |
| 13 | |
| 14 | func IsUnavailableError(err error) bool { |
| 15 | var apiErr *v1.Error |
| 16 | if ok := errors.As(err, &apiErr); ok { |
| 17 | return apiErr.Type == v1.ErrServer |
| 18 | } |
| 19 | |
| 20 | return true |
| 21 | } |
| 22 | |
| 23 | type APIError struct { |
| 24 | Status string `json:"status"` |
| 25 | ErrorType v1.ErrorType `json:"errorType"` |
| 26 | Error string `json:"error"` |
| 27 | } |
| 28 | |
| 29 | func CanRetryError(err error, delta time.Duration) (time.Duration, bool) { |
| 30 | if errors.Is(err, syscall.ECONNREFUSED) { |
| 31 | return delta, false |
| 32 | } |
| 33 | |
| 34 | var neterr net.Error |
| 35 | if ok := errors.As(err, &neterr); ok && neterr.Timeout() { |
| 36 | return (delta / 2).Round(time.Minute), true |
| 37 | } |
| 38 | |
| 39 | var apiErr *v1.Error |
| 40 | if ok := errors.As(err, &apiErr); ok { |
| 41 | // {"status":"error","errorType":"timeout","error":"query timed out in expression evaluation"} |
| 42 | // Comes with 503 and ends up being reported as server_error but Detail contains raw JSON |
| 43 | // which we can parse and fix the error type |
| 44 | var v1e APIError |
| 45 | if err2 := json.Unmarshal([]byte(apiErr.Detail), &v1e); err2 == nil && v1e.ErrorType == v1.ErrTimeout { |
| 46 | apiErr.Type = v1.ErrTimeout |
| 47 | } |
| 48 | |
| 49 | switch apiErr.Type { |
| 50 | case v1.ErrBadData: |
| 51 | case v1.ErrTimeout: |
| 52 | return (delta / 4).Round(time.Minute), true |
| 53 | case v1.ErrCanceled: |
| 54 | case v1.ErrExec: |
| 55 | if strings.Contains(apiErr.Msg, "query processing would load too many samples into memory in ") { |
| 56 | return (delta / 4).Round(time.Minute), true |
| 57 | } |
| 58 | return delta / 2, true |
| 59 | case v1.ErrBadResponse: |
| 60 | case v1.ErrServer: |
| 61 | case v1.ErrClient: |
| 62 | return (delta / 2).Round(time.Minute), true |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return delta, false |
| 67 | } |
| 68 | |