cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.16.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/promapi/failover.go

97lines · modecode

1package promapi
2
3import (
4 "context"
5 "time"
6)
7
8type FailoverGroupError struct {
9 err error
10 uri string
11 isStrict bool
12}
13
14func (e *FailoverGroupError) Unwrap() error {
15 return e.err
16}
17
18func (e *FailoverGroupError) Error() string {
19 return e.err.Error()
20}
21
22func (e *FailoverGroupError) URI() string {
23 return e.uri
24}
25
26func (e *FailoverGroupError) IsStrict() bool {
27 return e.isStrict
28}
29
30type FailoverGroup struct {
31 name string
32 servers []*Prometheus
33 strictErrors bool
34}
35
36func NewFailoverGroup(name string, servers []*Prometheus, strictErrors bool) *FailoverGroup {
37 return &FailoverGroup{
38 name: name,
39 servers: servers,
40 strictErrors: strictErrors,
41 }
42}
43
44func (fg *FailoverGroup) Name() string {
45 return fg.name
46}
47
48func (fg *FailoverGroup) ClearCache() {
49 for _, prom := range fg.servers {
50 prom.cache.Purge()
51 }
52}
53
54func (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
69func (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
84func (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