cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/failover.go
60lines · modecode
| 1 | package promapi |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | type FailoverGroup struct { |
| 9 | name string |
| 10 | servers []*Prometheus |
| 11 | strictErrors bool |
| 12 | } |
| 13 | |
| 14 | func NewFailoverGroup(name string, servers []*Prometheus, strictErrors bool) *FailoverGroup { |
| 15 | return &FailoverGroup{ |
| 16 | name: name, |
| 17 | servers: servers, |
| 18 | strictErrors: strictErrors, |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | func (fg *FailoverGroup) Name() string { |
| 23 | return fg.name |
| 24 | } |
| 25 | |
| 26 | func (fg *FailoverGroup) ClearCache() { |
| 27 | for _, prom := range fg.servers { |
| 28 | prom.cache.Purge() |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func (fg *FailoverGroup) Config(ctx context.Context) (cfg *PrometheusConfig, err error) { |
| 33 | for _, prom := range fg.servers { |
| 34 | cfg, err = prom.Config(ctx) |
| 35 | if err == nil { |
| 36 | return |
| 37 | } |
| 38 | } |
| 39 | return nil, &Error{err: err, isStrict: fg.strictErrors} |
| 40 | } |
| 41 | |
| 42 | func (fg *FailoverGroup) Query(ctx context.Context, expr string) (qr *QueryResult, err error) { |
| 43 | for _, prom := range fg.servers { |
| 44 | qr, err = prom.Query(ctx, expr) |
| 45 | if err == nil { |
| 46 | return |
| 47 | } |
| 48 | } |
| 49 | return nil, &Error{err: err, isStrict: fg.strictErrors} |
| 50 | } |
| 51 | |
| 52 | func (fg *FailoverGroup) RangeQuery(ctx context.Context, expr string, start, end time.Time, step time.Duration) (rqr *RangeQueryResult, err error) { |
| 53 | for _, prom := range fg.servers { |
| 54 | rqr, err = prom.RangeQuery(ctx, expr, start, end, step) |
| 55 | if err == nil { |
| 56 | return |
| 57 | } |
| 58 | } |
| 59 | return nil, &Error{err: err, isStrict: fg.strictErrors} |
| 60 | } |
| 61 | |