forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rs
More file actions
295 lines (248 loc) · 10.7 KB
/
app.rs
File metadata and controls
295 lines (248 loc) · 10.7 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
use crate::cli::{handle_config_errors, Color, LogFormat, Opts, RootOpts, SubCommand};
use crate::signal::SignalTo;
use crate::topology::RunningTopology;
use crate::{
config, generate, heartbeat, list, metrics, signal, topology, trace, unit_test, validate,
};
use std::cmp::max;
use std::collections::HashMap;
use std::path::PathBuf;
use futures::StreamExt;
use tokio::sync::mpsc;
#[cfg(feature = "sources-host_metrics")]
use crate::sources::host_metrics;
#[cfg(feature = "api-client")]
use crate::top;
#[cfg(feature = "api")]
use crate::{api, internal_events::ApiStarted};
#[cfg(windows)]
use crate::service;
use crate::internal_events::{
VectorConfigLoadFailed, VectorQuit, VectorRecoveryFailed, VectorReloadFailed, VectorReloaded,
VectorStarted, VectorStopped,
};
use tokio::runtime;
use tokio::runtime::Runtime;
pub struct ApplicationConfig {
pub config_paths: Vec<(PathBuf, config::FormatHint)>,
pub topology: RunningTopology,
pub graceful_crash: mpsc::UnboundedReceiver<()>,
#[cfg(feature = "api")]
pub api: config::api::Options,
}
pub struct Application {
opts: RootOpts,
pub config: ApplicationConfig,
pub runtime: Runtime,
}
impl Application {
pub fn prepare() -> Result<Self, exitcode::ExitCode> {
let opts = Opts::get_matches();
Self::prepare_from_opts(opts)
}
pub fn prepare_from_opts(opts: Opts) -> Result<Self, exitcode::ExitCode> {
openssl_probe::init_ssl_cert_env_vars();
let level = std::env::var("LOG").unwrap_or_else(|_| match opts.log_level() {
"off" => "off".to_owned(),
level => [
format!("vector={}", level),
format!("codec={}", level),
format!("file_source={}", level),
"tower_limit=trace".to_owned(),
format!("rdkafka={}", level),
]
.join(","),
});
let root_opts = opts.root;
let sub_command = opts.sub_command;
let color = match root_opts.color {
#[cfg(unix)]
Color::Auto => atty::is(atty::Stream::Stdout),
#[cfg(windows)]
Color::Auto => false, // ANSI colors are not supported by cmd.exe
Color::Always => true,
Color::Never => false,
};
let json = match &root_opts.log_format {
LogFormat::Text => false,
LogFormat::Json => true,
};
trace::init(color, json, &level);
metrics::init().expect("metrics initialization failed");
if let Some(threads) = root_opts.threads {
if threads < 1 {
error!("The `threads` argument must be greater or equal to 1.");
return Err(exitcode::CONFIG);
}
}
let mut rt = {
let threads = root_opts.threads.unwrap_or_else(|| max(1, num_cpus::get()));
runtime::Builder::new()
.threaded_scheduler()
.enable_all()
.core_threads(threads)
.build()
.expect("Unable to create async runtime")
};
let config = {
let config_paths = root_opts.config_paths_with_formats();
let watch_config = root_opts.watch_config;
let require_healthy = root_opts.require_healthy;
rt.block_on(async move {
if let Some(s) = sub_command {
let code = match s {
SubCommand::Validate(v) => validate::validate(&v, color).await,
SubCommand::List(l) => list::cmd(&l),
SubCommand::Test(t) => unit_test::cmd(&t).await,
SubCommand::Generate(g) => generate::cmd(&g),
#[cfg(feature = "api-client")]
SubCommand::Top(t) => top::cmd(&t).await,
#[cfg(windows)]
SubCommand::Service(s) => service::cmd(&s),
#[cfg(feature = "vrl-cli")]
SubCommand::VRL(s) => remap_cli::cmd::cmd(&s),
};
return Err(code);
};
info!(message = "Log level is enabled.", level = ?level);
#[cfg(feature = "sources-host_metrics")]
host_metrics::init_roots();
let config_paths = config::process_paths(&config_paths).ok_or(exitcode::CONFIG)?;
if watch_config {
// Start listening for config changes immediately.
config::watcher::spawn_thread(config_paths.iter().map(|(path, _)| path), None)
.map_err(|error| {
error!(message = "Unable to start config watcher.", %error);
exitcode::CONFIG
})?;
}
info!(
message = "Loading configs.",
path = ?config_paths
);
let mut config =
config::load_from_paths(&config_paths, false).map_err(handle_config_errors)?;
config::LOG_SCHEMA
.set(config.global.log_schema.clone())
.expect("Couldn't set schema");
if !config.healthchecks.enabled {
info!("Health checks are disabled.");
}
config.healthchecks.set_require_healthy(require_healthy);
let diff = config::ConfigDiff::initial(&config);
let pieces = topology::build_or_log_errors(&config, &diff, HashMap::new())
.await
.ok_or(exitcode::CONFIG)?;
#[cfg(feature = "api")]
let api = config.api;
let result = topology::start_validated(config, diff, pieces).await;
let (topology, graceful_crash) = result.ok_or(exitcode::CONFIG)?;
Ok(ApplicationConfig {
config_paths,
topology,
graceful_crash,
#[cfg(feature = "api")]
api,
})
})
}?;
Ok(Application {
opts: root_opts,
config,
runtime: rt,
})
}
pub fn run(self) {
let mut rt = self.runtime;
let mut graceful_crash = self.config.graceful_crash;
let mut topology = self.config.topology;
let mut config_paths = self.config.config_paths;
let opts = self.opts;
#[cfg(feature = "api")]
let api_config = self.config.api;
// Any internal_logs sources will have grabbed a copy of the
// early buffer by this point and set up a subscriber.
crate::trace::stop_buffering();
rt.block_on(async move {
emit!(VectorStarted);
tokio::spawn(heartbeat::heartbeat());
#[cfg(feature = "api")]
// assigned to prevent the API terminating when falling out of scope
let api_server = if api_config.enabled {
emit!(ApiStarted {
addr: api_config.address.unwrap(),
playground: api_config.playground
});
Some(api::Server::start(topology.config()))
} else {
info!(message="API is disabled, enable by setting `api.enabled` to `true` and use commands like `vector top`.");
None
};
let signals = signal::signals();
tokio::pin!(signals);
let mut sources_finished = topology.sources_finished();
let signal = loop {
tokio::select! {
Some(signal) = signals.next() => {
if signal == SignalTo::Reload {
// Reload paths
config_paths = config::process_paths(&opts.config_paths_with_formats()).unwrap_or(config_paths);
// Reload config
let new_config = config::load_from_paths(&config_paths, false).map_err(handle_config_errors).ok();
if let Some(mut new_config) = new_config {
new_config.healthchecks.set_require_healthy(opts.require_healthy);
match topology
.reload_config_and_respawn(new_config)
.await
{
Ok(true) => {
#[cfg(feature="api")]
if let Some(ref api_server) = api_server {
api_server.update_config(topology.config())
}
emit!(VectorReloaded { config_paths: &config_paths })
},
Ok(false) => emit!(VectorReloadFailed),
// Trigger graceful shutdown for what remains of the topology
Err(()) => {
emit!(VectorReloadFailed);
emit!(VectorRecoveryFailed);
break SignalTo::Shutdown;
}
}
sources_finished = topology.sources_finished();
} else {
emit!(VectorConfigLoadFailed);
}
} else {
break signal;
}
}
// Trigger graceful shutdown if a component crashed, or all sources have ended.
_ = graceful_crash.next() => break SignalTo::Shutdown,
_ = &mut sources_finished => break SignalTo::Shutdown,
else => unreachable!("Signal streams never end"),
}
};
match signal {
SignalTo::Shutdown => {
emit!(VectorStopped);
tokio::select! {
_ = topology.stop() => (), // Graceful shutdown finished
_ = signals.next() => {
// It is highly unlikely that this event will exit from topology.
emit!(VectorQuit);
// Dropping the shutdown future will immediately shut the server down
}
}
}
SignalTo::Quit => {
// It is highly unlikely that this event will exit from topology.
emit!(VectorQuit);
drop(topology);
}
SignalTo::Reload => unreachable!(),
}
});
}
}