cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/failover.go
97lines · modecode
| 1 | package promapi |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | type FailoverGroupError struct { |
| 9 | err error |
| 10 | uri string |
| 11 | isStrict bool |
| 12 | } |
| 13 | |
| 14 | func (e *FailoverGroupError) Unwrap() error { |
| 15 | return e.err |
| 16 | } |
| 17 | |
| 18 | func (e *FailoverGroupError) Error() string { |
| 19 | return e.err.Error() |
| 20 | } |
| 21 | |
| 22 | func (e *FailoverGroupError) URI() string { |
| 23 | return e.uri |
| 24 | } |
| 25 | |
| 26 | func (e *FailoverGroupError) IsStrict() bool { |
| 27 | return e.isStrict |
| 28 | } |
| 29 | |
| 30 | type FailoverGroup struct { |
| 31 | name string |
| 32 | servers []*Prometheus |
| 33 | strictErrors bool |
| 34 | } |
| 35 | |
| 36 | func NewFailoverGroup(name string, servers []*Prometheus, strictErrors bool) *FailoverGroup { |
| 37 | return &FailoverGroup{ |
| 38 | name: name, |
| 39 | servers: servers, |
| 40 | strictErrors: strictErrors, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func (fg *FailoverGroup) Name() string { |
| 45 | return fg.name |
| 46 | } |
| 47 | |
| 48 | func (fg *FailoverGroup) ClearCache() { |
| 49 | for _, prom := range fg.servers { |
| 50 | prom.cache.Purge() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func (fg *FailoverGroup) Config(ctx context.Context) (cfg *ConfigResult, err error) { |
| 55 | var uri string |
| 56 | for _, prom := range fg.servers { |
| 57 | uri = prom.uri |
| 58 | cfg, err = prom.Config(ctx) |
| 59 | if err == nil { |
| 60 | return |
| 61 | } |
| 62 | if !IsUnavailableError(err) { |
| 63 | return cfg, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 64 | } |
| 65 | } |
| 66 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 67 | } |
| 68 | |
| 69 | func (fg *FailoverGroup) Query(ctx context.Context, expr string) (qr *QueryResult, err error) { |
| 70 | var uri string |
| 71 | for _, prom := range fg.servers { |
| 72 | uri = prom.uri |
| 73 | qr, err = prom.Query(ctx, expr) |
| 74 | if err == nil { |
| 75 | return |
| 76 | } |
| 77 | if !IsUnavailableError(err) { |
| 78 | return qr, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 79 | } |
| 80 | } |
| 81 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 82 | } |
| 83 | |
| 84 | func (fg *FailoverGroup) RangeQuery(ctx context.Context, expr string, lookback, step time.Duration) (rqr *RangeQueryResult, err error) { |
| 85 | var uri string |
| 86 | for _, prom := range fg.servers { |
| 87 | uri = prom.uri |
| 88 | rqr, err = prom.RangeQuery(ctx, expr, lookback, step) |
| 89 | if err == nil { |
| 90 | return |
| 91 | } |
| 92 | if !IsUnavailableError(err) { |
| 93 | return rqr, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 94 | } |
| 95 | } |
| 96 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 97 | } |
| 98 | |