Merge mssql and dfsr expandEnabledCollectors func

Move to common collector.go and add function test.

Signed-off-by: Ben Reedy <breed808@breed808.com>
This commit is contained in:
Ben Reedy
2020-12-03 20:20:28 +10:00
parent ccac306c2d
commit b5ce53fdac
4 changed files with 57 additions and 35 deletions

View File

@@ -2,6 +2,7 @@ package collector
import ( import (
"fmt" "fmt"
"sort"
"strconv" "strconv"
"strings" "strings"
@@ -128,3 +129,22 @@ func find(slice []string, val string) bool {
} }
return false return false
} }
// Used by more complex collectors where user input specifies enabled child collectors.
// Splits provided child collectors and deduplicate.
func expandEnabledChildCollectors(enabled string) []string {
separated := strings.Split(enabled, ",")
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)
}
// Ensure result is ordered, to prevent test failure
sort.Strings(result)
return result
}

View File

@@ -0,0 +1,34 @@
package collector
import (
"reflect"
"testing"
)
func TestExpandChildCollectors(t *testing.T) {
cases := []struct {
name string
input string
expectedOutput []string
}{
{
name: "simple",
input: "testing1,testing2,testing3",
expectedOutput: []string{"testing1", "testing2", "testing3"},
},
{
name: "duplicate",
input: "testing1,testing2,testing2,testing3",
expectedOutput: []string{"testing1", "testing2", "testing3"},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
output := expandEnabledChildCollectors(c.input)
if !reflect.DeepEqual(output, c.expectedOutput) {
t.Errorf("Output mismatch, expected %+v, got %+v", c.expectedOutput, output)
}
})
}
}

View File

@@ -4,7 +4,6 @@ package collector
import ( import (
"errors" "errors"
"strings"
"sync" "sync"
"time" "time"
@@ -88,22 +87,6 @@ type dfsrCollectorMap map[string]dfsrCollectorFunc
type dfsrCollectorFunc func(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) type dfsrCollectorFunc func(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error)
// Split provided perflib sources and deduplicate
func dfsrExpandEnabledSources(enabled string) []string {
separated := strings.Split(enabled, ",")
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
}
// Map Perflib sources to DFSR collector names // Map Perflib sources to DFSR collector names
// E.G. volume -> DFS Replication Service Volumes // E.G. volume -> DFS Replication Service Volumes
func dfsrGetPerfObjectName(collector string) string { func dfsrGetPerfObjectName(collector string) string {
@@ -124,7 +107,7 @@ func dfsrGetPerfObjectName(collector string) string {
func NewDFSRCollector() (Collector, error) { func NewDFSRCollector() (Collector, error) {
const subsystem = "dfsr" const subsystem = "dfsr"
enabled := dfsrExpandEnabledSources(*dfsrEnabledCollectors) enabled := expandEnabledChildCollectors(*dfsrEnabledCollectors)
perfCounters := make([]string, 0, len(enabled)) perfCounters := make([]string, 0, len(enabled))
for _, c := range enabled { for _, c := range enabled {
perfCounters = append(perfCounters, dfsrGetPerfObjectName(c)) perfCounters = append(perfCounters, dfsrGetPerfObjectName(c))

View File

@@ -90,21 +90,6 @@ func (c *MSSQLCollector) getMSSQLCollectors() mssqlCollectorsMap {
return mssqlCollectors return mssqlCollectors
} }
func mssqlExpandEnabledCollectors(enabled string) []string {
separated := strings.Split(enabled, ",")
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
}
// mssqlGetPerfObjectName - Returns the name of the Windows Performance // mssqlGetPerfObjectName - Returns the name of the Windows Performance
// Counter object for the given SQL instance and collector. // Counter object for the given SQL instance and collector.
func mssqlGetPerfObjectName(sqlInstance string, collector string) string { func mssqlGetPerfObjectName(sqlInstance string, collector string) string {
@@ -407,7 +392,7 @@ func NewMSSQLCollector() (Collector, error) {
const subsystem = "mssql" const subsystem = "mssql"
enabled := mssqlExpandEnabledCollectors(*mssqlEnabledCollectors) enabled := expandEnabledChildCollectors(*mssqlEnabledCollectors)
mssqlInstances := getMSSQLInstances() mssqlInstances := getMSSQLInstances()
perfCounters := make([]string, 0, len(mssqlInstances)*len(enabled)) perfCounters := make([]string, 0, len(mssqlInstances)*len(enabled))
for instance := range mssqlInstances { for instance := range mssqlInstances {
@@ -1857,7 +1842,7 @@ func (c *MSSQLCollector) execute(ctx *ScrapeContext, name string, fn mssqlCollec
func (c *MSSQLCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error { func (c *MSSQLCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
enabled := mssqlExpandEnabledCollectors(*mssqlEnabledCollectors) enabled := expandEnabledChildCollectors(*mssqlEnabledCollectors)
for sqlInstance := range c.mssqlInstances { for sqlInstance := range c.mssqlInstances {
for _, name := range enabled { for _, name := range enabled {
function := c.mssqlCollectors[name] function := c.mssqlCollectors[name]