chore: Move private packages to internal (#1664)

This commit is contained in:
Jan-Otto Kröpke
2024-10-03 20:23:56 +02:00
committed by GitHub
parent bcfe6df24d
commit 5d95610c84
181 changed files with 1680 additions and 1867 deletions

View File

@@ -0,0 +1,37 @@
//go:build windows
package utils
import (
"os"
"strings"
"github.com/prometheus-community/windows_exporter/internal/types"
)
func ExpandEnabledCollectors(enabled string) []string {
expanded := strings.ReplaceAll(enabled, types.DefaultCollectorsPlaceholder, types.DefaultCollectors)
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 PDHEnabled() bool {
if v, ok := os.LookupEnv("WINDOWS_EXPORTER_PERF_COUNTERS_ENGINE"); ok && v == "pdh" {
return true
}
return false
}

View File

@@ -0,0 +1,55 @@
package utils_test
import (
"sort"
"strings"
"testing"
"github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus-community/windows_exporter/internal/utils"
)
func TestExpandEnabled(t *testing.T) {
t.Parallel()
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)
}
}
}

19
internal/utils/utils.go Normal file
View File

@@ -0,0 +1,19 @@
//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
}
func ToPTR[t any](v t) *t {
return &v
}