forked from thanos-io/thanos
/
config.go
72 lines (63 loc) · 1.78 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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package query
import (
"fmt"
"net/url"
"strings"
"gopkg.in/yaml.v2"
"github.com/pkg/errors"
http_util "github.com/thanos-io/thanos/pkg/http"
)
type Config struct {
HTTPClientConfig http_util.ClientConfig `yaml:"http_config"`
EndpointsConfig http_util.EndpointsConfig `yaml:",inline"`
}
func DefaultConfig() Config {
return Config{
EndpointsConfig: http_util.EndpointsConfig{
Scheme: "http",
StaticAddresses: []string{},
FileSDConfigs: []http_util.FileSDConfig{},
},
}
}
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig()
type plain Config
return unmarshal((*plain)(c))
}
// LoadConfigs loads a list of Config from YAML data.
func LoadConfigs(confYAML []byte) ([]Config, error) {
var queryCfg []Config
if err := yaml.UnmarshalStrict(confYAML, &queryCfg); err != nil {
return nil, err
}
return queryCfg, nil
}
// BuildQueryConfig returns a query client configuration from a static address.
func BuildQueryConfig(queryAddrs []string) ([]Config, error) {
configs := make([]Config, 0, len(queryAddrs))
for _, addr := range queryAddrs {
if addr == "" {
return nil, errors.New("static querier address cannot be empty")
}
// If addr is missing schema, add http.
if !strings.Contains(addr, "://") {
addr = fmt.Sprintf("http://%s", addr)
}
u, err := url.Parse(addr)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse addr %q", addr)
}
configs = append(configs, Config{
EndpointsConfig: http_util.EndpointsConfig{
Scheme: u.Scheme,
StaticAddresses: []string{u.Host},
PathPrefix: u.Path,
},
})
}
return configs, nil
}