Skip to content

Commit

Permalink
Add [defaults] placeholder support to collectors.enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
carlpett committed Mar 4, 2017
1 parent a05febe commit a66f0b5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
27 changes: 23 additions & 4 deletions exporter.go
Expand Up @@ -25,8 +25,9 @@ type WmiCollector struct {
}

const (
defaultCollectors = "cpu,cs,logical_disk,net,os,service,system"
serviceName = "wmi_exporter"
defaultCollectors = "cpu,cs,logical_disk,net,os,service,system"
defaultCollectorsPlaceholder = "[defaults]"
serviceName = "wmi_exporter"
)

var (
Expand Down Expand Up @@ -90,9 +91,27 @@ func execute(name string, c collector.Collector, ch chan<- prometheus.Metric) {
scrapeDurations.WithLabelValues(name, result).Observe(duration.Seconds())
}

func expandEnabledCollectors(enabled string) []string {
expanded := strings.Replace(enabled, defaultCollectorsPlaceholder, defaultCollectors, -1)
separated := strings.Split(expanded, ",")
unique := map[string]bool{}
for _, s := range separated {
if s != "" {
unique[s] = true
}
}
result := make([]string, 0, len(unique))
for s, _ := range unique {
result = append(result, s)
}
return result
}

func loadCollectors(list string) (map[string]collector.Collector, error) {
collectors := map[string]collector.Collector{}
for _, name := range strings.Split(list, ",") {
enabled := expandEnabledCollectors(list)

for _, name := range enabled {
fn, ok := collector.Factories[name]
if !ok {
return nil, fmt.Errorf("collector '%s' not available", name)
Expand All @@ -115,7 +134,7 @@ func main() {
showVersion = flag.Bool("version", false, "Print version information.")
listenAddress = flag.String("telemetry.addr", ":9182", "host:port for WMI exporter.")
metricsPath = flag.String("telemetry.path", "/metrics", "URL path for surfacing collected metrics.")
enabledCollectors = flag.String("collectors.enabled", filterAvailableCollectors(defaultCollectors), "Comma-separated list of collectors to use.")
enabledCollectors = flag.String("collectors.enabled", filterAvailableCollectors(defaultCollectors), "Comma-separated list of collectors to use. Use '[default]' as a placeholder for all the collectors enabled by default")
printCollectors = flag.Bool("collectors.print", false, "If true, print available collectors and exit.")
)
flag.Parse()
Expand Down
49 changes: 49 additions & 0 deletions exporter_test.go
@@ -0,0 +1,49 @@
package main

import (
"sort"
"strings"
"testing"
)

type expansionTestCase struct {
input string
expectedOutput []string
}

func TestExpandEnabled(t *testing.T) {
expansionTests := []expansionTestCase{
{"", []string{}},
// Default case
{"cs,os", []string{"cs", "os"}},
// Placeholder expansion
{defaultCollectorsPlaceholder, strings.Split(defaultCollectors, ",")},
// De-duplication
{"cs,cs", []string{"cs"}},
// De-duplicate placeholder
{defaultCollectorsPlaceholder + "," + defaultCollectorsPlaceholder, strings.Split(defaultCollectors, ",")},
// Composite case
{"foo," + defaultCollectorsPlaceholder + ",bar", append(strings.Split(defaultCollectors, ","), "foo", "bar")},
}

for _, testCase := range expansionTests {
output := expandEnabledCollectors(testCase.input)
sort.Strings(output)

success := true
if len(output) != len(testCase.expectedOutput) {
success = false
} else {
sort.Strings(testCase.expectedOutput)
for idx := range output {
if output[idx] != testCase.expectedOutput[idx] {
success = false
break
}
}
}
if !success {
t.Error("For", testCase.input, "expected", testCase.expectedOutput, "got", output)
}
}
}

0 comments on commit a66f0b5

Please sign in to comment.