github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/alertmanager/alertstore/store.go (about) 1 package alertstore 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/go-kit/log" 8 "github.com/prometheus/client_golang/prometheus" 9 10 "github.com/cortexproject/cortex/pkg/alertmanager/alertspb" 11 "github.com/cortexproject/cortex/pkg/alertmanager/alertstore/bucketclient" 12 "github.com/cortexproject/cortex/pkg/alertmanager/alertstore/configdb" 13 "github.com/cortexproject/cortex/pkg/alertmanager/alertstore/local" 14 "github.com/cortexproject/cortex/pkg/alertmanager/alertstore/objectclient" 15 "github.com/cortexproject/cortex/pkg/chunk" 16 "github.com/cortexproject/cortex/pkg/chunk/aws" 17 "github.com/cortexproject/cortex/pkg/chunk/azure" 18 "github.com/cortexproject/cortex/pkg/chunk/gcp" 19 "github.com/cortexproject/cortex/pkg/configs/client" 20 "github.com/cortexproject/cortex/pkg/storage/bucket" 21 ) 22 23 // AlertStore stores and configures users rule configs 24 type AlertStore interface { 25 // ListAllUsers returns all users with alertmanager configuration. 26 ListAllUsers(ctx context.Context) ([]string, error) 27 28 // GetAlertConfigs loads and returns the alertmanager configuration for given users. 29 // If any of the provided users has no configuration, then this function does not return an 30 // error but the returned configs will not include the missing users. 31 GetAlertConfigs(ctx context.Context, userIDs []string) (map[string]alertspb.AlertConfigDesc, error) 32 33 // GetAlertConfig loads and returns the alertmanager configuration for the given user. 34 GetAlertConfig(ctx context.Context, user string) (alertspb.AlertConfigDesc, error) 35 36 // SetAlertConfig stores the alertmanager configuration for an user. 37 SetAlertConfig(ctx context.Context, cfg alertspb.AlertConfigDesc) error 38 39 // DeleteAlertConfig deletes the alertmanager configuration for an user. 40 // If configuration for the user doesn't exist, no error is reported. 41 DeleteAlertConfig(ctx context.Context, user string) error 42 43 // ListUsersWithFullState returns the list of users which have had state written. 44 ListUsersWithFullState(ctx context.Context) ([]string, error) 45 46 // GetFullState loads and returns the alertmanager state for the given user. 47 GetFullState(ctx context.Context, user string) (alertspb.FullStateDesc, error) 48 49 // SetFullState stores the alertmanager state for the given user. 50 SetFullState(ctx context.Context, user string, fs alertspb.FullStateDesc) error 51 52 // DeleteFullState deletes the alertmanager state for an user. 53 // If state for the user doesn't exist, no error is reported. 54 DeleteFullState(ctx context.Context, user string) error 55 } 56 57 // NewLegacyAlertStore returns a new alertmanager storage backend poller and store 58 func NewLegacyAlertStore(cfg LegacyConfig, logger log.Logger) (AlertStore, error) { 59 if cfg.Type == configdb.Name { 60 c, err := client.New(cfg.ConfigDB) 61 if err != nil { 62 return nil, err 63 } 64 return configdb.NewStore(c), nil 65 } 66 67 if cfg.Type == local.Name { 68 return local.NewStore(cfg.Local) 69 } 70 71 // Create the object store client. 72 var client chunk.ObjectClient 73 var err error 74 switch cfg.Type { 75 case "azure": 76 client, err = azure.NewBlobStorage(&cfg.Azure) 77 case "gcs": 78 client, err = gcp.NewGCSObjectClient(context.Background(), cfg.GCS) 79 case "s3": 80 client, err = aws.NewS3ObjectClient(cfg.S3) 81 default: 82 return nil, fmt.Errorf("unrecognized alertmanager storage backend %v, choose one of: azure, configdb, gcs, local, s3", cfg.Type) 83 } 84 if err != nil { 85 return nil, err 86 } 87 88 return objectclient.NewAlertStore(client, logger), nil 89 } 90 91 // NewAlertStore returns a alertmanager store backend client based on the provided cfg. 92 func NewAlertStore(ctx context.Context, cfg Config, cfgProvider bucket.TenantConfigProvider, logger log.Logger, reg prometheus.Registerer) (AlertStore, error) { 93 if cfg.Backend == configdb.Name { 94 c, err := client.New(cfg.ConfigDB) 95 if err != nil { 96 return nil, err 97 } 98 return configdb.NewStore(c), nil 99 } 100 101 if cfg.Backend == local.Name { 102 return local.NewStore(cfg.Local) 103 } 104 105 bucketClient, err := bucket.NewClient(ctx, cfg.Config, "alertmanager-storage", logger, reg) 106 if err != nil { 107 return nil, err 108 } 109 110 return bucketclient.NewBucketAlertStore(bucketClient, cfgProvider, logger), nil 111 }