This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
config.go
196 lines (167 loc) · 4.51 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/pelletier/go-toml"
)
// reads all config files into []byte
//
func concatConfigFiles(path string) ([]byte, error) {
var config []byte
pathCheck, err := os.Open(path)
if err != nil {
return []byte{}, err
}
pathInfo, err := pathCheck.Stat()
if err != nil {
return []byte{}, err
}
if pathInfo.IsDir() {
dir, _ := pathCheck.Readdir(-1)
buf := make([][]byte, len(dir))
for _, file := range dir {
if strings.HasSuffix(file.Name(), ".toml") && (file.Mode().IsRegular()) {
fileName := path + "/" + file.Name()
data, err2 := ioutil.ReadFile(fileName)
if err2 != nil {
logger.Errorf("Failed to read config file %s - %s", fileName, err2.Error())
continue
}
buf = append(buf, data)
}
}
config = bytes.Join(buf, []byte("\n"))
return config, nil
}
config, err = ioutil.ReadFile(path)
if err != nil {
return []byte{}, err
}
return config, nil
}
// resource config type
//
type resourceConfig struct {
pushGatewayURL string
defaultRoute string
resURL string
port int
host string
ssl bool
path string
routeMap string
}
// global pusher config type
// it contains instances of resourceConfig
//
type pusherConfig struct {
envLabels map[string]string
pushGatewayURL string
defaultRoute string
pushInterval time.Duration
routeMap string
resources map[string]*resourceConfig
}
// parses []byte with TOML config data into pusherConfig
// instance
//
func parseConfig(data []byte) (*pusherConfig, error) {
p := &pusherConfig{
pushInterval: time.Duration(60) * time.Second,
resources: make(map[string]*resourceConfig),
}
rd := bytes.NewReader(data)
t, err := toml.LoadReader(rd)
if err != nil {
return nil, err
}
envLabelLabels := make([]interface{}, 0)
envLabelsSet := false
if t.Has("default_env_labels") && t.Has("default_env_labels.env_labels") {
envLabelLabels = append(envLabelLabels, t.Get("default_env_labels.env_labels").([]interface{})...)
envLabelsSet = true
}
if t.Has("service_env_labels") && t.Has("service_env_labels.env_labels") {
envLabelLabels = append(envLabelLabels, t.Get("service_env_labels.env_labels").([]interface{})...)
envLabelsSet = true
}
if envLabelsSet {
envLabelsMap := make(map[string]string)
for _, label := range envLabelLabels {
strLabel := label.(string)
val := os.Getenv(strLabel)
if len(val) != 0 {
envLabelsMap[strings.ToLower(strLabel)] = val
logger.Debugf("Got additional ENV label %s with value %s", strings.ToLower(strLabel), val)
}
}
p.envLabels = envLabelsMap
}
if t.Has("config.pushgateway_url") {
p.pushGatewayURL = t.Get("config.pushgateway_url").(string)
} else {
p.pushGatewayURL = "http://localhost:9091/metrics"
}
if t.Has("config.push_interval") {
p.pushInterval = time.Duration(t.Get("config.push_interval").(int64)) * time.Second
}
if t.Has("config.route_map") {
p.routeMap = t.Get("config.route_map").(string)
}
if t.Has("config.default_route") {
p.defaultRoute = t.Get("config.default_route").(string)
}
for _, resName := range t.Keys() {
if resName == "config" || resName == "default_env_labels" || resName == "service_env_labels" {
continue
}
res := &resourceConfig{
pushGatewayURL: p.pushGatewayURL,
defaultRoute: p.defaultRoute,
resURL: "",
host: "localhost",
port: 0,
ssl: false,
path: "metrics",
routeMap: p.routeMap,
}
if t.Has(resName + ".port") {
res.port = int(t.Get(resName + ".port").(int64))
} else {
logger.Fatalf("missing port for resource '%s', exiting", resName)
continue
}
if t.Has(resName + ".pushgateway_url") {
res.pushGatewayURL = t.Get(resName + ".pushgateway_url").(string)
}
if t.Has(resName + ".default_route") {
res.defaultRoute = t.Get(resName + ".default_route").(string)
}
if t.Has(resName + ".host") {
res.host = t.Get(resName + ".host").(string)
}
if t.Has(resName + ".ssl") {
res.ssl = t.Get(resName + ".ssl").(bool)
}
if t.Has(resName + ".path") {
res.path = t.Get(resName + ".path").(string)
res.path = strings.TrimPrefix(res.path, "/")
}
if t.Has(resName + ".route_map") {
res.routeMap = t.Get(resName + ".path").(string)
}
var scheme string
if res.ssl {
scheme = "https"
} else {
scheme = "http"
}
res.resURL = fmt.Sprintf("%s://%s:%d/%s", scheme, res.host, res.port, res.path)
p.resources[resName] = res
}
return p, nil
}