cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.33.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/flags.go

128lines · 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, int) {
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 var qr queryResult
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, 1
43 }
44 defer resp.Body.Close()
45
46 if resp.StatusCode/100 != 2 {
47 qr.err = tryDecodingAPIError(resp)
48 return qr, 1
49 }
50
51 flags, err := streamFlags(resp.Body)
52 qr.value, qr.err = flags, err
53 if cost := len(flags); cost > 0 {
54 return qr, cost
55 }
56 return qr, 1
57}
58
59func (q flagsQuery) Endpoint() string {
60 return "/api/v1/status/flags"
61}
62
63func (q flagsQuery) String() string {
64 return "/api/v1/status/flags"
65}
66
67func (q flagsQuery) CacheKey() uint64 {
68 return hash(q.prom.unsafeURI, q.Endpoint())
69}
70
71func (q flagsQuery) CacheTTL() time.Duration {
72 return time.Minute * 10
73}
74
75func (p *Prometheus) Flags(ctx context.Context) (*FlagsResult, error) {
76 log.Debug().Str("uri", p.safeURI).Msg("Scheduling Prometheus flags query")
77
78 key := "/api/v1/status/flags"
79 p.locker.lock(key)
80 defer p.locker.unlock(key)
81
82 resultChan := make(chan queryResult)
83 p.queries <- queryRequest{
84 query: flagsQuery{prom: p, ctx: ctx, timestamp: time.Now()},
85 result: resultChan,
86 }
87
88 result := <-resultChan
89 if result.err != nil {
90 return nil, QueryError{err: result.err, msg: decodeError(result.err)}
91 }
92
93 r := FlagsResult{URI: p.safeURI, Flags: result.value.(v1.FlagsResult)}
94
95 return &r, nil
96}
97
98func streamFlags(r io.Reader) (flags v1.FlagsResult, err error) {
99 defer dummyReadAll(r)
100
101 var status, errType, errText string
102 flags = v1.FlagsResult{}
103 decoder := current.Object(
104 current.Key("status", current.Value(func(s string, isNil bool) {
105 status = s
106 })),
107 current.Key("error", current.Value(func(s string, isNil bool) {
108 errText = s
109 })),
110 current.Key("errorType", current.Value(func(s string, isNil bool) {
111 errType = s
112 })),
113 current.Key("data", current.Map(func(k, v string) {
114 flags[k] = v
115 })),
116 )
117
118 dec := json.NewDecoder(r)
119 if err = decoder.Stream(dec); err != nil {
120 return nil, APIError{Status: status, ErrorType: v1.ErrBadResponse, Err: fmt.Sprintf("JSON parse error: %s", err)}
121 }
122
123 if status != "success" {
124 return nil, APIError{Status: status, ErrorType: decodeErrorType(errType), Err: errText}
125 }
126
127 return flags, nil
128}
129