mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-13 16:36:37 +00:00
36
pkg/utils/collector.go
Normal file
36
pkg/utils/collector.go
Normal file
@@ -0,0 +1,36 @@
|
||||
//go:build windows
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/pkg/types"
|
||||
)
|
||||
|
||||
// ExpandEnabledChildCollectors used by more complex Collectors where user input specifies enabled child Collectors.
|
||||
// Splits provided child Collectors and deduplicate.
|
||||
func ExpandEnabledChildCollectors(enabled string) []string {
|
||||
result := slices.Compact(strings.Split(enabled, ","))
|
||||
// Ensure result is ordered, to prevent test failure
|
||||
sort.Strings(result)
|
||||
return result
|
||||
}
|
||||
|
||||
func ExpandEnabledCollectors(enabled string) []string {
|
||||
expanded := strings.Replace(enabled, types.DefaultCollectorsPlaceholder, types.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
|
||||
}
|
||||
79
pkg/utils/collector_test.go
Normal file
79
pkg/utils/collector_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/pkg/types"
|
||||
"github.com/prometheus-community/windows_exporter/pkg/utils"
|
||||
)
|
||||
|
||||
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 := utils.ExpandEnabledChildCollectors(c.input)
|
||||
if !reflect.DeepEqual(output, c.expectedOutput) {
|
||||
t.Errorf("Output mismatch, expected %+v, got %+v", c.expectedOutput, output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandEnabled(t *testing.T) {
|
||||
expansionTests := []struct {
|
||||
input string
|
||||
expectedOutput []string
|
||||
}{
|
||||
{"", []string{}},
|
||||
// Default case
|
||||
{"cs,os", []string{"cs", "os"}},
|
||||
// Placeholder expansion
|
||||
{types.DefaultCollectorsPlaceholder, strings.Split(types.DefaultCollectors, ",")},
|
||||
// De-duplication
|
||||
{"cs,cs", []string{"cs"}},
|
||||
// De-duplicate placeholder
|
||||
{types.DefaultCollectorsPlaceholder + "," + types.DefaultCollectorsPlaceholder, strings.Split(types.DefaultCollectors, ",")},
|
||||
// Composite case
|
||||
{"foo," + types.DefaultCollectorsPlaceholder + ",bar", append(strings.Split(types.DefaultCollectors, ","), "foo", "bar")},
|
||||
}
|
||||
|
||||
for _, testCase := range expansionTests {
|
||||
output := utils.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
14
pkg/utils/utils.go
Normal file
14
pkg/utils/utils.go
Normal file
@@ -0,0 +1,14 @@
|
||||
//go:build windows
|
||||
|
||||
package utils
|
||||
|
||||
func MilliSecToSec(t float64) float64 {
|
||||
return t / 1000
|
||||
}
|
||||
|
||||
func BoolToFloat(b bool) float64 {
|
||||
if b {
|
||||
return 1.0
|
||||
}
|
||||
return 0.0
|
||||
}
|
||||
Reference in New Issue
Block a user