cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.32.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/failover.go

152lines · modecode

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