1
- package ruler
1
+ package configs
2
2
3
3
import (
4
4
"encoding/json"
@@ -15,32 +15,34 @@ import (
15
15
16
16
// TODO: Extract configs client logic into go client library (ala users)
17
17
18
- type configID int
18
+ // A ConfigID is the ID of a single organization's Cortex configuration.
19
+ type ConfigID int
19
20
20
- type cortexConfig struct {
21
+ // A CortexConfig is a Cortex configuration for a single organization.
22
+ type CortexConfig struct {
21
23
// RulesFiles maps from a rules filename to file contents.
22
24
RulesFiles map [string ]string `json:"rules_files"`
23
25
}
24
26
25
- // cortexConfigView is what's returned from the Weave Cloud configs service
27
+ // CortexConfigView is what's returned from the Weave Cloud configs service
26
28
// when we ask for all Cortex configurations.
27
29
//
28
30
// The configs service is essentially a JSON blob store that gives each
29
31
// _version_ of a configuration a unique ID and guarantees that later versions
30
32
// have greater IDs.
31
- type cortexConfigView struct {
32
- ConfigID configID `json:"id"`
33
- Config cortexConfig `json:"config"`
33
+ type CortexConfigView struct {
34
+ ConfigID ConfigID `json:"id"`
35
+ Config CortexConfig `json:"config"`
34
36
}
35
37
36
- // cortexConfigsResponse is a response from server for getOrgConfigs
37
- type cortexConfigsResponse struct {
38
- // Configs maps organization ID to their latest cortexConfigView .
39
- Configs map [string ]cortexConfigView `json:"configs"`
38
+ // CortexConfigsResponse is a response from server for GetOrgConfigs.
39
+ type CortexConfigsResponse struct {
40
+ // Configs maps organization ID to their latest CortexConfigView .
41
+ Configs map [string ]CortexConfigView `json:"configs"`
40
42
}
41
43
42
- func configsFromJSON (body io.Reader ) (* cortexConfigsResponse , error ) {
43
- var configs cortexConfigsResponse
44
+ func configsFromJSON (body io.Reader ) (* CortexConfigsResponse , error ) {
45
+ var configs CortexConfigsResponse
44
46
if err := json .NewDecoder (body ).Decode (& configs ); err != nil {
45
47
log .Errorf ("configs: couldn't decode JSON body: %v" , err )
46
48
return nil , err
@@ -49,9 +51,9 @@ func configsFromJSON(body io.Reader) (*cortexConfigsResponse, error) {
49
51
return & configs , nil
50
52
}
51
53
52
- // getLatestConfigID returns the last config ID from a set of configs.
53
- func (c cortexConfigsResponse ) getLatestConfigID () configID {
54
- latest := configID (0 )
54
+ // GetLatestConfigID returns the last config ID from a set of configs.
55
+ func (c CortexConfigsResponse ) GetLatestConfigID () ConfigID {
56
+ latest := ConfigID (0 )
55
57
for _ , config := range c .Configs {
56
58
if config .ConfigID > latest {
57
59
latest = config .ConfigID
@@ -60,10 +62,10 @@ func (c cortexConfigsResponse) getLatestConfigID() configID {
60
62
return latest
61
63
}
62
64
63
- // Get the rules from the cortex configuration.
65
+ // GetRules gets the rules from the Cortex configuration.
64
66
//
65
67
// Strongly inspired by `loadGroups` in Prometheus.
66
- func (c cortexConfig ) GetRules () ([]rules.Rule , error ) {
68
+ func (c CortexConfig ) GetRules () ([]rules.Rule , error ) {
67
69
result := []rules.Rule {}
68
70
for fn , content := range c .RulesFiles {
69
71
stmts , err := promql .ParseStmts (content )
@@ -90,24 +92,25 @@ func (c cortexConfig) GetRules() ([]rules.Rule, error) {
90
92
return result , nil
91
93
}
92
94
93
- type configsAPI struct {
94
- url * url.URL
95
- timeout time.Duration
95
+ // API allows retrieving Cortex configs.
96
+ type API struct {
97
+ URL * url.URL
98
+ Timeout time.Duration
96
99
}
97
100
98
- // getOrgConfigs returns all Cortex configurations from a configs api server
99
- // that have been updated after the given configID was last updated.
100
- func (c * configsAPI ) getOrgConfigs (since configID ) (* cortexConfigsResponse , error ) {
101
+ // GetOrgConfigs returns all Cortex configurations from a configs API server
102
+ // that have been updated after the given ConfigID was last updated.
103
+ func (c * API ) GetOrgConfigs (since ConfigID ) (* CortexConfigsResponse , error ) {
101
104
suffix := ""
102
105
if since != 0 {
103
106
suffix = fmt .Sprintf ("?since=%d" , since )
104
107
}
105
- url := fmt .Sprintf ("%s/private/api/configs/org/cortex%s" , c .url .String (), suffix )
108
+ url := fmt .Sprintf ("%s/private/api/configs/org/cortex%s" , c .URL .String (), suffix )
106
109
req , err := http .NewRequest ("GET" , url , nil )
107
110
if err != nil {
108
111
return nil , err
109
112
}
110
- client := & http.Client {Timeout : c .timeout }
113
+ client := & http.Client {Timeout : c .Timeout }
111
114
res , err := client .Do (req )
112
115
if err != nil {
113
116
return nil , err
0 commit comments