From efa77e7c446b526a4ce6f187c8569adb03385035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Tue, 13 Aug 2024 20:46:09 +0000 Subject: [PATCH] mssql: fix nil panic, if YAML file is used a configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- pkg/collector/dfsr/dfsr.go | 11 +++++++-- pkg/collector/exchange/exchange.go | 9 +++++++- pkg/collector/mssql/mssql.go | 36 +++++++++++++++++------------- pkg/collector/textfile/textfile.go | 11 +++++++-- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/pkg/collector/dfsr/dfsr.go b/pkg/collector/dfsr/dfsr.go index df7af73e3..a65c93778 100644 --- a/pkg/collector/dfsr/dfsr.go +++ b/pkg/collector/dfsr/dfsr.go @@ -120,10 +120,17 @@ func NewWithFlags(app *kingpin.Application) *Collector { c := &Collector{ config: ConfigDefaults, } - c.config.CollectorsEnabled = make([]string, 0) + + var collectorsEnabled string app.Flag("collectors.dfsr.sources-enabled", "Comma-separated list of DFSR Perflib sources to use."). - Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringsVar(&c.config.CollectorsEnabled) + Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringVar(&collectorsEnabled) + + app.Action(func(*kingpin.ParseContext) error { + c.config.CollectorsEnabled = strings.Split(collectorsEnabled, ",") + + return nil + }) return c } diff --git a/pkg/collector/exchange/exchange.go b/pkg/collector/exchange/exchange.go index 01330bad0..de2462985 100644 --- a/pkg/collector/exchange/exchange.go +++ b/pkg/collector/exchange/exchange.go @@ -111,6 +111,7 @@ func NewWithFlags(app *kingpin.Application) *Collector { c.config.CollectorsEnabled = make([]string, 0) var listAllCollectors bool + var collectorsEnabled string app.Flag( "collectors.exchange.list", @@ -120,7 +121,7 @@ func NewWithFlags(app *kingpin.Application) *Collector { app.Flag( "collectors.exchange.enabled", "Comma-separated list of collectors to use. Defaults to all, if not specified.", - ).Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringsVar(&c.config.CollectorsEnabled) + ).Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringVar(&collectorsEnabled) app.PreAction(func(*kingpin.ParseContext) error { if listAllCollectors { @@ -152,6 +153,12 @@ func NewWithFlags(app *kingpin.Application) *Collector { return nil }) + app.Action(func(*kingpin.ParseContext) error { + c.config.CollectorsEnabled = strings.Split(collectorsEnabled, ",") + + return nil + }) + return c } diff --git a/pkg/collector/mssql/mssql.go b/pkg/collector/mssql/mssql.go index 8e5c71462..027e87673 100644 --- a/pkg/collector/mssql/mssql.go +++ b/pkg/collector/mssql/mssql.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "os" - "slices" "sort" "strings" "sync" @@ -437,9 +436,9 @@ func NewWithFlags(app *kingpin.Application) *Collector { c := &Collector{ config: ConfigDefaults, } - c.config.CollectorsEnabled = make([]string, 0) var listAllCollectors bool + var collectorsEnabled string app.Flag( "collectors.mssql.class-print", @@ -449,7 +448,7 @@ func NewWithFlags(app *kingpin.Application) *Collector { app.Flag( "collectors.mssql.classes-enabled", "Comma-separated list of mssql WMI classes to use.", - ).Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringsVar(&c.config.CollectorsEnabled) + ).Default(strings.Join(c.config.CollectorsEnabled, ",")).StringVar(&collectorsEnabled) app.PreAction(func(*kingpin.ParseContext) error { if listAllCollectors { @@ -468,6 +467,12 @@ func NewWithFlags(app *kingpin.Application) *Collector { return nil }) + app.Action(func(*kingpin.ParseContext) error { + c.config.CollectorsEnabled = strings.Split(collectorsEnabled, ",") + + return nil + }) + return c } @@ -480,16 +485,11 @@ func (c *Collector) SetLogger(logger log.Logger) { } func (c *Collector) GetPerfCounter() ([]string, error) { - enabled := slices.Compact(c.config.CollectorsEnabled) - - // Result must order, to prevent test failures. - sort.Strings(enabled) - c.mssqlInstances = getMSSQLInstances(c.logger) - perfCounters := make([]string, 0, len(c.mssqlInstances)*len(enabled)) + perfCounters := make([]string, 0, len(c.mssqlInstances)*len(c.config.CollectorsEnabled)) for instance := range c.mssqlInstances { - for _, c := range enabled { + for _, c := range c.config.CollectorsEnabled { perfCounters = append(perfCounters, mssqlGetPerfObjectName(instance, c)) } } @@ -502,6 +502,9 @@ func (c *Collector) Close() error { } func (c *Collector) Build() error { + // Result must order, to prevent test failures. + sort.Strings(c.config.CollectorsEnabled) + // meta c.mssqlScrapeDurationDesc = prometheus.NewDesc( prometheus.BuildFQName(types.Namespace, Name, "collector_duration_seconds"), @@ -1965,6 +1968,12 @@ func (c *Collector) Build() error { c.mssqlCollectors = c.getMSSQLCollectors() + for _, name := range c.config.CollectorsEnabled { + if _, ok := c.mssqlCollectors[name]; !ok { + return errors.New("unknown mssql collector: " + name) + } + } + return nil } @@ -2007,13 +2016,8 @@ func (c *Collector) execute(ctx *types.ScrapeContext, name string, fn mssqlColle func (c *Collector) Collect(ctx *types.ScrapeContext, ch chan<- prometheus.Metric) error { wg := sync.WaitGroup{} - enabled := slices.Compact(c.config.CollectorsEnabled) - - // Result must order, to prevent test failures. - sort.Strings(enabled) - for sqlInstance := range c.mssqlInstances { - for _, name := range enabled { + for _, name := range c.config.CollectorsEnabled { function := c.mssqlCollectors[name] wg.Add(1) diff --git a/pkg/collector/textfile/textfile.go b/pkg/collector/textfile/textfile.go index 411f50d34..e625084cc 100644 --- a/pkg/collector/textfile/textfile.go +++ b/pkg/collector/textfile/textfile.go @@ -78,12 +78,19 @@ func NewWithFlags(app *kingpin.Application) *Collector { c := &Collector{ config: ConfigDefaults, } - c.config.TextFileDirectories = make([]string, 0) + + var textFileDirectories string app.Flag( "collector.textfile.directories", "Directory or Directories to read text files with metrics from.", - ).Default(strings.Join(ConfigDefaults.TextFileDirectories, ",")).StringsVar(&c.config.TextFileDirectories) + ).Default(strings.Join(ConfigDefaults.TextFileDirectories, ",")).StringVar(&textFileDirectories) + + app.Action(func(*kingpin.ParseContext) error { + c.config.TextFileDirectories = strings.Split(textFileDirectories, ",") + + return nil + }) return c }