forked from stripe/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_parse.go
146 lines (124 loc) · 3.92 KB
/
config_parse.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package veneur
import (
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"time"
"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v2"
)
const defaultBufferSizeBytes = 1048576 * 2 // 2 MB
// ReadProxyConfig unmarshals the proxy config file and slurps in its data.
func ReadProxyConfig(path string) (c ProxyConfig, err error) {
f, err := os.Open(path)
if err != nil {
return c, err
}
defer f.Close()
bts, err := ioutil.ReadAll(f)
if err != nil {
return
}
err = yaml.Unmarshal(bts, &c)
if err != nil {
return
}
err = envconfig.Process("veneur", &c)
if err != nil {
return c, err
}
return c, nil
}
// ReadConfig unmarshals the config file and slurps in it's data.
func ReadConfig(path string) (c Config, err error) {
f, err := os.Open(path)
if err != nil {
return c, err
}
defer f.Close()
return readConfig(f)
}
func readConfig(r io.Reader) (c Config, err error) {
// Unfortunately the YAML package does not
// support reader inputs
// TODO(aditya) convert this when the
// upstream PR lands
bts, err := ioutil.ReadAll(r)
if err != nil {
return
}
err = yaml.Unmarshal(bts, &c)
if err != nil {
return
}
err = envconfig.Process("veneur", &c)
if err != nil {
return c, err
}
if c.Hostname == "" && !c.OmitEmptyHostname {
c.Hostname, _ = os.Hostname()
}
if c.ReadBufferSizeBytes == 0 {
c.ReadBufferSizeBytes = defaultBufferSizeBytes
}
if c.Key != "" {
log.Warn("The config key `key` is deprecated and replaced with `datadog_api_key` and will be removed in 2.0!")
// If they set the DatadogAPIKey, favor it. Otherwise, replace it.
if c.DatadogAPIKey == "" {
c.DatadogAPIKey = c.Key
}
}
if c.APIHostname != "" {
log.Warn("The config key `api_hostname` is deprecated and replaced with `datadog_api_hostname` and will be removed in 2.0!")
if c.DatadogAPIHostname == "" {
c.DatadogAPIHostname = c.APIHostname
}
}
if c.TraceAPIAddress != "" {
log.Warn("The config key `trace_api_address` is deprecated and replaced with `datadog_trace_api_hostname` and will be removed in 2.0!")
if c.DatadogTraceAPIAddress == "" {
c.DatadogTraceAPIAddress = c.TraceAPIAddress
}
}
if c.TraceAddress != "" {
log.Warn("The config key `trace_address` is deprecated and replaced with entries in `ssf_listen_addresses` and will be removed in 2.0!")
if c.SsfAddress == "" {
c.SsfAddress = c.TraceAddress
}
}
var statsdAddrs []string
if c.UdpAddress != "" {
if len(c.StatsdListenAddresses) > 0 {
err = fmt.Errorf("`statsd_listen_addresses` and deprecated parameter `udp_address` are both present")
return
}
log.Warn("The config key `udp_address` is deprecated and replaced with entries in `statsd_listen_addresses` and will be removed in 2.0!")
statsdAddrs = append(statsdAddrs, (&url.URL{Scheme: "udp", Host: c.UdpAddress}).String())
}
if c.TcpAddress != "" {
if len(c.StatsdListenAddresses) > 0 {
err = fmt.Errorf("`statsd_listen_addresses` and deprecated parameter `tcp_address` are both present")
return
}
log.Warn("The config key `tcp_address` is deprecated and replaced with entries in `statsd_listen_addresses` and will be removed in 2.0!")
statsdAddrs = append(statsdAddrs, (&url.URL{Scheme: "tcp", Host: c.TcpAddress}).String())
}
c.StatsdListenAddresses = append(c.StatsdListenAddresses, statsdAddrs...)
var ssfAddrs []string
if c.SsfAddress != "" {
if len(c.SsfListenAddresses) > 0 {
err = fmt.Errorf("`ssf_listen_addresses` and deprecated parameter `ssf_address` are both present")
return
}
log.Warn("The config key `ssf_address` is deprecated and replaced with entries in `ssf_listen_addresses` and will be removed in 2.0!")
ssfAddrs = append(ssfAddrs, (&url.URL{Scheme: "udp", Host: c.SsfAddress}).String())
}
c.SsfListenAddresses = append(c.SsfListenAddresses, ssfAddrs...)
return c, nil
}
// ParseInterval handles parsing the flush interval as a time.Duration
func (c Config) ParseInterval() (time.Duration, error) {
return time.ParseDuration(c.Interval)
}