cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.43.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/config.go

155lines · modecode

1package promapi
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "io"
8 "net/http"
9 "net/url"
10 "time"
11
12 v1 "github.com/prometheus/client_golang/api/prometheus/v1"
13 "github.com/prymitive/current"
14 "github.com/rs/zerolog/log"
15 "gopkg.in/yaml.v3"
16)
17
18type ConfigSectionGlobal struct {
19 ScrapeInterval time.Duration `yaml:"scrape_interval"`
20 ScrapeTimeout time.Duration `yaml:"scrape_timeout"`
21 EvaluationInterval time.Duration `yaml:"evaluation_interval"`
22 ExternalLabels map[string]string `yaml:"external_labels"`
23}
24
25type PrometheusConfig struct {
26 Global ConfigSectionGlobal `yaml:"global"`
27}
28
29type ConfigResult struct {
30 URI string
31 Config PrometheusConfig
32}
33
34type configQuery struct {
35 prom *Prometheus
36 ctx context.Context
37 timestamp time.Time
38}
39
40func (q configQuery) Run() queryResult {
41 log.Debug().
42 Str("uri", q.prom.safeURI).
43 Msg("Getting prometheus configuration")
44
45 ctx, cancel := q.prom.requestContext(q.ctx)
46 defer cancel()
47
48 var qr queryResult
49
50 args := url.Values{}
51 resp, err := q.prom.doRequest(ctx, http.MethodGet, q.Endpoint(), args)
52 if err != nil {
53 qr.err = fmt.Errorf("failed to query Prometheus config: %w", err)
54 return qr
55 }
56 defer resp.Body.Close()
57
58 if resp.StatusCode/100 != 2 {
59 qr.err = tryDecodingAPIError(resp)
60 return qr
61 }
62
63 qr.value, err = streamConfig(resp.Body)
64 if err != nil {
65 prometheusQueryErrorsTotal.WithLabelValues(q.prom.name, "/api/v1/status/config", errReason(err)).Inc()
66 qr.err = fmt.Errorf("failed to decode config data in %s response: %w", q.prom.safeURI, err)
67 }
68 return qr
69}
70
71func (q configQuery) Endpoint() string {
72 return "/api/v1/status/config"
73}
74
75func (q configQuery) String() string {
76 return "/api/v1/status/config"
77}
78
79func (q configQuery) CacheKey() uint64 {
80 return hash(q.prom.unsafeURI, q.Endpoint())
81}
82
83func (q configQuery) CacheTTL() time.Duration {
84 return time.Minute * 10
85}
86
87func (p *Prometheus) Config(ctx context.Context) (*ConfigResult, error) {
88 log.Debug().Str("uri", p.safeURI).Msg("Scheduling Prometheus configuration query")
89
90 key := "/api/v1/status/config"
91 p.locker.lock(key)
92 defer p.locker.unlock(key)
93
94 resultChan := make(chan queryResult)
95 p.queries <- queryRequest{
96 query: configQuery{prom: p, ctx: ctx, timestamp: time.Now()},
97 result: resultChan,
98 }
99
100 result := <-resultChan
101 if result.err != nil {
102 return nil, QueryError{err: result.err, msg: decodeError(result.err)}
103 }
104
105 r := ConfigResult{URI: p.safeURI, Config: result.value.(PrometheusConfig)}
106
107 return &r, nil
108}
109
110func streamConfig(r io.Reader) (cfg PrometheusConfig, err error) {
111 defer dummyReadAll(r)
112
113 var yamlBody, status, errType, errText string
114 decoder := current.Object(
115 current.Key("status", current.Value(func(s string, isNil bool) {
116 status = s
117 })),
118 current.Key("error", current.Value(func(s string, isNil bool) {
119 errText = s
120 })),
121 current.Key("errorType", current.Value(func(s string, isNil bool) {
122 errType = s
123 })),
124 current.Key("data", current.Object(
125 current.Key("yaml", current.Value(func(s string, isNil bool) {
126 yamlBody = s
127 })),
128 )),
129 )
130
131 dec := json.NewDecoder(r)
132 if err = decoder.Stream(dec); err != nil {
133 return cfg, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("JSON parse error: %s", err)}
134 }
135
136 if status != "success" {
137 return cfg, APIError{Status: status, ErrorType: decodeErrorType(errType), Err: errText}
138 }
139
140 if err = yaml.Unmarshal([]byte(yamlBody), &cfg); err != nil {
141 return cfg, err
142 }
143
144 if cfg.Global.ScrapeInterval == 0 {
145 cfg.Global.ScrapeInterval = time.Minute
146 }
147 if cfg.Global.ScrapeTimeout == 0 {
148 cfg.Global.ScrapeTimeout = time.Second * 10
149 }
150 if cfg.Global.EvaluationInterval == 0 {
151 cfg.Global.EvaluationInterval = time.Minute
152 }
153
154 return cfg, nil
155}
156