cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/failover.go
132lines · modecode
| 1 | package promapi |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | ) |
| 6 | |
| 7 | type FailoverGroupError struct { |
| 8 | err error |
| 9 | uri string |
| 10 | isStrict bool |
| 11 | } |
| 12 | |
| 13 | func (e *FailoverGroupError) Unwrap() error { |
| 14 | return e.err |
| 15 | } |
| 16 | |
| 17 | func (e *FailoverGroupError) Error() string { |
| 18 | return e.err.Error() |
| 19 | } |
| 20 | |
| 21 | func (e *FailoverGroupError) URI() string { |
| 22 | return e.uri |
| 23 | } |
| 24 | |
| 25 | func (e *FailoverGroupError) IsStrict() bool { |
| 26 | return e.isStrict |
| 27 | } |
| 28 | |
| 29 | type FailoverGroup struct { |
| 30 | name string |
| 31 | servers []*Prometheus |
| 32 | strictErrors bool |
| 33 | } |
| 34 | |
| 35 | func NewFailoverGroup(name string, servers []*Prometheus, strictErrors bool) *FailoverGroup { |
| 36 | return &FailoverGroup{ |
| 37 | name: name, |
| 38 | servers: servers, |
| 39 | strictErrors: strictErrors, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func (fg *FailoverGroup) Name() string { |
| 44 | return fg.name |
| 45 | } |
| 46 | |
| 47 | func (fg *FailoverGroup) StartWorkers() { |
| 48 | for _, prom := range fg.servers { |
| 49 | prom.StartWorkers() |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func (fg *FailoverGroup) Close() { |
| 54 | for _, prom := range fg.servers { |
| 55 | prom.Close() |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func (fg *FailoverGroup) Config(ctx context.Context) (cfg *ConfigResult, err error) { |
| 60 | var uri string |
| 61 | for _, prom := range fg.servers { |
| 62 | uri = prom.uri |
| 63 | cfg, err = prom.Config(ctx) |
| 64 | if err == nil { |
| 65 | return |
| 66 | } |
| 67 | if !IsUnavailableError(err) { |
| 68 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 69 | } |
| 70 | } |
| 71 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 72 | } |
| 73 | |
| 74 | func (fg *FailoverGroup) Query(ctx context.Context, expr string) (qr *QueryResult, err error) { |
| 75 | var uri string |
| 76 | for _, prom := range fg.servers { |
| 77 | uri = prom.uri |
| 78 | qr, err = prom.Query(ctx, expr) |
| 79 | if err == nil { |
| 80 | return |
| 81 | } |
| 82 | if !IsUnavailableError(err) { |
| 83 | return qr, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 84 | } |
| 85 | } |
| 86 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 87 | } |
| 88 | |
| 89 | func (fg *FailoverGroup) RangeQuery(ctx context.Context, expr string, params RangeQueryTimes) (rqr *RangeQueryResult, err error) { |
| 90 | var uri string |
| 91 | for _, prom := range fg.servers { |
| 92 | uri = prom.uri |
| 93 | rqr, err = prom.RangeQuery(ctx, expr, params) |
| 94 | if err == nil { |
| 95 | return |
| 96 | } |
| 97 | if !IsUnavailableError(err) { |
| 98 | return rqr, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 99 | } |
| 100 | } |
| 101 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 102 | } |
| 103 | |
| 104 | func (fg *FailoverGroup) Metadata(ctx context.Context, metric string) (metadata *MetadataResult, err error) { |
| 105 | var uri string |
| 106 | for _, prom := range fg.servers { |
| 107 | uri = prom.uri |
| 108 | metadata, err = prom.Metadata(ctx, metric) |
| 109 | if err == nil { |
| 110 | return |
| 111 | } |
| 112 | if !IsUnavailableError(err) { |
| 113 | return metadata, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 114 | } |
| 115 | } |
| 116 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 117 | } |
| 118 | |
| 119 | func (fg *FailoverGroup) Flags(ctx context.Context) (flags *FlagsResult, err error) { |
| 120 | var uri string |
| 121 | for _, prom := range fg.servers { |
| 122 | uri = prom.uri |
| 123 | flags, err = prom.Flags(ctx) |
| 124 | if err == nil { |
| 125 | return |
| 126 | } |
| 127 | if !IsUnavailableError(err) { |
| 128 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 129 | } |
| 130 | } |
| 131 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 132 | } |
| 133 | |