forked from criteo/graphite-remote-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
156 lines (126 loc) · 4.18 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
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
147
148
149
150
151
152
153
154
155
156
package config
import (
"fmt"
"io/ioutil"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/common/promlog"
yaml "gopkg.in/yaml.v2"
graphite "github.com/criteo/graphite-remote-adapter/client/graphite/config"
"github.com/criteo/graphite-remote-adapter/utils"
)
// Load parses the YAML input s into a Config.
func Load(s string) (*Config, error) {
cfg := &Config{}
*cfg = DefaultConfig
err := yaml.Unmarshal([]byte(s), cfg)
if err != nil {
return nil, err
}
cfg.original = s
return cfg, nil
}
// LoadFile parses the given YAML file into a Config.
func LoadFile(logger log.Logger, filename string) (*Config, error) {
level.Info(logger).Log("file", filename, "msg", "Loading configuration file")
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cfg, err := Load(string(content))
if err != nil {
return nil, err
}
return cfg, nil
}
// DefaultConfig is the default top-level configuration.
var DefaultConfig = Config{
Web: webOptions{
ListenAddress: "0.0.0.0:9201",
TelemetryPath: "/metrics",
},
Read: readOptions{
Timeout: 5 * time.Minute,
Delay: 1 * time.Hour,
IgnoreError: true,
},
Write: writeOptions{
Timeout: 5 * time.Minute,
},
Graphite: graphite.DefaultConfig,
}
// Config is the top-level configuration.
type Config struct {
ConfigFile string
LogLevel promlog.AllowedLevel
Web webOptions `yaml:"web,omitempty" json:"web,omitempty"`
Read readOptions `yaml:"read,omitempty" json:"read,omitempty"`
Write writeOptions `yaml:"write,omitempty" json:"write,omitempty"`
Graphite graphite.Config `yaml:"graphite,omitempty" json:"graphite,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
// original is the input from which the Config was parsed.
original string
}
func (c Config) String() string {
b, err := yaml.Marshal(c)
if err != nil {
return fmt.Sprintf("<error creating config string: %s>", err)
}
return string(b)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain Config
*c = DefaultConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
return utils.CheckOverflow(c.XXX, "config")
}
type webOptions struct {
ListenAddress string `yaml:"listen_address,omitempty" json:"listen_address,omitempty"`
TelemetryPath string `yaml:"telemetry_path,omitempty" json:"telemetry_path,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (opts *webOptions) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain webOptions
*opts = DefaultConfig.Web
if err := unmarshal((*plain)(opts)); err != nil {
return err
}
return utils.CheckOverflow(opts.XXX, "webOptions")
}
type readOptions struct {
Timeout time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
Delay time.Duration `yaml:"delay,omitempty" json:"delay,omitempty"`
IgnoreError bool `yaml:"ignore_error,omitempty" json:"ignore_error,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (opts *readOptions) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain readOptions
*opts = DefaultConfig.Read
if err := unmarshal((*plain)(opts)); err != nil {
return err
}
return utils.CheckOverflow(opts.XXX, "readOptions")
}
type writeOptions struct {
Timeout time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
// Catches all undefined fields and must be empty after parsing.
XXX map[string]interface{} `yaml:",inline" json:"-"`
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (opts *writeOptions) UnmarshalYAML(unmarshal func(interface{}) error) error {
type plain writeOptions
*opts = DefaultConfig.Write
if err := unmarshal((*plain)(opts)); err != nil {
return err
}
return utils.CheckOverflow(opts.XXX, "writeOptions")
}