github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/ruler/base/storage.go (about)

     1  package base
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  
     8  	"github.com/go-kit/log"
     9  	"github.com/pkg/errors"
    10  	"github.com/prometheus/client_golang/prometheus"
    11  	promRules "github.com/prometheus/prometheus/rules"
    12  
    13  	configClient "github.com/grafana/loki/pkg/configs/client"
    14  	"github.com/grafana/loki/pkg/ruler/rulestore"
    15  	"github.com/grafana/loki/pkg/ruler/rulestore/bucketclient"
    16  	"github.com/grafana/loki/pkg/ruler/rulestore/configdb"
    17  	"github.com/grafana/loki/pkg/ruler/rulestore/local"
    18  	"github.com/grafana/loki/pkg/ruler/rulestore/objectclient"
    19  	"github.com/grafana/loki/pkg/storage"
    20  	"github.com/grafana/loki/pkg/storage/bucket"
    21  	"github.com/grafana/loki/pkg/storage/chunk/client"
    22  	"github.com/grafana/loki/pkg/storage/chunk/client/aws"
    23  	"github.com/grafana/loki/pkg/storage/chunk/client/azure"
    24  	"github.com/grafana/loki/pkg/storage/chunk/client/baidubce"
    25  	"github.com/grafana/loki/pkg/storage/chunk/client/gcp"
    26  	"github.com/grafana/loki/pkg/storage/chunk/client/hedging"
    27  	"github.com/grafana/loki/pkg/storage/chunk/client/openstack"
    28  )
    29  
    30  // RuleStoreConfig configures a rule store.
    31  // TODO remove this legacy config in Cortex 1.11.
    32  type RuleStoreConfig struct {
    33  	Type     string              `yaml:"type"`
    34  	ConfigDB configClient.Config `yaml:"configdb"`
    35  
    36  	// Object Storage Configs
    37  	Azure azure.BlobStorageConfig   `yaml:"azure"`
    38  	GCS   gcp.GCSConfig             `yaml:"gcs"`
    39  	S3    aws.S3Config              `yaml:"s3"`
    40  	BOS   baidubce.BOSStorageConfig `yaml:"bos"`
    41  	Swift openstack.SwiftConfig     `yaml:"swift"`
    42  	Local local.Config              `yaml:"local"`
    43  
    44  	mock rulestore.RuleStore `yaml:"-"`
    45  }
    46  
    47  // RegisterFlags registers flags.
    48  func (cfg *RuleStoreConfig) RegisterFlags(f *flag.FlagSet) {
    49  	cfg.ConfigDB.RegisterFlagsWithPrefix("ruler.", f)
    50  	cfg.Azure.RegisterFlagsWithPrefix("ruler.storage.", f)
    51  	cfg.GCS.RegisterFlagsWithPrefix("ruler.storage.", f)
    52  	cfg.S3.RegisterFlagsWithPrefix("ruler.storage.", f)
    53  	cfg.Swift.RegisterFlagsWithPrefix("ruler.storage.", f)
    54  	cfg.Local.RegisterFlagsWithPrefix("ruler.storage.", f)
    55  	cfg.BOS.RegisterFlagsWithPrefix("ruler.storage.", f)
    56  	f.StringVar(&cfg.Type, "ruler.storage.type", "configdb", "Method to use for backend rule storage (configdb, azure, gcs, s3, swift, local)")
    57  }
    58  
    59  // Validate config and returns error on failure
    60  func (cfg *RuleStoreConfig) Validate() error {
    61  	if err := cfg.Swift.Validate(); err != nil {
    62  		return errors.Wrap(err, "invalid Swift Storage config")
    63  	}
    64  	if err := cfg.Azure.Validate(); err != nil {
    65  		return errors.Wrap(err, "invalid Azure Storage config")
    66  	}
    67  	if err := cfg.S3.Validate(); err != nil {
    68  		return errors.Wrap(err, "invalid S3 Storage config")
    69  	}
    70  	return nil
    71  }
    72  
    73  // IsDefaults returns true if the storage options have not been set
    74  func (cfg *RuleStoreConfig) IsDefaults() bool {
    75  	return cfg.Type == "configdb" && cfg.ConfigDB.ConfigsAPIURL.URL == nil
    76  }
    77  
    78  // NewLegacyRuleStore returns a rule store backend client based on the provided cfg.
    79  // The client used by the function is based a legacy object store clients that shouldn't
    80  // be used anymore.
    81  func NewLegacyRuleStore(cfg RuleStoreConfig, hedgeCfg hedging.Config, clientMetrics storage.ClientMetrics, loader promRules.GroupLoader, logger log.Logger) (rulestore.RuleStore, error) {
    82  	if cfg.mock != nil {
    83  		return cfg.mock, nil
    84  	}
    85  
    86  	if loader == nil {
    87  		loader = promRules.FileLoader{}
    88  	}
    89  
    90  	var err error
    91  	var client client.ObjectClient
    92  
    93  	switch cfg.Type {
    94  	case "configdb":
    95  		c, err := configClient.New(cfg.ConfigDB)
    96  		if err != nil {
    97  			return nil, err
    98  		}
    99  		return configdb.NewConfigRuleStore(c), nil
   100  	case "azure":
   101  		client, err = azure.NewBlobStorage(&cfg.Azure, clientMetrics.AzureMetrics, hedgeCfg)
   102  	case "gcs":
   103  		client, err = gcp.NewGCSObjectClient(context.Background(), cfg.GCS, hedgeCfg)
   104  	case "s3":
   105  		client, err = aws.NewS3ObjectClient(cfg.S3, hedgeCfg)
   106  	case "bos":
   107  		client, err = baidubce.NewBOSObjectStorage(&cfg.BOS)
   108  	case "swift":
   109  		client, err = openstack.NewSwiftObjectClient(cfg.Swift, hedgeCfg)
   110  	case "local":
   111  		return local.NewLocalRulesClient(cfg.Local, loader)
   112  	default:
   113  		return nil, fmt.Errorf("unrecognized rule storage mode %v, choose one of: configdb, gcs, s3, swift, azure, local", cfg.Type)
   114  	}
   115  
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return objectclient.NewRuleStore(client, loadRulesConcurrency, logger), nil
   121  }
   122  
   123  // NewRuleStore returns a rule store backend client based on the provided cfg.
   124  func NewRuleStore(ctx context.Context, cfg rulestore.Config, cfgProvider bucket.TenantConfigProvider, loader promRules.GroupLoader, logger log.Logger, reg prometheus.Registerer) (rulestore.RuleStore, error) {
   125  	if cfg.Backend == configdb.Name {
   126  		c, err := configClient.New(cfg.ConfigDB)
   127  		if err != nil {
   128  			return nil, err
   129  		}
   130  
   131  		return configdb.NewConfigRuleStore(c), nil
   132  	}
   133  
   134  	if cfg.Backend == local.Name {
   135  		return local.NewLocalRulesClient(cfg.Local, loader)
   136  	}
   137  
   138  	bucketClient, err := bucket.NewClient(ctx, cfg.Config, "ruler-storage", logger, reg)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	store := bucketclient.NewBucketRuleStore(bucketClient, cfgProvider, logger)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	return store, nil
   149  }