cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.22.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/config.go

98lines · modecode

1package promapi
2
3import (
4 "context"
5 "crypto/sha1"
6 "fmt"
7 "io"
8 "time"
9
10 v1 "github.com/prometheus/client_golang/api/prometheus/v1"
11 "github.com/rs/zerolog/log"
12 "gopkg.in/yaml.v3"
13)
14
15type ConfigSectionGlobal struct {
16 ScrapeInterval time.Duration `yaml:"scrape_interval"`
17 ScrapeTimeout time.Duration `yaml:"scrape_timeout"`
18 EvaluationInterval time.Duration `yaml:"evaluation_interval"`
19 ExternalLabels map[string]string `yaml:"external_labels"`
20}
21
22type PrometheusConfig struct {
23 Global ConfigSectionGlobal `yaml:"global"`
24}
25
26type ConfigResult struct {
27 URI string
28 Config PrometheusConfig
29}
30
31type configQuery struct {
32 prom *Prometheus
33 ctx context.Context
34}
35
36func (q configQuery) Run() (any, error) {
37 log.Debug().
38 Str("uri", q.prom.uri).
39 Msg("Getting prometheus configuration")
40
41 ctx, cancel := context.WithTimeout(q.ctx, q.prom.timeout)
42 defer cancel()
43
44 v, err := q.prom.api.Config(ctx)
45 if err != nil {
46 return nil, fmt.Errorf("failed to query Prometheus config: %w", err)
47 }
48 return v, nil
49}
50
51func (q configQuery) Endpoint() string {
52 return "/api/v1/status/config"
53}
54
55func (q configQuery) String() string {
56 return "/api/v1/status/config"
57}
58
59func (q configQuery) CacheKey() string {
60 h := sha1.New()
61 _, _ = io.WriteString(h, q.Endpoint())
62 return fmt.Sprintf("%x", h.Sum(nil))
63}
64
65func (p *Prometheus) Config(ctx context.Context) (*ConfigResult, error) {
66 log.Debug().Str("uri", p.uri).Msg("Scheduling Prometheus configuration query")
67
68 resultChan := make(chan queryResult)
69 p.queries <- queryRequest{
70 query: configQuery{prom: p, ctx: ctx},
71 result: resultChan,
72 }
73
74 result := <-resultChan
75 if result.err != nil {
76 return nil, QueryError{err: result.err, msg: decodeError(result.err)}
77 }
78
79 var cfg PrometheusConfig
80 if err := yaml.Unmarshal([]byte(result.value.(v1.ConfigResult).YAML), &cfg); err != nil {
81 prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/status/config", errReason(err)).Inc()
82 return nil, fmt.Errorf("failed to decode config data in %s response: %w", p.uri, err)
83 }
84
85 if cfg.Global.ScrapeInterval == 0 {
86 cfg.Global.ScrapeInterval = time.Minute
87 }
88 if cfg.Global.ScrapeTimeout == 0 {
89 cfg.Global.ScrapeTimeout = time.Second * 10
90 }
91 if cfg.Global.EvaluationInterval == 0 {
92 cfg.Global.EvaluationInterval = time.Minute
93 }
94
95 r := ConfigResult{URI: p.uri, Config: cfg}
96
97 return &r, nil
98}
99