cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.15.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/config.go

74lines · modecode

1package promapi
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/rs/zerolog/log"
9 "gopkg.in/yaml.v3"
10)
11
12type ConfigSectionGlobal struct {
13 ScrapeInterval time.Duration `yaml:"scrape_interval"`
14 ScrapeTimeout time.Duration `yaml:"scrape_timeout"`
15 EvaluationInterval time.Duration `yaml:"evaluation_interval"`
16 ExternalLabels map[string]string `yaml:"external_labels"`
17}
18
19type PrometheusConfig struct {
20 Global ConfigSectionGlobal `yaml:"global"`
21}
22
23type ConfigResult struct {
24 URI string
25 Config PrometheusConfig
26}
27
28func (p *Prometheus) Config(ctx context.Context) (*ConfigResult, error) {
29 log.Debug().Str("uri", p.uri).Msg("Query Prometheus configuration")
30
31 key := "/api/v1/status/config"
32 p.lock.lock(key)
33 defer p.lock.unlock((key))
34
35 if v, ok := p.cache.Get(key); ok {
36 log.Debug().Str("key", key).Str("uri", p.uri).Msg("Config cache hit")
37 cfg := v.(ConfigResult)
38 return &cfg, nil
39 }
40
41 ctx, cancel := context.WithTimeout(ctx, p.timeout)
42 defer cancel()
43
44 prometheusQueriesTotal.WithLabelValues(p.name, "/api/v1/status/config")
45 resp, err := p.api.Config(ctx)
46 if err != nil {
47 log.Error().Err(err).Str("uri", p.uri).Msg("Failed to query Prometheus configuration")
48 prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/status/config", errReason(err)).Inc()
49 return nil, fmt.Errorf("failed to query Prometheus config: %w", err)
50 }
51
52 var cfg PrometheusConfig
53 if err = yaml.Unmarshal([]byte(resp.YAML), &cfg); err != nil {
54 prometheusQueryErrorsTotal.WithLabelValues(p.name, "/api/v1/status/config", errReason(err)).Inc()
55 return nil, fmt.Errorf("failed to decode config data in %s response: %w", p.uri, err)
56 }
57
58 if cfg.Global.ScrapeInterval == 0 {
59 cfg.Global.ScrapeInterval = time.Minute
60 }
61 if cfg.Global.ScrapeTimeout == 0 {
62 cfg.Global.ScrapeTimeout = time.Second * 10
63 }
64 if cfg.Global.EvaluationInterval == 0 {
65 cfg.Global.EvaluationInterval = time.Minute
66 }
67
68 r := ConfigResult{URI: p.uri, Config: cfg}
69
70 log.Debug().Str("key", key).Str("uri", p.uri).Msg("Config cache miss")
71 p.cache.Add(key, r)
72
73 return &r, nil
74}
75