moira

Форк
0
/
config.go 
186 строк · 4.3 Кб
1
package cmd
2

3
import (
4
	"fmt"
5
	"io/ioutil"
6
	"strings"
7

8
	"gopkg.in/yaml.v2"
9

10
	"go.avito.ru/DO/moira"
11
	"go.avito.ru/DO/moira/database/redis"
12
	"go.avito.ru/DO/moira/logging"
13
	"go.avito.ru/DO/moira/metrics"
14
	"go.avito.ru/DO/moira/netbox"
15
	"go.avito.ru/DO/moira/sentry"
16
)
17

18
// RedisConfig is redis config structure, which are taken on the start of moira
19
type RedisConfig struct {
20
	Host string `yaml:"host"`
21
	Port string `yaml:"port"`
22
	DBID int    `yaml:"dbid"`
23
}
24

25
// GetSettings return redis config parsed from moira config files
26
func (config *RedisConfig) GetSettings() redis.Config {
27
	return redis.Config{
28
		Host: config.Host,
29
		Port: config.Port,
30
		DBID: config.DBID,
31
	}
32
}
33

34
type Neo4jConfig struct {
35
	Host         string `yaml:"host"`
36
	Port         int    `yaml:"port"`
37
	DBName       string `yaml:"db_name"`
38
	User         string `yaml:"user"`
39
	Password     string `yaml:"password"`
40
	PasswordPath string `yaml:"password_path"`
41
}
42

43
// LoggerConfig is logger settings, which are taken on the start of moira
44
type LoggerConfig struct {
45
	LogFile  string `yaml:"log_file"`
46
	LogLevel string `yaml:"log_level"`
47
}
48

49
// NetboxConfig is settings of netbox client, which are taken on the start of moira
50
type NetboxConfig struct {
51
	Enabled bool   `yaml:"enabled"`
52
	Token   string `yaml:"token"`
53
	URL     string `yaml:"url"`
54
}
55

56
func (netboxConfig *NetboxConfig) GetSettings() *netbox.Config {
57
	if !netboxConfig.Enabled {
58
		return nil
59
	}
60

61
	token, _ := moira.GetFileContent(netboxConfig.Token)
62
	url := strings.TrimSuffix(netboxConfig.URL, "/")
63
	return &netbox.Config{
64
		Token: strings.TrimSpace(token),
65
		URL:   url,
66
	}
67
}
68

69
type RateLimit struct {
70
	AcceptRate float64 `yaml:"rate"`
71
	ThreadsQty int     `yaml:"threads"`
72
}
73

74
func NewDefaultLoggerRateLimit() RateLimit {
75
	return RateLimit{
76
		AcceptRate: 1,
77
		ThreadsQty: 4,
78
	}
79
}
80

81
func NewDefaultMetricsRateLimit() RateLimit {
82
	return RateLimit{
83
		AcceptRate: 1,
84
		ThreadsQty: 4,
85
	}
86
}
87

88
func (rateLimit *RateLimit) GetSettings() moira.RateLimit {
89
	var (
90
		acceptRate = rateLimit.AcceptRate
91
		threadsQty = rateLimit.ThreadsQty
92
	)
93

94
	if acceptRate == 0 {
95
		acceptRate = 1
96
	}
97

98
	if threadsQty == 0 {
99
		threadsQty = 8
100
	}
101

102
	return moira.RateLimit{
103
		AcceptRate: acceptRate,
104
		ThreadsQty: threadsQty,
105
	}
106
}
107

108
type RsyslogConfig struct {
109
	Enabled  bool   `yaml:"enabled"`
110
	Host     string `yaml:"host"`
111
	Port     int    `yaml:"port"`
112
	Level    string `yaml:"level"`
113
	Fallback string `yaml:"fallback"`
114
	Debug    bool   `yaml:"debug"`
115
}
116

117
func (rsyslogConfig *RsyslogConfig) GetSettings() logging.Config {
118
	return logging.Config{
119
		Enabled:  rsyslogConfig.Enabled,
120
		Host:     rsyslogConfig.Host,
121
		Port:     rsyslogConfig.Port,
122
		Level:    rsyslogConfig.Level,
123
		Fallback: rsyslogConfig.Fallback,
124
		Debug:    rsyslogConfig.Debug,
125
	}
126
}
127

128
type StatsdConfig struct {
129
	Enabled bool   `yaml:"enabled"`
130
	Host    string `yaml:"host"`
131
	Port    int    `yaml:"port"`
132
	Prefix  string `yaml:"prefix"`
133
}
134

135
func (statsdConfig *StatsdConfig) GetSettings() metrics.Config {
136
	return metrics.Config{
137
		Enabled: statsdConfig.Enabled,
138
		Host:    statsdConfig.Host,
139
		Port:    statsdConfig.Port,
140
		Prefix:  statsdConfig.Prefix,
141
	}
142
}
143

144
// ProfilerConfig is pprof settings, which are taken on the start of moira
145
type ProfilerConfig struct {
146
	Listen string `yaml:"listen"`
147
}
148

149
// LivenessConfig is liveness check settings, which are taken on the start of moira
150
type LivenessConfig struct {
151
	Listen string `yaml:"listen"`
152
}
153

154
// SentryConfig is configuration for sentry reporter
155
type SentryConfig struct {
156
	Dsn        string `yaml:"dsn"`
157
	Enabled    bool   `yaml:"enabled"`
158
	IsFilePath bool   `yaml:"is_file_path"`
159
}
160

161
func (sentryConfig *SentryConfig) GetSettings() sentry.Config {
162
	return sentry.Config{
163
		Dsn:        sentryConfig.Dsn,
164
		Enabled:    sentryConfig.Enabled,
165
		IsFilePath: sentryConfig.IsFilePath,
166
	}
167
}
168

169
// ReadConfig gets config file by given file and marshal it to moira-used type
170
func ReadConfig(configFileName string, config interface{}) error {
171
	configYaml, err := ioutil.ReadFile(configFileName)
172
	if err != nil {
173
		return fmt.Errorf("Can't read file [%s] [%s]", configFileName, err.Error())
174
	}
175
	err = yaml.Unmarshal(configYaml, config)
176
	if err != nil {
177
		return fmt.Errorf("Can't parse config file [%s] [%s]", configFileName, err.Error())
178
	}
179
	return nil
180
}
181

182
// PrintConfig prints config to std
183
func PrintConfig(config interface{}) {
184
	d, _ := yaml.Marshal(&config)
185
	fmt.Println(string(d))
186
}
187

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.