forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
89 lines (79 loc) · 3 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package common
import (
"errors"
"fmt"
"strings"
"github.com/DataDog/viper"
"github.com/StackVista/stackstate-agent/pkg/autodiscovery/integration"
"github.com/StackVista/stackstate-agent/pkg/config"
)
// SetupConfig fires up the configuration system
func SetupConfig(confFilePath string) error {
_, err := SetupConfigWithWarnings(confFilePath, "")
return err
}
// SetupConfigWithWarnings fires up the configuration system and returns warnings if any.
func SetupConfigWithWarnings(confFilePath, configName string) (*config.Warnings, error) {
return setupConfig(confFilePath, configName, false, true)
}
// SetupConfigWithoutSecrets fires up the configuration system without secrets support
func SetupConfigWithoutSecrets(confFilePath string, configName string) error {
_, err := setupConfig(confFilePath, configName, true, true)
return err
}
// SetupConfigIfExist fires up the configuration system but
// doesn't raise an error if the configuration file is the default one
// and it doesn't exist.
func SetupConfigIfExist(confFilePath string) error {
_, err := setupConfig(confFilePath, "", false, false)
return err
}
func setupConfig(confFilePath string, configName string, withoutSecrets bool, failOnMissingFile bool) (*config.Warnings, error) {
if configName != "" {
config.Datadog.SetConfigName(configName)
}
// set the paths where a config file is expected
if len(confFilePath) != 0 {
// if the configuration file path was supplied on the command line,
// add that first so it's first in line
config.Datadog.AddConfigPath(confFilePath)
// If they set a config file directly, let's try to honor that
if strings.HasSuffix(confFilePath, ".yaml") {
config.Datadog.SetConfigFile(confFilePath)
}
}
config.Datadog.AddConfigPath(DefaultConfPath)
// load the configuration
var err error
var warnings *config.Warnings
if withoutSecrets {
warnings, err = config.LoadWithoutSecret()
} else {
warnings, err = config.Load()
}
// If `!failOnMissingFile`, do not issue an error if we cannot find the default config file.
var e viper.ConfigFileNotFoundError
if err != nil && (failOnMissingFile || !errors.As(err, &e) || confFilePath != "") {
return warnings, fmt.Errorf("unable to load Datadog config file: %w", err)
}
return warnings, nil
}
// SelectedCheckMatcherBuilder returns a function that returns true if the number of configs found for the
// check name is more or equal to min instances
func SelectedCheckMatcherBuilder(checkNames []string, minInstances uint) func(configs []integration.Config) bool {
return func(configs []integration.Config) bool {
var matchedConfigsCount uint
for _, cfg := range configs {
for _, name := range checkNames {
if cfg.Name == name {
matchedConfigsCount++
}
}
}
return matchedConfigsCount >= minInstances
}
}