Skip to content

Commit 93cebe5

Browse files
committed
Move configs handling to separate package
1 parent 5b7e359 commit 93cebe5

File tree

4 files changed

+49
-41
lines changed

4 files changed

+49
-41
lines changed

ruler/configs.go renamed to configs/configs.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ruler
1+
package configs
22

33
import (
44
"encoding/json"
@@ -15,32 +15,34 @@ import (
1515

1616
// TODO: Extract configs client logic into go client library (ala users)
1717

18-
type configID int
18+
// A ConfigID is the ID of a single organization's Cortex configuration.
19+
type ConfigID int
1920

20-
type cortexConfig struct {
21+
// A CortexConfig is a Cortex configuration for a single organization.
22+
type CortexConfig struct {
2123
// RulesFiles maps from a rules filename to file contents.
2224
RulesFiles map[string]string `json:"rules_files"`
2325
}
2426

25-
// cortexConfigView is what's returned from the Weave Cloud configs service
27+
// CortexConfigView is what's returned from the Weave Cloud configs service
2628
// when we ask for all Cortex configurations.
2729
//
2830
// The configs service is essentially a JSON blob store that gives each
2931
// _version_ of a configuration a unique ID and guarantees that later versions
3032
// 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"`
3436
}
3537

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"`
4042
}
4143

42-
func configsFromJSON(body io.Reader) (*cortexConfigsResponse, error) {
43-
var configs cortexConfigsResponse
44+
func configsFromJSON(body io.Reader) (*CortexConfigsResponse, error) {
45+
var configs CortexConfigsResponse
4446
if err := json.NewDecoder(body).Decode(&configs); err != nil {
4547
log.Errorf("configs: couldn't decode JSON body: %v", err)
4648
return nil, err
@@ -49,9 +51,9 @@ func configsFromJSON(body io.Reader) (*cortexConfigsResponse, error) {
4951
return &configs, nil
5052
}
5153

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)
5557
for _, config := range c.Configs {
5658
if config.ConfigID > latest {
5759
latest = config.ConfigID
@@ -60,10 +62,10 @@ func (c cortexConfigsResponse) getLatestConfigID() configID {
6062
return latest
6163
}
6264

63-
// Get the rules from the cortex configuration.
65+
// GetRules gets the rules from the Cortex configuration.
6466
//
6567
// Strongly inspired by `loadGroups` in Prometheus.
66-
func (c cortexConfig) GetRules() ([]rules.Rule, error) {
68+
func (c CortexConfig) GetRules() ([]rules.Rule, error) {
6769
result := []rules.Rule{}
6870
for fn, content := range c.RulesFiles {
6971
stmts, err := promql.ParseStmts(content)
@@ -90,24 +92,25 @@ func (c cortexConfig) GetRules() ([]rules.Rule, error) {
9092
return result, nil
9193
}
9294

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
9699
}
97100

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) {
101104
suffix := ""
102105
if since != 0 {
103106
suffix = fmt.Sprintf("?since=%d", since)
104107
}
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)
106109
req, err := http.NewRequest("GET", url, nil)
107110
if err != nil {
108111
return nil, err
109112
}
110-
client := &http.Client{Timeout: c.timeout}
113+
client := &http.Client{Timeout: c.Timeout}
111114
res, err := client.Do(req)
112115
if err != nil {
113116
return nil, err

ruler/configs_test.go renamed to configs/configs_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ruler
1+
package configs
22

33
import (
44
"strings"
@@ -23,10 +23,10 @@ func TestJSONDecoding(t *testing.T) {
2323
}
2424
`))
2525
assert.Nil(t, err)
26-
expected := cortexConfigsResponse{Configs: map[string]cortexConfigView{
26+
expected := CortexConfigsResponse{Configs: map[string]CortexConfigView{
2727
"2": {
2828
ConfigID: 1,
29-
Config: cortexConfig{
29+
Config: CortexConfig{
3030
RulesFiles: map[string]string{
3131
"recording.rules": ":scope_authfe_request_duration_seconds:99quantile = histogram_quantile(0.99, sum(rate(scope_request_duration_seconds_bucket{ws=\"false\",job=\"authfe\",route!~\"(admin|metrics).*\"}[5m])) by (le))\n",
3232
},

ruler/ruler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/weaveworks/common/user"
1919
"github.com/weaveworks/cortex/chunk"
20+
"github.com/weaveworks/cortex/configs"
2021
"github.com/weaveworks/cortex/distributor"
2122
"github.com/weaveworks/cortex/querier"
2223
"github.com/weaveworks/cortex/util"
@@ -201,7 +202,10 @@ type Server struct {
201202

202203
// NewServer makes a new rule processing server.
203204
func NewServer(cfg Config, ruler *Ruler) (*Server, error) {
204-
c := configsAPI{cfg.ConfigsAPIURL.URL, cfg.ClientTimeout}
205+
c := configs.API{
206+
URL: cfg.ConfigsAPIURL.URL,
207+
Timeout: cfg.ClientTimeout,
208+
}
205209
// TODO: Separate configuration for polling interval.
206210
s := newScheduler(c, cfg.EvaluationInterval, cfg.EvaluationInterval)
207211
if cfg.NumWorkers <= 0 {

ruler/scheduler.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/prometheus/prometheus/rules"
1313

1414
"github.com/weaveworks/common/instrument"
15+
"github.com/weaveworks/cortex/configs"
1516
)
1617

1718
const (
@@ -61,30 +62,30 @@ func (w workItem) Defer(interval time.Duration) workItem {
6162
}
6263

6364
type scheduler struct {
64-
configsAPI configsAPI // XXX: Maybe make this an interface ConfigSource or similar.
65+
configsAPI configs.API // XXX: Maybe make this an interface ConfigSource or similar.
6566
evaluationInterval time.Duration
6667
q *SchedulingQueue
6768

6869
// All the configurations that we have. Only used for instrumentation.
69-
cfgs map[string]cortexConfig
70+
cfgs map[string]configs.CortexConfig
7071

7172
pollInterval time.Duration
7273

73-
latestConfig configID
74+
latestConfig configs.ConfigID
7475
latestMutex sync.RWMutex
7576

7677
stop chan struct{}
7778
done chan struct{}
7879
}
7980

8081
// newScheduler makes a new scheduler.
81-
func newScheduler(configsAPI configsAPI, evaluationInterval, pollInterval time.Duration) scheduler {
82+
func newScheduler(configsAPI configs.API, evaluationInterval, pollInterval time.Duration) scheduler {
8283
return scheduler{
8384
configsAPI: configsAPI,
8485
evaluationInterval: evaluationInterval,
8586
pollInterval: pollInterval,
8687
q: NewSchedulingQueue(clockwork.NewRealClock()),
87-
cfgs: map[string]cortexConfig{},
88+
cfgs: map[string]configs.CortexConfig{},
8889

8990
stop: make(chan struct{}),
9091
done: make(chan struct{}),
@@ -121,7 +122,7 @@ func (s *scheduler) Stop() {
121122

122123
// Load the full set of configurations from the server, retrying with backoff
123124
// until we can get them.
124-
func (s *scheduler) loadAllConfigs() map[string]cortexConfigView {
125+
func (s *scheduler) loadAllConfigs() map[string]configs.CortexConfigView {
125126
backoff := minBackoff
126127
for {
127128
cfgs, err := s.poll()
@@ -148,25 +149,25 @@ func (s *scheduler) updateConfigs(now time.Time) error {
148149
}
149150

150151
// poll the configuration server. Not re-entrant.
151-
func (s *scheduler) poll() (map[string]cortexConfigView, error) {
152+
func (s *scheduler) poll() (map[string]configs.CortexConfigView, error) {
152153
configID := s.latestConfig
153-
var cfgs *cortexConfigsResponse
154+
var cfgs *configs.CortexConfigsResponse
154155
err := instrument.TimeRequestHistogram(context.Background(), "Configs.GetOrgConfigs", configsRequestDuration, func(_ context.Context) error {
155156
var err error
156-
cfgs, err = s.configsAPI.getOrgConfigs(configID)
157+
cfgs, err = s.configsAPI.GetOrgConfigs(configID)
157158
return err
158159
})
159160
if err != nil {
160161
log.Warnf("Scheduler: configs server poll failed: %v", err)
161162
return nil, err
162163
}
163164
s.latestMutex.Lock()
164-
s.latestConfig = cfgs.getLatestConfigID()
165+
s.latestConfig = cfgs.GetLatestConfigID()
165166
s.latestMutex.Unlock()
166167
return cfgs.Configs, nil
167168
}
168169

169-
func (s *scheduler) addNewConfigs(now time.Time, cfgs map[string]cortexConfigView) {
170+
func (s *scheduler) addNewConfigs(now time.Time, cfgs map[string]configs.CortexConfigView) {
170171
// TODO: instrument how many configs we have, both valid & invalid.
171172
log.Debugf("Adding %d configurations", len(cfgs))
172173
for userID, config := range cfgs {

0 commit comments

Comments
 (0)