mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-21 20:26:36 +00:00
111
pkg/config/config.go
Normal file
111
pkg/config/config.go
Normal file
@@ -0,0 +1,111 @@
|
||||
// Copyright 2018 Prometheus Team
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/kingpin/v2"
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type getFlagger interface {
|
||||
GetFlag(name string) *kingpin.FlagClause
|
||||
}
|
||||
|
||||
// Resolver represents a configuration file resolver for kingpin.
|
||||
type Resolver struct {
|
||||
flags map[string]string
|
||||
}
|
||||
|
||||
// NewResolver returns a Resolver structure.
|
||||
func NewResolver(file string, logger log.Logger, insecure_skip_verify bool) (*Resolver, error) {
|
||||
flags := map[string]string{}
|
||||
var fileBytes []byte
|
||||
var err error
|
||||
if strings.HasPrefix(file, "http://") || strings.HasPrefix(file, "https://") {
|
||||
_ = level.Info(logger).Log("msg", fmt.Sprintf("Loading configuration file from URL: %v", file))
|
||||
tr := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure_skip_verify},
|
||||
}
|
||||
if insecure_skip_verify {
|
||||
_ = level.Warn(logger).Log("msg", "Loading configuration file with TLS verification disabled")
|
||||
}
|
||||
client := &http.Client{Transport: tr}
|
||||
resp, err := client.Get(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
fileBytes, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
_ = level.Info(logger).Log("msg", fmt.Sprintf("Loading configuration file: %v", file))
|
||||
if _, err := os.Stat(file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fileBytes, err = os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var rawValues map[string]interface{}
|
||||
err = yaml.Unmarshal(fileBytes, &rawValues)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Flatten nested YAML values
|
||||
flattenedValues := flatten(rawValues)
|
||||
for k, v := range flattenedValues {
|
||||
if _, ok := flags[k]; !ok {
|
||||
flags[k] = v
|
||||
}
|
||||
}
|
||||
return &Resolver{flags: flags}, nil
|
||||
}
|
||||
|
||||
func (c *Resolver) setDefault(v getFlagger) {
|
||||
for name, value := range c.flags {
|
||||
f := v.GetFlag(name)
|
||||
if f != nil {
|
||||
f.Default(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bind sets active flags with their default values from the configuration file(s).
|
||||
func (c *Resolver) Bind(app *kingpin.Application, args []string) error {
|
||||
// Parse the command line arguments to get the selected command.
|
||||
pc, err := app.ParseContext(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.setDefault(app)
|
||||
if pc.SelectedCommand != nil {
|
||||
c.setDefault(pc.SelectedCommand)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
61
pkg/config/flatten.go
Normal file
61
pkg/config/flatten.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 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[fmt.Sprint(idx)] = fmt.Sprint(typed)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func convertMap(originalMap map[interface{}]interface{}) map[string]interface{} {
|
||||
convertedMap := map[string]interface{}{}
|
||||
for key, value := range originalMap {
|
||||
convertedMap[key.(string)] = value
|
||||
}
|
||||
return convertedMap
|
||||
}
|
||||
34
pkg/config/flatten_test.go
Normal file
34
pkg/config/flatten_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Unmarshal good configuration file and confirm data is flattened correctly
|
||||
func TestConfigFlattening(t *testing.T) {
|
||||
goodYamlConfig := []byte(`---
|
||||
|
||||
collectors:
|
||||
enabled: cpu,net,service
|
||||
|
||||
log:
|
||||
level: debug`)
|
||||
var data map[string]interface{}
|
||||
err := yaml.Unmarshal(goodYamlConfig, &data)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
expectedResult := map[string]string{
|
||||
"collectors.enabled": "cpu,net,service",
|
||||
"log.level": "debug",
|
||||
}
|
||||
flattenedValues := flatten(data)
|
||||
|
||||
if !reflect.DeepEqual(expectedResult, flattenedValues) {
|
||||
t.Errorf("Flattened values do not match!\nExpected result: %s\nActual result: %s", expectedResult, flattenedValues)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user