From e9789f7f99ff01c352b7f0e4ff6e97946a67b561 Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Wed, 15 Apr 2020 08:11:42 -0400 Subject: [PATCH] adds config validation, similar to cortex (#1939) --- cmd/loki/main.go | 8 ++++++++ pkg/loki/loki.go | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/cmd/loki/main.go b/cmd/loki/main.go index 55a72080679e3..eab42aed36ae3 100644 --- a/cmd/loki/main.go +++ b/cmd/loki/main.go @@ -50,6 +50,14 @@ func main() { } util.InitLogger(&config.Server) + // Validate the config once both the config file has been loaded + // and CLI flags parsed. + err := config.Validate(util.Logger) + if err != nil { + level.Error(util.Logger).Log("msg", "validating config", "err", err.Error()) + os.Exit(1) + } + // Setting the environment variable JAEGER_AGENT_HOST enables tracing trace, err := tracing.NewFromEnv(fmt.Sprintf("loki-%s", config.Target)) if err != nil { diff --git a/pkg/loki/loki.go b/pkg/loki/loki.go index 6b14a3e50f20f..e7ce6b36063b5 100644 --- a/pkg/loki/loki.go +++ b/pkg/loki/loki.go @@ -11,6 +11,7 @@ import ( "github.com/cortexproject/cortex/pkg/util" "github.com/cortexproject/cortex/pkg/util/runtimeconfig" + "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/weaveworks/common/middleware" @@ -73,6 +74,24 @@ func (c *Config) RegisterFlags(f *flag.FlagSet) { c.RuntimeConfig.RegisterFlags(f) } +// Validate the config and returns an error if the validation +// doesn't pass +func (c *Config) Validate(log log.Logger) error { + if err := c.SchemaConfig.Validate(); err != nil { + return errors.Wrap(err, "invalid schema config") + } + if err := c.StorageConfig.Validate(); err != nil { + return errors.Wrap(err, "invalid storage config") + } + if err := c.QueryRange.Validate(log); err != nil { + return errors.Wrap(err, "invalid queryrange config") + } + if err := c.TableManager.Validate(); err != nil { + return errors.Wrap(err, "invalid tablemanager config") + } + return nil +} + // Loki is the root datastructure for Loki. type Loki struct { cfg Config