-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathconfig.go
115 lines (101 loc) · 3.64 KB
/
config.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package config
import (
"strings"
"time"
log "github.com/cihub/seelog"
"github.com/spf13/viper"
)
// Datadog is the global configuration object
var Datadog = viper.New()
// MetadataProviders helps unmarshalling `metadata_providers` config param
type MetadataProviders struct {
Name string `mapstructure:"name"`
Interval time.Duration `mapstructure:"interval"`
}
func init() {
// config identifiers
Datadog.SetConfigName("datadog")
Datadog.SetEnvPrefix("DD")
// Configuration defaults
// Agent
Datadog.SetDefault("dd_url", "http://localhost:17123")
Datadog.SetDefault("proxy", "")
Datadog.SetDefault("skip_ssl_validation", false)
Datadog.SetDefault("hostname", "")
Datadog.SetDefault("conf_path", ".")
Datadog.SetDefault("confd_path", defaultConfdPath)
Datadog.SetDefault("additional_checksd", defaultAdditionalChecksPath)
Datadog.SetDefault("log_file", defaultLogPath)
Datadog.SetDefault("log_level", "info")
Datadog.SetDefault("cmd_sock", "/tmp/agent.sock")
// BUG(massi): make the listener_windows.go module actually use the following:
Datadog.SetDefault("cmd_pipe_name", `\\.\pipe\ddagent`)
Datadog.SetDefault("check_runners", int64(4))
Datadog.SetDefault("forwarder_timeout", 20)
// Dogstatsd
Datadog.SetDefault("use_dogstatsd", true)
Datadog.SetDefault("dogstatsd_port", 8125)
Datadog.SetDefault("dogstatsd_buffer_size", 1024*8) // 8KB buffer
Datadog.SetDefault("dogstatsd_non_local_traffic", false)
Datadog.SetDefault("dogstatsd_socket", "") // Notice: empty means feature disabled
Datadog.SetDefault("dogstatsd_stats_enable", false)
Datadog.SetDefault("dogstatsd_stats_buffer", 10)
// JMX
Datadog.SetDefault("jmx_pipe_path", defaultJMXPipePath)
Datadog.SetDefault("jmx_pipe_name", "dd-auto_discovery")
// Autoconfig
Datadog.SetDefault("autoconf_template_dir", "/datadog/check_configs")
// ENV vars bindings
Datadog.BindEnv("api_key")
Datadog.BindEnv("dd_url")
Datadog.BindEnv("cmd_sock")
Datadog.BindEnv("conf_path")
Datadog.BindEnv("dogstatsd_socket")
Datadog.BindEnv("dogstatsd_non_local_traffic")
}
// GetMultipleEndpoints returns the api keys per domain specified in the main agent config
func GetMultipleEndpoints() (map[string][]string, error) {
return getMultipleEndpoints(Datadog)
}
// getMultipleEndpoints implements the logic to extract the api keys per domain from an agent config
func getMultipleEndpoints(config *viper.Viper) (map[string][]string, error) {
keysPerDomain := map[string][]string{
config.GetString("dd_url"): {
config.GetString("api_key"),
},
}
var additionalEndpoints map[string][]string
err := config.UnmarshalKey("additional_endpoints", &additionalEndpoints)
if err != nil {
return keysPerDomain, err
}
// merge additional endpoints into keysPerDomain
for domain, apiKeys := range additionalEndpoints {
if _, ok := keysPerDomain[domain]; ok {
for _, apiKey := range apiKeys {
keysPerDomain[domain] = append(keysPerDomain[domain], apiKey)
}
} else {
keysPerDomain[domain] = apiKeys
}
}
// dedupe api keys and remove domains with no api keys (or empty ones)
for domain, apiKeys := range keysPerDomain {
dedupedAPIKeys := make([]string, 0, len(apiKeys))
seen := make(map[string]bool)
for _, apiKey := range apiKeys {
trimmedAPIKey := strings.TrimSpace(apiKey)
if _, ok := seen[trimmedAPIKey]; !ok && trimmedAPIKey != "" {
seen[trimmedAPIKey] = true
dedupedAPIKeys = append(dedupedAPIKeys, trimmedAPIKey)
}
}
if len(dedupedAPIKeys) > 0 {
keysPerDomain[domain] = dedupedAPIKeys
} else {
log.Infof("No API key provided for domain \"%s\", removing domain from endpoints", domain)
delete(keysPerDomain, domain)
}
}
return keysPerDomain, nil
}