Skip to content

Refactored alertmanager storage packages #3881

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 2 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ lint:
./pkg/querier/...
faillint -paths "github.com/cortexproject/cortex/pkg/querier/..." ./pkg/scheduler/...
faillint -paths "github.com/cortexproject/cortex/pkg/storage/tsdb/..." ./pkg/storage/bucket/...
faillint -paths "github.com/cortexproject/cortex/pkg/..." ./pkg/alertmanager/alertspb/...

# Ensure the query path is supporting multiple tenants
faillint -paths "\
Expand All @@ -183,6 +184,7 @@ lint:
./pkg/frontend/... \
./pkg/querier/tenantfederation/... \
./pkg/querier/queryrange/...

# Ensure packages that no longer use a global logger don't reintroduce it
faillint -paths "github.com/cortexproject/cortex/pkg/util/log.{Logger}" \
./pkg/ingester/... \
Expand Down
8 changes: 4 additions & 4 deletions integration/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/cortexproject/cortex/integration/e2e"
e2edb "github.com/cortexproject/cortex/integration/e2e/db"
"github.com/cortexproject/cortex/integration/e2ecortex"
"github.com/cortexproject/cortex/pkg/alertmanager/alerts"
"github.com/cortexproject/cortex/pkg/alertmanager/alertspb"
s3 "github.com/cortexproject/cortex/pkg/chunk/aws"
)

