cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.65.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/flags.go

127lines · modecode

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