-
Notifications
You must be signed in to change notification settings - Fork 838
Easy to use Cortex: Single binary, single process #1262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e6a3e96
Single binary, single process Cortex.
tomwilkie 8a019d3
Unify the config client between the alertmanager and the ruler.
tomwilkie 5238d2c
Prefix frontend memcache flags with 'frontend.'
tomwilkie 4fa102b
Don't register the config DB flags twice.
tomwilkie ea98df4
Remove the FromStr parsing hack in schema config, do it with a custom…
tomwilkie a3a7538
Add yaml struct tags to more config fields; Manage lifecycle of overi��
tomwilkie 067009e
Add -print.config flag, which causes Cortex to print config and exit.
tomwilkie a5dee71
Add getting started guide and example config for running as a single …
tomwilkie f8c9ac1
Review feedback & minor fixups.
tomwilkie 453443e
Update modules.txt
tomwilkie f43d891
Fixes from testing as microservices.
tomwilkie 310897b
Review feedback.
tomwilkie 7cb7de9
Update CI for moved migrations.
tomwilkie cee654f
Review feedback
tomwilkie 491fb2a
Register alertmanager config.
tomwilkie 4ac3bf5
Allow alertmanager to start speaking directly to the DB.
tomwilkie ccd9362
Add /user_stats handler
tomwilkie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Unify the config client between the alertmanager and the ruler.
Signed-off-by: Tom Wilkie <tom.wilkie@gmail.com>
- Loading branch information
commit 8a019d3f6716a43ed82fd3ab292aff58de94eb53
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/configs" | ||
| "github.com/cortexproject/cortex/pkg/configs/db" | ||
| "github.com/cortexproject/cortex/pkg/util" | ||
| "github.com/go-kit/kit/log/level" | ||
| ) | ||
|
|
||
| // Client is what the ruler and altermanger needs from a config store to process rules. | ||
| type Client interface { | ||
| // GetRules returns all Cortex configurations from a configs API server | ||
| // that have been updated after the given configs.ID was last updated. | ||
| GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error) | ||
|
|
||
| // GetAlerts fetches all the alerts that have changes since since. | ||
| GetAlerts(since configs.ID) (*ConfigsResponse, error) | ||
| } | ||
|
|
||
| // New creates a new ConfigClient. | ||
| func New(cfg Config) (Client, error) { | ||
| // All of this falderal is to allow for a smooth transition away from | ||
| // using the configs server and toward directly connecting to the database. | ||
| // See https://github.com/cortexproject/cortex/issues/619 | ||
| if cfg.ConfigsAPIURL.URL != nil { | ||
| return instrumented{ | ||
| next: configsClient{ | ||
| URL: cfg.ConfigsAPIURL.URL, | ||
| Timeout: cfg.ClientTimeout, | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| db, err := db.NewRulesDB(cfg.DBConfig) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return instrumented{ | ||
| next: dbStore{ | ||
| db: db, | ||
| }, | ||
| }, nil | ||
| } | ||
|
|
||
| // configsClient allows retrieving recording and alerting rules from the configs server. | ||
| type configsClient struct { | ||
| URL *url.URL | ||
| Timeout time.Duration | ||
| } | ||
|
|
||
| // GetRules implements ConfigClient. | ||
| func (c configsClient) GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error) { | ||
| suffix := "" | ||
| if since != 0 { | ||
| suffix = fmt.Sprintf("?since=%d", since) | ||
| } | ||
| endpoint := fmt.Sprintf("%s/private/api/prom/configs/rules%s", c.URL.String(), suffix) | ||
| response, err := doRequest(endpoint, c.Timeout, since) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| configs := map[string]configs.VersionedRulesConfig{} | ||
| for id, view := range response.Configs { | ||
| cfg := view.GetVersionedRulesConfig() | ||
| if cfg != nil { | ||
| configs[id] = *cfg | ||
| } | ||
| } | ||
| return configs, nil | ||
| } | ||
|
|
||
| // GetAlerts implements ConfigClient. | ||
| func (c configsClient) GetAlerts(since configs.ID) (*ConfigsResponse, error) { | ||
| suffix := "" | ||
| if since != 0 { | ||
| suffix = fmt.Sprintf("?since=%d", since) | ||
| } | ||
| endpoint := fmt.Sprintf("%s/private/api/prom/configs/alertmanager%s", c.URL.String(), suffix) | ||
| return doRequest(endpoint, c.Timeout, since) | ||
| } | ||
|
|
||
| func doRequest(endpoint string, timeout time.Duration, since configs.ID) (*ConfigsResponse, error) { | ||
| req, err := http.NewRequest("GET", endpoint, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| client := &http.Client{Timeout: timeout} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("Invalid response from configs server: %v", resp.StatusCode) | ||
| } | ||
|
|
||
| var config ConfigsResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&config); err != nil { | ||
| level.Error(util.Logger).Log("msg", "configs: couldn't decode JSON body", "err", err) | ||
| return nil, err | ||
| } | ||
|
|
||
| config.since = since | ||
| return &config, nil | ||
| } | ||
|
|
||
| type dbStore struct { | ||
| db db.RulesDB | ||
| } | ||
|
|
||
| // GetRules implements ConfigClient. | ||
| func (d dbStore) GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error) { | ||
| if since == 0 { | ||
| return d.db.GetAllRulesConfigs() | ||
| } | ||
| return d.db.GetRulesConfigs(since) | ||
| } | ||
|
|
||
| // GetAlerts implements ConfigClient. | ||
| func (d dbStore) GetAlerts(since configs.ID) (*ConfigsResponse, error) { | ||
| // TODO implement this! | ||
| return nil, nil | ||
| } | ||
|
|
||
| // ConfigsResponse is a response from server for GetConfigs. | ||
| type ConfigsResponse struct { | ||
| // The version since which these configs were changed | ||
| since configs.ID | ||
|
|
||
| // Configs maps user ID to their latest configs.View. | ||
| Configs map[string]configs.View `json:"configs"` | ||
| } | ||
|
|
||
| // GetLatestConfigID returns the last config ID from a set of configs. | ||
| func (c ConfigsResponse) GetLatestConfigID() configs.ID { | ||
| latest := c.since | ||
| for _, config := range c.Configs { | ||
| if config.ID > latest { | ||
| latest = config.ID | ||
| } | ||
| } | ||
| return latest | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "github.com/weaveworks/common/instrument" | ||
|
|
||
| "github.com/cortexproject/cortex/pkg/configs" | ||
| "github.com/cortexproject/cortex/pkg/configs/db" | ||
| "github.com/cortexproject/cortex/pkg/util/flagext" | ||
| ) | ||
|
|
||
| // Config says where we can find the ruler configs. | ||
| type Config struct { | ||
| DBConfig db.Config | ||
|
|
||
| // DEPRECATED | ||
| ConfigsAPIURL flagext.URLValue | ||
|
|
||
| // DEPRECATED. HTTP timeout duration for requests made to the Weave Cloud | ||
| // configs service. | ||
| ClientTimeout time.Duration | ||
| } | ||
|
|
||
| // RegisterFlags adds the flags required to config this to the given FlagSet | ||
| func (cfg *Config) RegisterFlags(f *flag.FlagSet) { | ||
| cfg.DBConfig.RegisterFlags(f) | ||
| f.Var(&cfg.ConfigsAPIURL, "ruler.configs.url", "DEPRECATED. URL of configs API server.") | ||
| f.DurationVar(&cfg.ClientTimeout, "ruler.client-timeout", 5*time.Second, "DEPRECATED. Timeout for requests to Weave Cloud configs service.") | ||
| } | ||
|
|
||
| var configsRequestDuration = instrument.NewHistogramCollector(prometheus.NewHistogramVec(prometheus.HistogramOpts{ | ||
| Namespace: "cortex", | ||
| Name: "configs_request_duration_seconds", | ||
| Help: "Time spent requesting configs.", | ||
| Buckets: prometheus.DefBuckets, | ||
| }, []string{"operation", "status_code"})) | ||
|
|
||
| func init() { | ||
| configsRequestDuration.Register() | ||
| } | ||
|
|
||
| type instrumented struct { | ||
| next Client | ||
| } | ||
|
|
||
| func (i instrumented) GetRules(since configs.ID) (map[string]configs.VersionedRulesConfig, error) { | ||
| var cfgs map[string]configs.VersionedRulesConfig | ||
| err := instrument.CollectedRequest(context.Background(), "Configs.GetConfigs", configsRequestDuration, instrument.ErrorCode, func(_ context.Context) error { | ||
| var err error | ||
| cfgs, err = i.next.GetRules(since) // Warning: this will produce an incorrect result if the configID ever overflows | ||
| return err | ||
| }) | ||
| return cfgs, err | ||
| } | ||
|
|
||
| func (i instrumented) GetAlerts(since configs.ID) (*ConfigsResponse, error) { | ||
| var cfgs *ConfigsResponse | ||
| err := instrument.CollectedRequest(context.Background(), "Configs.GetConfigs", configsRequestDuration, instrument.ErrorCode, func(_ context.Context) error { | ||
| var err error | ||
| cfgs, err = i.next.GetAlerts(since) // Warning: this will produce an incorrect result if the configID ever overflows | ||
| return err | ||
| }) | ||
| return cfgs, err | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This interface looks to be missing a
GetTemplatesmethod for alert template files, which were added in PR #1237.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @khaines - I don't understand - I don't see a
GetTemplatesmethod in that PR (or anywhere in the codebase). What am I missing?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think we're good - GetAlerts returns a ConfigsResponse which contains the templates, like before.