Expand Down Expand Up @@ -176,7 +176,7 @@ func TestAlertmanagerClustering(t *testing.T) {

// Create and upload an Alertmanager configuration.
user := "user-1"
desc := alerts.AlertConfigDesc{RawConfig: simpleAlertmanagerConfig, User: user, Templates: []*alerts.TemplateDesc{}}
desc := alertspb.AlertConfigDesc{RawConfig: simpleAlertmanagerConfig, User: user, Templates: []*alertspb.TemplateDesc{}}

d, err := desc.Marshal()
require.NoError(t, err)
Expand Down Expand Up @@ -232,10 +232,10 @@ func TestAlertmanagerSharding(t *testing.T) {
// Create and upload Alertmanager configurations.
for i := 1; i <= 30; i++ {
user := fmt.Sprintf("user-%d", i)
desc := alerts.AlertConfigDesc{
desc := alertspb.AlertConfigDesc{
RawConfig: simpleAlertmanagerConfig,
User: user,
Templates: []*alerts.TemplateDesc{},
Templates: []*alertspb.TemplateDesc{},
}

d, err := desc.Marshal()
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package alerts;

import "github.com/gogo/protobuf/gogoproto/gogo.proto";

option go_package = "alertspb";
option (gogoproto.marshaler_all) = true;
option (gogoproto.unmarshaler_all) = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package alerts
package alertspb

import "errors"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package alertmanager
package alertstore

import (
"context"
Expand All @@ -7,27 +7,18 @@ import (

"github.com/pkg/errors"

"github.com/cortexproject/cortex/pkg/alertmanager/alerts"
"github.com/cortexproject/cortex/pkg/alertmanager/alerts/configdb"
"github.com/cortexproject/cortex/pkg/alertmanager/alerts/local"
"github.com/cortexproject/cortex/pkg/alertmanager/alerts/objectclient"
"github.com/cortexproject/cortex/pkg/alertmanager/alertstore/configdb"
"github.com/cortexproject/cortex/pkg/alertmanager/alertstore/local"
"github.com/cortexproject/cortex/pkg/alertmanager/alertstore/objectclient"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/chunk/aws"
"github.com/cortexproject/cortex/pkg/chunk/azure"
"github.com/cortexproject/cortex/pkg/chunk/gcp"
"github.com/cortexproject/cortex/pkg/configs/client"
)

// AlertStore stores and configures users rule configs
type AlertStore interface {
ListAlertConfigs(ctx context.Context) (map[string]alerts.AlertConfigDesc, error)
GetAlertConfig(ctx context.Context, user string) (alerts.AlertConfigDesc, error)
SetAlertConfig(ctx context.Context, cfg alerts.AlertConfigDesc) error
DeleteAlertConfig(ctx context.Context, user string) error
}

// AlertStoreConfig configures the alertmanager backend
type AlertStoreConfig struct {
// Config configures the alertmanager backend
type Config struct {
Type string `yaml:"type"`
ConfigDB client.Config `yaml:"configdb"`

Expand All @@ -39,7 +30,7 @@ type AlertStoreConfig struct {
}

// RegisterFlags registers flags.
func (cfg *AlertStoreConfig) RegisterFlags(f *flag.FlagSet) {
func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
cfg.ConfigDB.RegisterFlagsWithPrefix("alertmanager.", f)
f.StringVar(&cfg.Type, "alertmanager.storage.type", "configdb", "Type of backend to use to store alertmanager configs. Supported values are: \"configdb\", \"gcs\", \"s3\", \"local\".")

Expand All @@ -50,7 +41,7 @@ func (cfg *AlertStoreConfig) RegisterFlags(f *flag.FlagSet) {
}

// Validate config and returns error on failure
func (cfg *AlertStoreConfig) Validate() error {
func (cfg *Config) Validate() error {
if err := cfg.Azure.Validate(); err != nil {
return errors.Wrap(err, "invalid Azure Storage config")
}
Expand All @@ -61,7 +52,7 @@ func (cfg *AlertStoreConfig) Validate() error {
}

// NewAlertStore returns a new rule storage backend poller and store
func NewAlertStore(cfg AlertStoreConfig) (AlertStore, error) {
func NewAlertStore(cfg Config) (AlertStore, error) {
switch cfg.Type {
case "configdb":
c, err := client.New(cfg.ConfigDB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"

"github.com/cortexproject/cortex/pkg/alertmanager/alerts"
"github.com/cortexproject/cortex/pkg/alertmanager/alertspb"
"github.com/cortexproject/cortex/pkg/configs/client"
"github.com/cortexproject/cortex/pkg/configs/userconfig"
)
Expand All @@ -17,20 +17,20 @@ var (
type Store struct {
configClient client.Client
since userconfig.ID
alertConfigs map[string]alerts.AlertConfigDesc
alertConfigs map[string]alertspb.AlertConfigDesc
}

// NewStore constructs a Store
func NewStore(c client.Client) *Store {
return &Store{
configClient: c,
since: 0,
alertConfigs: make(map[string]alerts.AlertConfigDesc),
alertConfigs: make(map[string]alertspb.AlertConfigDesc),
}
}

// ListAlertConfigs implements RuleStore
func (c *Store) ListAlertConfigs(ctx context.Context) (map[string]alerts.AlertConfigDesc, error) {
// ListAlertConfigs implements alertstore.AlertStore.
func (c *Store) ListAlertConfigs(ctx context.Context) (map[string]alertspb.AlertConfigDesc, error) {

configs, err := c.configClient.GetAlerts(ctx, c.since)

Expand All @@ -44,15 +44,15 @@ func (c *Store) ListAlertConfigs(ctx context.Context) (map[string]alerts.AlertCo
continue
}

var templates []*alerts.TemplateDesc
var templates []*alertspb.TemplateDesc
for fn, template := range cfg.Config.TemplateFiles {
templates = append(templates, &alerts.TemplateDesc{
templates = append(templates, &alertspb.TemplateDesc{
Filename: fn,
Body: template,
})
}

c.alertConfigs[user] = alerts.AlertConfigDesc{
c.alertConfigs[user] = alertspb.AlertConfigDesc{
User: user,
RawConfig: cfg.Config.AlertmanagerConfig,
Templates: templates,
Expand All @@ -64,28 +64,30 @@ func (c *Store) ListAlertConfigs(ctx context.Context) (map[string]alerts.AlertCo
return c.alertConfigs, nil
}

// GetAlertConfig finds and returns the AlertManager configuration of an user.
func (c *Store) GetAlertConfig(ctx context.Context, user string) (alerts.AlertConfigDesc, error) {
// GetAlertConfig implements alertstore.AlertStore.
func (c *Store) GetAlertConfig(ctx context.Context, user string) (alertspb.AlertConfigDesc, error) {

// Refresh the local state before fetching an specific one.
_, err := c.ListAlertConfigs(ctx)
if err != nil {
return alerts.AlertConfigDesc{}, err
return alertspb.AlertConfigDesc{}, err
}

cfg, exists := c.alertConfigs[user]

if !exists {
return alerts.AlertConfigDesc{}, alerts.ErrNotFound
return alertspb.AlertConfigDesc{}, alertspb.ErrNotFound
}

return cfg, nil
}

func (c *Store) SetAlertConfig(ctx context.Context, cfg alerts.AlertConfigDesc) error {
// SetAlertConfig implements alertstore.AlertStore.
func (c *Store) SetAlertConfig(ctx context.Context, cfg alertspb.AlertConfigDesc) error {
return errReadOnly
}

// DeleteAlertConfig implements alertstore.AlertStore.
func (c *Store) DeleteAlertConfig(ctx context.Context, user string) error {
return errReadOnly
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/alertmanager/config"

"github.com/cortexproject/cortex/pkg/alertmanager/alerts"
"github.com/cortexproject/cortex/pkg/alertmanager/alertspb"
)

var (
Expand All @@ -38,9 +38,9 @@ func NewStore(cfg StoreConfig) (*Store, error) {
return &Store{cfg}, nil
}

// ListAlertConfigs returns a list of each users alertmanager config.
func (f *Store) ListAlertConfigs(ctx context.Context) (map[string]alerts.AlertConfigDesc, error) {
configs := map[string]alerts.AlertConfigDesc{}
// ListAlertConfigs implements alertstore.AlertStore.
func (f *Store) ListAlertConfigs(ctx context.Context) (map[string]alertspb.AlertConfigDesc, error) {
configs := map[string]alertspb.AlertConfigDesc{}
err := filepath.Walk(f.cfg.Path, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.Wrap(err, "unable to walk file path")
Expand All @@ -67,7 +67,7 @@ func (f *Store) ListAlertConfigs(ctx context.Context) (map[string]alerts.AlertCo
// The file name must correspond to the user tenant ID
user := strings.TrimSuffix(info.Name(), ext)

configs[user] = alerts.AlertConfigDesc{
configs[user] = alertspb.AlertConfigDesc{
User: user,
RawConfig: string(content),
}
Expand All @@ -81,25 +81,28 @@ func (f *Store) ListAlertConfigs(ctx context.Context) (map[string]alerts.AlertCo
return configs, nil
}

func (f *Store) GetAlertConfig(ctx context.Context, user string) (alerts.AlertConfigDesc, error) {
// GetAlertConfig implements alertstore.AlertStore.
func (f *Store) GetAlertConfig(ctx context.Context, user string) (alertspb.AlertConfigDesc, error) {
cfgs, err := f.ListAlertConfigs(ctx)
if err != nil {
return alerts.AlertConfigDesc{}, err
return alertspb.AlertConfigDesc{}, err
}

cfg, exists := cfgs[user]

if !exists {
return alerts.AlertConfigDesc{}, alerts.ErrNotFound
return alertspb.AlertConfigDesc{}, alertspb.ErrNotFound
}

return cfg, nil
}

func (f *Store) SetAlertConfig(ctx context.Context, cfg alerts.AlertConfigDesc) error {
// SetAlertConfig implements alertstore.AlertStore.
func (f *Store) SetAlertConfig(ctx context.Context, cfg alertspb.AlertConfigDesc) error {
return errReadOnly
}

// DeleteAlertConfig implements alertstore.AlertStore.
func (f *Store) DeleteAlertConfig(ctx context.Context, user string) error {
return errReadOnly
}
Loading