mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-07 21:46:37 +00:00
config: fix lists (#2124)
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
collectors:
|
||||
enabled: cpu,cpu_info,exchange,iis,logical_disk,memory,net,os,performancecounter,process,remote_fx,service,system,tcp,time,terminal_services,textfile
|
||||
collector:
|
||||
textfile:
|
||||
directories:
|
||||
- 'C:\MyDir1'
|
||||
- 'C:\MyDir2'
|
||||
service:
|
||||
include: "windows_exporter"
|
||||
performancecounter:
|
||||
|
||||
@@ -73,19 +73,10 @@ func NewWithFlags(app *kingpin.Application) *Collector {
|
||||
}
|
||||
c.config.FilePatterns = make([]string, 0)
|
||||
|
||||
var filePatterns string
|
||||
|
||||
app.Flag(
|
||||
"collector.filetime.file-patterns",
|
||||
"Comma-separated list of file patterns. Each pattern is a glob pattern that can contain `*`, `?`, and `**` (recursive). See https://github.com/bmatcuk/doublestar#patterns",
|
||||
).Default(strings.Join(ConfigDefaults.FilePatterns, ",")).StringVar(&filePatterns)
|
||||
|
||||
app.Action(func(*kingpin.ParseContext) error {
|
||||
// doublestar.Glob() requires forward slashes
|
||||
c.config.FilePatterns = strings.Split(filepath.ToSlash(filePatterns), ",")
|
||||
|
||||
return nil
|
||||
})
|
||||
).Default(strings.Join(ConfigDefaults.FilePatterns, ",")).StringsVar(&c.config.FilePatterns)
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -19,72 +19,52 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// flatten flattens the nested struct.
|
||||
//
|
||||
// All keys will be joined by dot
|
||||
// e.g. {"a": {"b":"c"}} => {"a.b":"c"}
|
||||
// or {"a": {"b":[1,2]}} => {"a.b.0":1, "a.b.1": 2}.
|
||||
func flatten(data map[string]interface{}) map[string]string {
|
||||
ret := make(map[string]string)
|
||||
|
||||
for k, v := range data {
|
||||
switch typed := v.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
for fk, fv := range flatten(convertMap(typed)) {
|
||||
ret[fmt.Sprintf("%s.%s", k, fk)] = fv
|
||||
}
|
||||
case map[string]interface{}:
|
||||
for fk, fv := range flatten(typed) {
|
||||
ret[fmt.Sprintf("%s.%s", k, fk)] = fv
|
||||
}
|
||||
case []interface{}:
|
||||
for fk, fv := range flattenSlice(typed) {
|
||||
ret[fmt.Sprintf("%s.%s", k, fk)] = fv
|
||||
}
|
||||
default:
|
||||
ret[k] = fmt.Sprint(typed)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func flattenSlice(data []interface{}) map[string]string {
|
||||
ret := make(map[string]string)
|
||||
|
||||
for idx, v := range data {
|
||||
switch typed := v.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
for fk, fv := range flatten(convertMap(typed)) {
|
||||
ret[fmt.Sprintf("%d,%s", idx, fk)] = fv
|
||||
}
|
||||
case map[string]interface{}:
|
||||
for fk, fv := range flatten(typed) {
|
||||
ret[fmt.Sprintf("%d,%s", idx, fk)] = fv
|
||||
}
|
||||
case []interface{}:
|
||||
for fk, fv := range flattenSlice(typed) {
|
||||
ret[fmt.Sprintf("%d,%s", idx, fk)] = fv
|
||||
}
|
||||
default:
|
||||
ret[strconv.Itoa(idx)] = fmt.Sprint(typed)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func convertMap(originalMap map[interface{}]interface{}) map[string]interface{} {
|
||||
convertedMap := map[string]interface{}{}
|
||||
|
||||
// convertMap converts a map with any comparable key type to a map with string keys.
|
||||
func convertMap[K comparable, V any](originalMap map[K]V) map[string]V {
|
||||
convertedMap := make(map[string]V, len(originalMap))
|
||||
for key, value := range originalMap {
|
||||
if keyString, ok := key.(string); ok {
|
||||
if keyString, ok := any(key).(string); ok {
|
||||
convertedMap[keyString] = value
|
||||
}
|
||||
}
|
||||
|
||||
return convertedMap
|
||||
}
|
||||
|
||||
// flatten flattens a nested map, joining keys with dots.
|
||||
// e.g. {"a": {"b":"c"}} => {"a.b":"c"}
|
||||
func flatten(data map[string]any) map[string]string {
|
||||
result := make(map[string]string)
|
||||
|
||||
flattenHelper("", data, result)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func flattenHelper(prefix string, data map[string]any, result map[string]string) {
|
||||
for k, v := range data {
|
||||
fullKey := k
|
||||
if prefix != "" {
|
||||
fullKey = prefix + "." + k
|
||||
}
|
||||
|
||||
switch val := v.(type) {
|
||||
case map[any]any:
|
||||
flattenHelper(fullKey, convertMap(val), result)
|
||||
case map[string]any:
|
||||
flattenHelper(fullKey, val, result)
|
||||
case []any:
|
||||
strSlice := make([]string, len(val))
|
||||
for i, elem := range val {
|
||||
strSlice[i] = fmt.Sprint(elem)
|
||||
}
|
||||
|
||||
result[fullKey] = strings.Join(strSlice, ",")
|
||||
default:
|
||||
result[fullKey] = fmt.Sprint(val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user