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/flags.go

124lines · 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)
16
17type FlagsResult struct {
18 URI string
19 Flags v1.FlagsResult
20}
21
22type flagsQuery struct {
23 prom *Prometheus
24 ctx context.Context
25 timestamp time.Time
26}
27
28func (q flagsQuery) Run() queryResult {
29 log.Debug().
30 Str("uri", q.prom.safeURI).
31 Msg("Getting prometheus flags")
32
33 ctx, cancel := context.WithTimeout(q.ctx, q.prom.timeout)
34 defer cancel()
35
36 qr := queryResult{expires: q.timestamp.Add(cacheExpiry * 2)}
37
38 args := url.Values{}
39 resp, err := q.prom.doRequest(ctx, http.MethodGet, q.Endpoint(), args)
40 if err != nil {
41 qr.err = fmt.Errorf("failed to query Prometheus flags: %w", err)
42 return qr
43 }
44 defer resp.Body.Close()
45
46 if resp.StatusCode/100 != 2 {
47 qr.err = tryDecodingAPIError(resp)
48 return qr
49 }
50
51 qr.value, qr.err = streamFlags(resp.Body)
52 return qr
53}
54
55func (q flagsQuery) Endpoint() string {
56 return "/api/v1/status/flags"
57}
58
59func (q flagsQuery) String() string {
60 return "/api/v1/status/flags"
61}
62
63func (q flagsQuery) CacheAfter() int {
64 return 0
65}
66
67func (q flagsQuery) CacheKey() uint64 {
68 return hash(q.prom.unsafeURI, q.Endpoint(), q.timestamp.Round(cacheExpiry).Format(time.RFC3339))
69}
70
71func (p *Prometheus) Flags(ctx context.Context) (*FlagsResult, error) {
72 log.Debug().Str("uri", p.safeURI).Msg("Scheduling Prometheus flags query")
73
74 key := "/api/v1/status/flags"
75 p.locker.lock(key)
76 defer p.locker.unlock(key)
77
78 resultChan := make(chan queryResult)
79 p.queries <- queryRequest{
80 query: flagsQuery{prom: p, ctx: ctx, timestamp: time.Now()},
81 result: resultChan,
82 }
83
84 result := <-resultChan
85 if result.err != nil {
86 return nil, QueryError{err: result.err, msg: decodeError(result.err)}
87 }
88
89 r := FlagsResult{URI: p.safeURI, Flags: result.value.(v1.FlagsResult)}
90
91 return &r, nil
92}
93
94func streamFlags(r io.Reader) (flags v1.FlagsResult, err error) {
95 defer dummyReadAll(r)
96
97 var status, errType, errText string
98 flags = v1.FlagsResult{}
99 decoder := current.Object(
100 current.Key("status", current.Value(func(s string, isNil bool) {
101 status = s
102 })),
103 current.Key("error", current.Value(func(s string, isNil bool) {
104 errText = s
105 })),
106 current.Key("errorType", current.Value(func(s string, isNil bool) {
107 errType = s
108 })),
109 current.Key("data", current.Map(func(k, v string) {
110 flags[k] = v
111 })),
112 )
113
114 dec := json.NewDecoder(r)
115 if err = decoder.Stream(dec); err != nil {
116 return nil, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("JSON parse error: %s", err)}
117 }
118
119 if status != "success" {
120 return nil, APIError{Status: status, ErrorType: decodeErrorType(errType), Err: errText}
121 }
122
123 return flags, nil
124}
125