Skip to content

Commit a2b6928

Browse files
authored
Merge pull request #1238 from cloudflare/apis
Store API paths in one place
2 parents 05412b4 + da74f97 commit a2b6928

File tree

10 files changed

+64
-45
lines changed

10 files changed

+64
-45
lines changed

cmd/pint/bench_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/cloudflare/pint/internal/git"
1818
"github.com/cloudflare/pint/internal/log"
1919
"github.com/cloudflare/pint/internal/parser"
20+
"github.com/cloudflare/pint/internal/promapi"
2021
)
2122

2223
func BenchmarkFindEntries(b *testing.B) {
@@ -50,23 +51,23 @@ func BenchmarkCheckRules(b *testing.B) {
5051

5152
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5253
switch r.URL.Path {
53-
case "/api/v1/status/config":
54+
case promapi.APIPathConfig:
5455
w.WriteHeader(200)
5556
w.Header().Set("Content-Type", "application/json")
5657
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`))
57-
case "/api/v1/status/flags":
58+
case promapi.APIPathFlags:
5859
w.WriteHeader(200)
5960
w.Header().Set("Content-Type", "application/json")
6061
_, _ = w.Write([]byte(`{"status":"success","data":{"storage.tsdb.retention.time": "1d"}}`))
61-
case "/api/v1/metadata":
62+
case promapi.APIPathMetadata:
6263
w.WriteHeader(200)
6364
w.Header().Set("Content-Type", "application/json")
6465
_, _ = w.Write([]byte(`{"status":"success","data":{}}`))
65-
case "/api/v1/query":
66+
case promapi.APIPathQuery:
6667
w.WriteHeader(200)
6768
w.Header().Set("Content-Type", "application/json")
6869
_, _ = w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[]}}`))
69-
case "/api/v1/query_range":
70+
case promapi.APIPathQueryRange:
7071
w.WriteHeader(200)
7172
w.Header().Set("Content-Type", "application/json")
7273
_, _ = w.Write([]byte(`{"status":"success","data":{"resultType":"matrix","result":[]}}`))

internal/checks/base_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ func (fc formCond) isMatch(r *http.Request) bool {
285285
}
286286

287287
var (
288-
requireConfigPath = requestPathCond{path: "/api/v1/status/config"}
289-
requireFlagsPath = requestPathCond{path: "/api/v1/status/flags"}
290-
requireQueryPath = requestPathCond{path: "/api/v1/query"}
291-
requireRangeQueryPath = requestPathCond{path: "/api/v1/query_range"}
292-
requireMetadataPath = requestPathCond{path: "/api/v1/metadata"}
288+
requireConfigPath = requestPathCond{path: promapi.APIPathConfig}
289+
requireFlagsPath = requestPathCond{path: promapi.APIPathFlags}
290+
requireQueryPath = requestPathCond{path: promapi.APIPathQuery}
291+
requireRangeQueryPath = requestPathCond{path: promapi.APIPathQueryRange}
292+
requireMetadataPath = requestPathCond{path: promapi.APIPathMetadata}
293293
)
294294

295295
type promError struct {

internal/checks/promql_series_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,7 +3975,7 @@ func TestSeriesCheck(t *testing.T) {
39753975
},
39763976
mocks: []*prometheusMock{
39773977
{
3978-
conds: []requestCondition{requestPathCond{path: "/other/api/v1/query"}},
3978+
conds: []requestCondition{requestPathCond{path: "/other" + promapi.APIPathQuery}},
39793979
resp: respondWithEmptyVector(),
39803980
},
39813981
{
@@ -4016,7 +4016,7 @@ func TestSeriesCheck(t *testing.T) {
40164016
},
40174017
mocks: []*prometheusMock{
40184018
{
4019-
conds: []requestCondition{requestPathCond{path: "/other/api/v1/query"}},
4019+
conds: []requestCondition{requestPathCond{path: "/other" + promapi.APIPathQuery}},
40204020
resp: respondWithSingleInstantVector(),
40214021
},
40224022
{
@@ -4057,7 +4057,7 @@ func TestSeriesCheck(t *testing.T) {
40574057
},
40584058
mocks: []*prometheusMock{
40594059
{
4060-
conds: []requestCondition{requestPathCond{path: "/other/api/v1/query"}},
4060+
conds: []requestCondition{requestPathCond{path: "/other" + promapi.APIPathQuery}},
40614061
resp: respondWithSingleInstantVector(),
40624062
},
40634063
{
@@ -4108,7 +4108,7 @@ func TestSeriesCheck(t *testing.T) {
41084108
},
41094109
mocks: []*prometheusMock{
41104110
{
4111-
conds: []requestCondition{requestPathCond{path: "/other/api/v1/query"}},
4111+
conds: []requestCondition{requestPathCond{path: "/other" + promapi.APIPathQuery}},
41124112
resp: sleepResponse{
41134113
sleep: time.Millisecond * 20,
41144114
resp: respondWithSingleInstantVector(),
@@ -4163,7 +4163,7 @@ func TestSeriesCheck(t *testing.T) {
41634163
},
41644164
mocks: []*prometheusMock{
41654165
{
4166-
conds: []requestCondition{requestPathCond{path: "/other/api/v1/query"}},
4166+
conds: []requestCondition{requestPathCond{path: "/other" + promapi.APIPathQuery}},
41674167
resp: sleepResponse{
41684168
sleep: time.Millisecond * 230,
41694169
resp: respondWithSingleInstantVector(),

internal/promapi/config.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
"gopkg.in/yaml.v3"
1616
)
1717

18+
const (
19+
APIPathConfig = "/api/v1/status/config"
20+
)
21+
1822
type ConfigSectionGlobal struct {
1923
ExternalLabels map[string]string `yaml:"external_labels"`
2024
ScrapeInterval time.Duration `yaml:"scrape_interval"`
@@ -62,18 +66,18 @@ func (q configQuery) Run() queryResult {
6266

6367
qr.value, err = streamConfig(resp.Body)
6468
if err != nil {
65-
prometheusQueryErrorsTotal.WithLabelValues(q.prom.name, "/api/v1/status/config", errReason(err)).Inc()
69+
prometheusQueryErrorsTotal.WithLabelValues(q.prom.name, APIPathConfig, errReason(err)).Inc()
6670
qr.err = fmt.Errorf("failed to decode config data in %s response: %w", q.prom.safeURI, err)
6771
}
6872
return qr
6973
}
7074

7175
func (q configQuery) Endpoint() string {
72-
return "/api/v1/status/config"
76+
return APIPathConfig
7377
}
7478

7579
func (q configQuery) String() string {
76-
return "/api/v1/status/config"
80+
return APIPathConfig
7781
}
7882

7983
func (q configQuery) CacheKey() uint64 {
@@ -87,9 +91,8 @@ func (q configQuery) CacheTTL() time.Duration {
8791
func (p *Prometheus) Config(ctx context.Context, cacheTTL time.Duration) (*ConfigResult, error) {
8892
slog.Debug("Scheduling Prometheus configuration query", slog.String("uri", p.safeURI))
8993

90-
key := "/api/v1/status/config"
91-
p.locker.lock(key)
92-
defer p.locker.unlock(key)
94+
p.locker.lock(APIPathConfig)
95+
defer p.locker.unlock(APIPathConfig)
9396

9497
if cacheTTL == 0 {
9598
cacheTTL = time.Minute

internal/promapi/config_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,31 @@ import (
2020
func TestConfig(t *testing.T) {
2121
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2222
switch r.URL.Path {
23-
case "/30s/api/v1/status/config":
23+
case "/30s" + promapi.APIPathConfig:
2424
w.WriteHeader(200)
2525
w.Header().Set("Content-Type", "application/json")
2626
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 30s\n"}}`))
27-
case "/1m/api/v1/status/config":
27+
case "/1m" + promapi.APIPathConfig:
2828
w.WriteHeader(200)
2929
w.Header().Set("Content-Type", "application/json")
3030
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n scrape_interval: 1m\n"}}`))
31-
case "/default/api/v1/status/config":
31+
case "/default" + promapi.APIPathConfig:
3232
w.WriteHeader(200)
3333
w.Header().Set("Content-Type", "application/json")
3434
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`))
35-
case "/once/api/v1/status/config":
35+
case "/once" + promapi.APIPathConfig:
3636
w.WriteHeader(200)
3737
w.Header().Set("Content-Type", "application/json")
3838
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`))
39-
case "/slow/api/v1/status/config":
39+
case "/slow" + promapi.APIPathConfig:
4040
w.WriteHeader(200)
4141
w.Header().Set("Content-Type", "application/json")
4242
time.Sleep(time.Second * 2)
4343
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"global:\n {}\n"}}`))
44-
case "/error/api/v1/status/config":
44+
case "/error" + promapi.APIPathConfig:
4545
w.WriteHeader(500)
4646
_, _ = w.Write([]byte("fake error\n"))
47-
case "/badYaml/api/v1/status/config":
47+
case "/badYaml" + promapi.APIPathConfig:
4848
w.WriteHeader(200)
4949
w.Header().Set("Content-Type", "application/json")
5050
_, _ = w.Write([]byte(`{"status":"success","data":{"yaml":"invalid yaml"}}`))

internal/promapi/flags.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/prymitive/current"
1515
)
1616

17+
const (
18+
APIPathFlags = "/api/v1/status/flags"
19+
)
20+
1721
type FlagsResult struct {
1822
Flags v1.FlagsResult
1923
URI string
@@ -52,11 +56,11 @@ func (q flagsQuery) Run() queryResult {
5256
}
5357

5458
func (q flagsQuery) Endpoint() string {
55-
return "/api/v1/status/flags"
59+
return APIPathFlags
5660
}
5761

5862
func (q flagsQuery) String() string {
59-
return "/api/v1/status/flags"
63+
return APIPathFlags
6064
}
6165

6266
func (q flagsQuery) CacheKey() uint64 {
@@ -70,9 +74,8 @@ func (q flagsQuery) CacheTTL() time.Duration {
7074
func (p *Prometheus) Flags(ctx context.Context) (*FlagsResult, error) {
7175
slog.Debug("Scheduling Prometheus flags query", slog.String("uri", p.safeURI))
7276

73-
key := "/api/v1/status/flags"
74-
p.locker.lock(key)
75-
defer p.locker.unlock(key)
77+
p.locker.lock(APIPathFlags)
78+
defer p.locker.unlock(APIPathFlags)
7679

7780
resultChan := make(chan queryResult)
7881
p.queries <- queryRequest{

internal/promapi/flags_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ import (
1818
func TestFlags(t *testing.T) {
1919
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2020
switch r.URL.Path {
21-
case "/default/api/v1/status/flags":
21+
case "/default" + promapi.APIPathFlags:
2222
w.WriteHeader(200)
2323
w.Header().Set("Content-Type", "application/json")
2424
_, _ = w.Write([]byte(`{"status":"success","data":{}}`))
25-
case "/foo/api/v1/status/flags":
25+
case "/foo" + promapi.APIPathFlags:
2626
w.WriteHeader(200)
2727
w.Header().Set("Content-Type", "application/json")
2828
_, _ = w.Write([]byte(`{"status":"success","data":{"foo":"bar"}}`))
29-
case "/once/api/v1/status/flags":
29+
case "/once" + promapi.APIPathFlags:
3030
w.WriteHeader(200)
3131
w.Header().Set("Content-Type", "application/json")
3232
_, _ = w.Write([]byte(`{"status":"success","data":{}}`))
33-
case "/slow/api/v1/status/flags":
33+
case "/slow" + promapi.APIPathFlags:
3434
w.WriteHeader(200)
3535
w.Header().Set("Content-Type", "application/json")
3636
time.Sleep(time.Second * 2)
3737
_, _ = w.Write([]byte(`{"status":"success","data":{}}`))
38-
case "/error/api/v1/status/flags":
38+
case "/error" + promapi.APIPathFlags:
3939
w.WriteHeader(500)
4040
_, _ = w.Write([]byte("fake error\n"))
41-
case "/badYaml/api/v1/status/flags":
41+
case "/badYaml" + promapi.APIPathFlags:
4242
w.WriteHeader(200)
4343
w.Header().Set("Content-Type", "application/json")
4444
_, _ = w.Write([]byte(`{"status":"success","data":{"xxx"}}`))

internal/promapi/metadata.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/prymitive/current"
1515
)
1616

17+
const (
18+
APIPathMetadata = "/api/v1/metadata"
19+
)
20+
1721
type MetadataResult struct {
1822
URI string
1923
Metadata []v1.Metadata
@@ -58,7 +62,7 @@ func (q metadataQuery) Run() queryResult {
5862
}
5963

6064
func (q metadataQuery) Endpoint() string {
61-
return "/api/v1/metadata"
65+
return APIPathMetadata
6266
}
6367

6468
func (q metadataQuery) String() string {
@@ -76,7 +80,7 @@ func (q metadataQuery) CacheTTL() time.Duration {
7680
func (p *Prometheus) Metadata(ctx context.Context, metric string) (*MetadataResult, error) {
7781
slog.Debug("Scheduling Prometheus metrics metadata query", slog.String("uri", p.safeURI), slog.String("metric", metric))
7882

79-
key := "/api/v1/metadata/" + metric
83+
key := APIPathMetadata + metric
8084
p.locker.lock(key)
8185
defer p.locker.unlock(key)
8286

internal/promapi/query.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
"github.com/prymitive/current"
1818
)
1919

20+
const (
21+
APIPathQuery = "/api/v1/query"
22+
)
23+
2024
type QueryResult struct {
2125
URI string
2226
Series []Sample
@@ -63,7 +67,7 @@ func (q instantQuery) Run() queryResult {
6367
}
6468

6569
func (q instantQuery) Endpoint() string {
66-
return "/api/v1/query"
70+
return APIPathQuery
6771
}
6872

6973
func (q instantQuery) String() string {
@@ -81,7 +85,7 @@ func (q instantQuery) CacheTTL() time.Duration {
8185
func (p *Prometheus) Query(ctx context.Context, expr string) (*QueryResult, error) {
8286
slog.Debug("Scheduling prometheus query", slog.String("uri", p.safeURI), slog.String("query", expr))
8387

84-
key := "/api/v1/query/" + expr
88+
key := APIPathQuery + expr
8589
p.locker.lock(key)
8690
defer p.locker.unlock(key)
8791

internal/promapi/range.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import (
2222
"github.com/cloudflare/pint/internal/output"
2323
)
2424

25+
const (
26+
APIPathQueryRange = "/api/v1/query_range"
27+
)
28+
2529
type RangeQueryResult struct {
2630
URI string
2731
Series SeriesTimeRanges
@@ -69,7 +73,7 @@ func (q rangeQuery) Run() queryResult {
6973
}
7074

7175
func (q rangeQuery) Endpoint() string {
72-
return "/api/v1/query_range"
76+
return APIPathQueryRange
7377
}
7478

7579
func (q rangeQuery) String() string {
@@ -116,7 +120,7 @@ func (p *Prometheus) RangeQuery(ctx context.Context, expr string, params RangeQu
116120
slog.Int("slices", len(slices)),
117121
)
118122

119-
key := fmt.Sprintf("/api/v1/query_range/%s/%s", expr, params.String())
123+
key := fmt.Sprintf("%s/%s/%s", APIPathQueryRange, expr, params.String())
120124
p.locker.lock(key)
121125
defer p.locker.unlock(key)
122126

0 commit comments

Comments
 (0)