Skip to content

Commit

Permalink
mssql: fix nil panic, if YAML file is used a configuration (prometheu…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke authored Aug 14, 2024
1 parent 4505859 commit aa1132c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
11 changes: 9 additions & 2 deletions pkg/collector/dfsr/dfsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/collector/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
36 changes: 20 additions & 16 deletions pkg/collector/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"os"
"slices"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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))
}
}
Expand All @@ -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"),
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions pkg/collector/textfile/textfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit aa1132c

Please sign in to comment.