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
197
198
199
200
201
202
203
204
205
206
use super::constant::*;
use crate::{
base::{check_validity_for_reuse_statistic, constant::*, ResourceType},
Error, Result,
};
use serde::{Deserialize, Serialize};
use serde_json;
use std::fmt;
#[derive(Serialize, Deserialize, Debug)]
pub struct AppConfig {
pub app_name: String,
pub app_type: ResourceType,
}
impl Default for AppConfig {
fn default() -> Self {
AppConfig {
app_name: DEFAULT_APP_NAME.into(),
app_type: DEFAULT_APP_TYPE.into(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LogMetricConfig {
pub use_pid: bool,
pub dir: String,
pub single_file_max_size: u64,
pub max_file_count: usize,
pub flush_interval_sec: u32,
}
impl Default for LogMetricConfig {
fn default() -> Self {
LogMetricConfig {
use_pid: true,
dir: LOG_METRICS_DIR.into(),
single_file_max_size: SINGLE_FILE_MAX_SIZE,
max_file_count: MAX_FILE_AMOUNT,
flush_interval_sec: FLUSH_INTERVAL_SEC,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ExporterConfig {
pub addr: String,
pub metrics_path: String,
}
impl Default for ExporterConfig {
fn default() -> Self {
ExporterConfig {
addr: EXPORTER_ADDR.into(),
metrics_path: EXPORTER_METRICS_PATH.into(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LogConfig {
pub metric: LogMetricConfig,
pub exporter: ExporterConfig,
pub config_file: String,
}
impl Default for LogConfig {
fn default() -> Self {
LogConfig {
metric: LogMetricConfig::default(),
exporter: ExporterConfig::default(),
config_file: LOG_CONFIG_FILE.into(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SystemStatConfig {
pub system_interval_ms: u32,
pub load_interval_ms: u32,
pub cpu_interval_ms: u32,
pub memory_interval_ms: u32,
}
impl Default for SystemStatConfig {
fn default() -> Self {
SystemStatConfig {
system_interval_ms: SYSTEM_INTERVAL_MS,
load_interval_ms: LOAD_INTERVAL_MS,
cpu_interval_ms: CPU_INTERVAL_MS,
memory_interval_ms: MEMORY_INTERVAL_MS,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct StatConfig {
pub sample_count_total: u32,
pub interval_ms_total: u32,
pub sample_count: u32,
pub interval_ms: u32,
pub system: SystemStatConfig,
}
impl Default for StatConfig {
fn default() -> Self {
StatConfig {
sample_count_total: DEFAULT_SAMPLE_COUNT_TOTAL,
interval_ms_total: DEFAULT_INTERVAL_MS_TOTAL,
sample_count: DEFAULT_SAMPLE_COUNT,
interval_ms: DEFAULT_INTERVAL_MS,
system: SystemStatConfig::default(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SentinelConfig {
pub app: AppConfig,
pub log: LogConfig,
pub stat: StatConfig,
pub use_cache_time: bool,
}
impl Default for SentinelConfig {
fn default() -> Self {
SentinelConfig {
use_cache_time: true,
app: AppConfig::default(),
log: LogConfig::default(),
stat: StatConfig::default(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ConfigEntity {
pub version: String,
pub config: SentinelConfig,
}
impl Default for ConfigEntity {
fn default() -> Self {
ConfigEntity {
version: SENTINEL_VERSION.into(),
config: SentinelConfig::default(),
}
}
}
impl ConfigEntity {
pub fn new() -> Self {
ConfigEntity::default()
}
pub fn check(&self) -> Result<()> {
if self.version.len() == 0 {
return Err(Error::msg("empty version"));
}
if self.config.app.app_name.len() == 0 {
return Err(Error::msg("empty app name"));
}
if self.config.log.metric.max_file_count == 0 {
return Err(Error::msg(
"illegal metric log configuration: max_file_count < 0",
));
}
if self.config.log.metric.single_file_max_size == 0 {
return Err(Error::msg(
"illegal metric log configuration: single_file_max_size < 0",
));
}
check_validity_for_reuse_statistic(
self.config.stat.sample_count,
self.config.stat.interval_ms,
self.config.stat.sample_count_total,
self.config.stat.interval_ms_total,
)?;
Ok(())
}
}
impl fmt::Display for ConfigEntity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let fmtted = serde_json::to_string_pretty(self).unwrap();
write!(f, "{}", fmtted)
}
}