-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathutils.go
147 lines (131 loc) · 4.21 KB
/
utils.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
package main
import (
"encoding/json"
"log"
"os"
"strconv"
"strings"
)
const sampleConfig = "config.sample.json"
type clickhouseConfig struct {
Servers []string `json:"servers"`
TLSServerName string `json:"tls_server_name"`
TLSSkipVerify bool `json:"insecure_tls_skip_verify"`
DownTimeout int `json:"down_timeout"`
ConnectTimeout int `json:"connect_timeout"`
}
// Config stores config data
type Config struct {
Listen string `json:"listen"`
Clickhouse clickhouseConfig `json:"clickhouse"`
FlushCount int `json:"flush_count"`
FlushInterval int `json:"flush_interval"`
CleanInterval int `json:"clean_interval"`
RemoveQueryID bool `json:"remove_query_id"`
DumpCheckInterval int `json:"dump_check_interval"`
DumpDir string `json:"dump_dir"`
Debug bool `json:"debug"`
LogQueries bool `json:"log_queries"`
MetricsPrefix string `json:"metrics_prefix"`
UseTLS bool `json:"use_tls"`
TLSCertFile string `json:"tls_cert_file"`
TLSKeyFile string `json:"tls_key_file"`
}
func defaultConfig() Config {
return Config{
Listen: ":8124",
FlushCount: 10000,
FlushInterval: 1000,
CleanInterval: 0,
RemoveQueryID: true,
DumpCheckInterval: 300,
DumpDir: "dumps",
Debug: false,
LogQueries: false,
MetricsPrefix: "",
UseTLS: false,
TLSCertFile: "",
TLSKeyFile: "",
Clickhouse: clickhouseConfig{
DownTimeout: 60,
ConnectTimeout: 10,
TLSServerName: "",
TLSSkipVerify: false,
Servers: []string{"http://127.0.0.1:8123"},
},
}
}
// ReadJSON - read json file to struct
func ReadJSON(fn string, v interface{}) error {
file, err := os.Open(fn)
defer file.Close()
if err != nil {
return err
}
decoder := json.NewDecoder(file)
return decoder.Decode(v)
}
// HasPrefix tests case insensitive whether the string s begins with prefix.
func HasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && strings.ToLower(s[0:len(prefix)]) == strings.ToLower(prefix)
}
func readEnvInt(name string, value *int) {
s := os.Getenv(name)
if s != "" {
v, err := strconv.Atoi(s)
if err != nil {
log.Printf("ERROR: Wrong %+v env: %+v\n", name, err)
}
*value = v
}
}
func readEnvBool(name string, value *bool) {
s := os.Getenv(name)
if s != "" {
v, err := strconv.ParseBool(s)
if err != nil {
log.Printf("ERROR: Wrong %+v env: %+v\n", name, err)
} else {
*value = v
}
}
}
func readEnvString(name string, value *string) {
s := os.Getenv(name)
if s != "" {
*value = s
}
}
// ReadConfig init config data
func ReadConfig(configFile string) (Config, error) {
// Start with default values
cnf := defaultConfig()
// Load the config file if it exists
if _, err := os.Stat(configFile); err == nil {
if err := ReadJSON(configFile, &cnf); err != nil {
return Config{}, err
}
} else if !os.IsNotExist(err) {
// Return other errors (e.g., permission issues)
return Config{}, err
}
// Apply environment variable overrides
readEnvBool("CLICKHOUSE_BULK_DEBUG", &cnf.Debug)
readEnvInt("CLICKHOUSE_FLUSH_COUNT", &cnf.FlushCount)
readEnvInt("CLICKHOUSE_FLUSH_INTERVAL", &cnf.FlushInterval)
readEnvInt("CLICKHOUSE_CLEAN_INTERVAL", &cnf.CleanInterval)
readEnvBool("CLICKHOUSE_REMOVE_QUERY_ID", &cnf.RemoveQueryID)
readEnvInt("DUMP_CHECK_INTERVAL", &cnf.DumpCheckInterval)
readEnvInt("CLICKHOUSE_DOWN_TIMEOUT", &cnf.Clickhouse.DownTimeout)
readEnvInt("CLICKHOUSE_CONNECT_TIMEOUT", &cnf.Clickhouse.ConnectTimeout)
readEnvString("CLICKHOUSE_TLS_SERVER_NAME", &cnf.Clickhouse.TLSServerName)
readEnvBool("CLICKHOUSE_INSECURE_TLS_SKIP_VERIFY", &cnf.Clickhouse.TLSSkipVerify)
readEnvString("METRICS_PREFIX", &cnf.MetricsPrefix)
readEnvBool("LOG_QUERIES", &cnf.LogQueries)
serversList := os.Getenv("CLICKHOUSE_SERVERS")
if serversList != "" {
cnf.Clickhouse.Servers = strings.Split(serversList, ",")
}
log.Printf("use servers: %+v", strings.Join(cnf.Clickhouse.Servers, ", "))
return cnf, nil
}