cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/promapi/failover.go
138lines · 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) CleanCache() { |
| 60 | for _, prom := range fg.servers { |
| 61 | prom.purgeExpiredCache() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func (fg *FailoverGroup) Config(ctx context.Context) (cfg *ConfigResult, err error) { |
| 66 | var uri string |
| 67 | for _, prom := range fg.servers { |
| 68 | uri = prom.uri |
| 69 | cfg, err = prom.Config(ctx) |
| 70 | if err == nil { |
| 71 | return |
| 72 | } |
| 73 | if !IsUnavailableError(err) { |
| 74 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 75 | } |
| 76 | } |
| 77 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 78 | } |
| 79 | |
| 80 | func (fg *FailoverGroup) Query(ctx context.Context, expr string) (qr *QueryResult, err error) { |
| 81 | var uri string |
| 82 | for _, prom := range fg.servers { |
| 83 | uri = prom.uri |
| 84 | qr, err = prom.Query(ctx, expr) |
| 85 | if err == nil { |
| 86 | return |
| 87 | } |
| 88 | if !IsUnavailableError(err) { |
| 89 | return qr, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 90 | } |
| 91 | } |
| 92 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 93 | } |
| 94 | |
| 95 | func (fg *FailoverGroup) RangeQuery(ctx context.Context, expr string, params RangeQueryTimes) (rqr *RangeQueryResult, err error) { |
| 96 | var uri string |
| 97 | for _, prom := range fg.servers { |
| 98 | uri = prom.uri |
| 99 | rqr, err = prom.RangeQuery(ctx, expr, params) |
| 100 | if err == nil { |
| 101 | return |
| 102 | } |
| 103 | if !IsUnavailableError(err) { |
| 104 | return rqr, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 105 | } |
| 106 | } |
| 107 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 108 | } |
| 109 | |
| 110 | func (fg *FailoverGroup) Metadata(ctx context.Context, metric string) (metadata *MetadataResult, err error) { |
| 111 | var uri string |
| 112 | for _, prom := range fg.servers { |
| 113 | uri = prom.uri |
| 114 | metadata, err = prom.Metadata(ctx, metric) |
| 115 | if err == nil { |
| 116 | return |
| 117 | } |
| 118 | if !IsUnavailableError(err) { |
| 119 | return metadata, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 120 | } |
| 121 | } |
| 122 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 123 | } |
| 124 | |
| 125 | func (fg *FailoverGroup) Flags(ctx context.Context) (flags *FlagsResult, err error) { |
| 126 | var uri string |
| 127 | for _, prom := range fg.servers { |
| 128 | uri = prom.uri |
| 129 | flags, err = prom.Flags(ctx) |
| 130 | if err == nil { |
| 131 | return |
| 132 | } |
| 133 | if !IsUnavailableError(err) { |
| 134 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 135 | } |
| 136 | } |
| 137 | return nil, &FailoverGroupError{err: err, uri: uri, isStrict: fg.strictErrors} |
| 138 | } |
| 139 | |