mirror of
https://github.com/chylex/Apache-Prometheus-Exporter.git
synced 2025-04-29 12:34:07 +02:00
Migrate error handling to anyhow crate
This commit is contained in:
parent
bbc416b8d3
commit
974f7f4035
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -17,10 +17,17 @@ version = "1.0.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.75"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "apache_prometheus_exporter"
|
name = "apache_prometheus_exporter"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"hyper",
|
"hyper",
|
||||||
"notify",
|
"notify",
|
||||||
"path-slash",
|
"path-slash",
|
||||||
|
@ -13,6 +13,7 @@ lto = true
|
|||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = "1.0.75"
|
||||||
hyper = { version = "0.14.27", default-features = false, features = ["http1", "server", "runtime"] }
|
hyper = { version = "0.14.27", default-features = false, features = ["http1", "server", "runtime"] }
|
||||||
notify = { version = "6.1.1", default-features = false, features = ["macos_kqueue"] }
|
notify = { version = "6.1.1", default-features = false, features = ["macos_kqueue"] }
|
||||||
path-slash = "0.2.1"
|
path-slash = "0.2.1"
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use std::{env, io};
|
|
||||||
use std::env::VarError;
|
|
||||||
use std::fs::DirEntry;
|
use std::fs::DirEntry;
|
||||||
|
use std::io;
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
|
use anyhow::{anyhow, bail, Result};
|
||||||
use path_slash::PathExt;
|
use path_slash::PathExt;
|
||||||
|
|
||||||
/// Reads and parses an environment variable that determines the path and file name pattern of log files.
|
/// Reads and parses an environment variable that determines the path and file name pattern of log files.
|
||||||
@ -13,34 +13,22 @@ use path_slash::PathExt;
|
|||||||
/// 1. A simple path to a file.
|
/// 1. A simple path to a file.
|
||||||
/// 2. A path with a wildcard anywhere in the file name.
|
/// 2. A path with a wildcard anywhere in the file name.
|
||||||
/// 3. A path with a standalone wildcard component (i.e. no prefix or suffix in the folder name).
|
/// 3. A path with a standalone wildcard component (i.e. no prefix or suffix in the folder name).
|
||||||
pub fn parse_log_file_pattern_from_env(variable_name: &str) -> Result<LogFilePattern, String> {
|
pub fn parse_log_file_pattern_from_str(pattern: &str) -> Result<LogFilePattern> {
|
||||||
match env::var(variable_name) {
|
let pattern = Path::new(pattern).to_slash().ok_or_else(|| anyhow!("Path is invalid"))?;
|
||||||
Ok(str) => {
|
if pattern.trim().is_empty() {
|
||||||
let pattern_str = Path::new(&str).to_slash().ok_or(format!("Environment variable {} contains an invalid path.", variable_name))?;
|
bail!("Path is empty");
|
||||||
parse_log_file_pattern_from_str(&pattern_str)
|
|
||||||
}
|
|
||||||
Err(err) => match err {
|
|
||||||
VarError::NotPresent => Err(format!("Environment variable {} must be set.", variable_name)),
|
|
||||||
VarError::NotUnicode(_) => Err(format!("Environment variable {} contains invalid characters.", variable_name))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_log_file_pattern_from_str(pattern_str: &str) -> Result<LogFilePattern, String> {
|
|
||||||
if pattern_str.trim().is_empty() {
|
|
||||||
return Err(String::from("Path is empty."));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((left, right)) = pattern_str.split_once('*') {
|
if let Some((left, right)) = pattern.split_once('*') {
|
||||||
parse_log_file_pattern_split_on_wildcard(left, right)
|
parse_log_file_pattern_split_on_wildcard(left, right)
|
||||||
} else {
|
} else {
|
||||||
Ok(LogFilePattern::WithoutWildcard(pattern_str.to_string()))
|
Ok(LogFilePattern::WithoutWildcard(pattern.to_string()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_log_file_pattern_split_on_wildcard(left: &str, right: &str) -> Result<LogFilePattern, String> {
|
fn parse_log_file_pattern_split_on_wildcard(left: &str, right: &str) -> Result<LogFilePattern> {
|
||||||
if left.contains('*') || right.contains('*') {
|
if left.contains('*') || right.contains('*') {
|
||||||
return Err(String::from("Path has too many wildcards."));
|
bail!("Path has too many wildcards");
|
||||||
}
|
}
|
||||||
|
|
||||||
if left.ends_with('/') && right.starts_with('/') {
|
if left.ends_with('/') && right.starts_with('/') {
|
||||||
@ -51,7 +39,7 @@ fn parse_log_file_pattern_split_on_wildcard(left: &str, right: &str) -> Result<L
|
|||||||
}
|
}
|
||||||
|
|
||||||
if right.contains('/') {
|
if right.contains('/') {
|
||||||
return Err(String::from("Path has a folder wildcard with a prefix or suffix."));
|
bail!("Path has a folder wildcard with a prefix or suffix");
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some((folder_path, file_name_prefix)) = left.rsplit_once('/') {
|
if let Some((folder_path, file_name_prefix)) = left.rsplit_once('/') {
|
||||||
@ -122,10 +110,7 @@ impl LogFilePattern {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn search_without_wildcard(path_str: &String) -> Result<Vec<LogFilePath>, io::Error> {
|
fn search_without_wildcard(path_str: &String) -> Result<Vec<LogFilePath>, io::Error> {
|
||||||
let path = Path::new(path_str);
|
if Path::new(path_str).is_file() {
|
||||||
let is_valid = path.is_file() || matches!(path.parent(), Some(parent) if parent.is_dir());
|
|
||||||
|
|
||||||
if is_valid {
|
|
||||||
Ok(vec![LogFilePath::with_empty_label(path_str)])
|
Ok(vec![LogFilePath::with_empty_label(path_str)])
|
||||||
} else {
|
} else {
|
||||||
Err(io::Error::from(ErrorKind::NotFound))
|
Err(io::Error::from(ErrorKind::NotFound))
|
||||||
@ -182,23 +167,23 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn empty_path() {
|
fn empty_path() {
|
||||||
assert!(matches!(parse_log_file_pattern_from_str(""), Err(err) if err == "Path is empty."));
|
assert!(matches!(parse_log_file_pattern_from_str(""), Err(err) if err.to_string() == "Path is empty"));
|
||||||
assert!(matches!(parse_log_file_pattern_from_str(" "), Err(err) if err == "Path is empty."));
|
assert!(matches!(parse_log_file_pattern_from_str(" "), Err(err) if err.to_string() == "Path is empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn too_many_wildcards() {
|
fn too_many_wildcards() {
|
||||||
assert!(matches!(parse_log_file_pattern_from_str("/path/*/to/files/*.log"), Err(err) if err == "Path has too many wildcards."));
|
assert!(matches!(parse_log_file_pattern_from_str("/path/*/to/files/*.log"), Err(err) if err.to_string() == "Path has too many wildcards"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn folder_wildcard_with_prefix_not_supported() {
|
fn folder_wildcard_with_prefix_not_supported() {
|
||||||
assert!(matches!(parse_log_file_pattern_from_str("/path/*abc/to/files/access.log"), Err(err) if err == "Path has a folder wildcard with a prefix or suffix."));
|
assert!(matches!(parse_log_file_pattern_from_str("/path/*abc/to/files/access.log"), Err(err) if err.to_string() == "Path has a folder wildcard with a prefix or suffix"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn folder_wildcard_with_suffix_not_supported() {
|
fn folder_wildcard_with_suffix_not_supported() {
|
||||||
assert!(matches!(parse_log_file_pattern_from_str("/path/abc*/to/files/access.log"), Err(err) if err == "Path has a folder wildcard with a prefix or suffix."));
|
assert!(matches!(parse_log_file_pattern_from_str("/path/abc*/to/files/access.log"), Err(err) if err.to_string() == "Path has a folder wildcard with a prefix or suffix"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -2,6 +2,7 @@ use std::cmp::max;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use notify::{Event, EventKind};
|
use notify::{Event, EventKind};
|
||||||
use notify::event::{CreateKind, ModifyKind};
|
use notify::event::{CreateKind, ModifyKind};
|
||||||
use tokio::fs::File;
|
use tokio::fs::File;
|
||||||
@ -50,10 +51,9 @@ impl LogWatcherConfiguration {
|
|||||||
self.files.push((path, metadata));
|
self.files.push((path, metadata));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start(self, metrics: &Metrics) -> bool {
|
pub async fn start(self, metrics: &Metrics) -> Result<()> {
|
||||||
if self.files.is_empty() {
|
if self.files.is_empty() {
|
||||||
println!("[LogWatcher] No log files provided.");
|
bail!("No log files provided");
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("[LogWatcher] Watching {} access log file(s) and {} error log file(s).", self.count_files_of_kind(LogFileKind::Access), self.count_files_of_kind(LogFileKind::Error));
|
println!("[LogWatcher] Watching {} access log file(s) and {} error log file(s).", self.count_files_of_kind(LogFileKind::Access), self.count_files_of_kind(LogFileKind::Error));
|
||||||
@ -73,32 +73,16 @@ impl LogWatcherConfiguration {
|
|||||||
prepared_files.push(PreparedFile { path, metadata, fs_event_receiver });
|
prepared_files.push(PreparedFile { path, metadata, fs_event_receiver });
|
||||||
}
|
}
|
||||||
|
|
||||||
let fs_watcher = match FsWatcher::new(fs_callbacks) {
|
let fs_watcher = FsWatcher::new(fs_callbacks).context("Could not create filesystem watcher")?;
|
||||||
Ok(fs_watcher) => fs_watcher,
|
|
||||||
Err(e) => {
|
|
||||||
println!("[LogWatcher] Error creating filesystem watcher: {}", e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for file in &prepared_files {
|
for file in &prepared_files {
|
||||||
let file_path = &file.path;
|
let file_path = &file.path;
|
||||||
if !file_path.is_absolute() {
|
if !file_path.is_absolute() {
|
||||||
println!("[LogWatcher] Error creating filesystem watcher, path is not absolute: {}", file_path.to_string_lossy());
|
bail!("Path is not absolute: {}", file_path.to_string_lossy());
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let parent_path = if let Some(parent) = file_path.parent() {
|
let parent_path = file_path.parent().ok_or_else(|| anyhow!("Path has no parent: {}", file_path.to_string_lossy()))?;
|
||||||
parent
|
fs_watcher.watch(parent_path).await.with_context(|| format!("Could not create filesystem watcher for directory: {}", parent_path.to_string_lossy()))?;
|
||||||
} else {
|
|
||||||
println!("[LogWatcher] Error creating filesystem watcher for parent directory of file \"{}\", parent directory does not exist", file_path.to_string_lossy());
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(e) = fs_watcher.watch(parent_path).await {
|
|
||||||
println!("[LogWatcher] Error creating filesystem watcher for directory \"{}\": {}", parent_path.to_string_lossy(), e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let fs_watcher = Arc::new(fs_watcher);
|
let fs_watcher = Arc::new(fs_watcher);
|
||||||
@ -108,15 +92,13 @@ impl LogWatcherConfiguration {
|
|||||||
let _ = metrics.requests_total.get_or_create(&label_set);
|
let _ = metrics.requests_total.get_or_create(&label_set);
|
||||||
let _ = metrics.errors_total.get_or_create(&label_set);
|
let _ = metrics.errors_total.get_or_create(&label_set);
|
||||||
|
|
||||||
let log_watcher = match LogWatcher::create(file.path, file.metadata, metrics.clone(), Arc::clone(&fs_watcher), file.fs_event_receiver).await {
|
let log_watcher = LogWatcher::create(file.path.clone(), file.metadata, metrics.clone(), Arc::clone(&fs_watcher), file.fs_event_receiver);
|
||||||
Some(log_watcher) => log_watcher,
|
let log_watcher = log_watcher.await.with_context(|| format!("Could not watch log file: {}", file.path.to_string_lossy()))?;
|
||||||
None => return false,
|
|
||||||
};
|
|
||||||
|
|
||||||
tokio::spawn(log_watcher.watch());
|
tokio::spawn(log_watcher.watch());
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,14 +109,10 @@ struct LogWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl LogWatcher {
|
impl LogWatcher {
|
||||||
async fn create(path: PathBuf, metadata: LogFileMetadata, metrics: Metrics, fs_watcher: Arc<FsWatcher>, fs_event_receiver: Receiver<Event>) -> Option<Self> {
|
async fn create(path: PathBuf, metadata: LogFileMetadata, metrics: Metrics, fs_watcher: Arc<FsWatcher>, fs_event_receiver: Receiver<Event>) -> Result<Self> {
|
||||||
let state = match LogWatchingState::initialize(path.clone(), fs_watcher).await {
|
let state = LogWatchingState::initialize(path.clone(), fs_watcher).await?;
|
||||||
Some(state) => state,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let processor = LogLineProcessor { path, metadata, metrics };
|
let processor = LogLineProcessor { path, metadata, metrics };
|
||||||
Some(LogWatcher { state, processor, fs_event_receiver })
|
Ok(LogWatcher { state, processor, fs_event_receiver })
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn watch(mut self) {
|
async fn watch(mut self) {
|
||||||
@ -176,8 +154,11 @@ impl LogWatcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.state = match self.state.reinitialize().await {
|
self.state = match self.state.reinitialize().await {
|
||||||
Some(state) => state,
|
Ok(state) => state,
|
||||||
None => break 'read_loop,
|
Err(e) => {
|
||||||
|
println!("Could not re-watch log file \"{}\": {}", path.to_string_lossy(), e);
|
||||||
|
break 'read_loop;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
continue 'read_loop;
|
continue 'read_loop;
|
||||||
@ -222,24 +203,16 @@ struct LogWatchingState {
|
|||||||
impl LogWatchingState {
|
impl LogWatchingState {
|
||||||
const DEFAULT_BUFFER_CAPACITY: usize = 1024 * 4;
|
const DEFAULT_BUFFER_CAPACITY: usize = 1024 * 4;
|
||||||
|
|
||||||
async fn initialize(path: PathBuf, fs_watcher: Arc<FsWatcher>) -> Option<LogWatchingState> {
|
async fn initialize(path: PathBuf, fs_watcher: Arc<FsWatcher>) -> Result<LogWatchingState> {
|
||||||
if let Err(e) = fs_watcher.watch(&path).await {
|
fs_watcher.watch(&path).await.context("Could not create filesystem watcher")?;
|
||||||
println!("[LogWatcher] Error creating filesystem watcher for file \"{}\": {}", path.to_string_lossy(), e);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let lines = match File::open(&path).await {
|
let file = File::open(&path).await.context("Could not open file")?;
|
||||||
Ok(file) => BufReader::with_capacity(Self::DEFAULT_BUFFER_CAPACITY, file).lines(),
|
let lines = BufReader::with_capacity(Self::DEFAULT_BUFFER_CAPACITY, file).lines();
|
||||||
Err(e) => {
|
|
||||||
println!("[LogWatcher] Error opening file \"{}\": {}", path.to_string_lossy(), e);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(LogWatchingState { path, lines, fs_watcher })
|
Ok(LogWatchingState { path, lines, fs_watcher })
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn reinitialize(self) -> Option<LogWatchingState> {
|
async fn reinitialize(self) -> Result<LogWatchingState> {
|
||||||
LogWatchingState::initialize(self.path, self.fs_watcher).await
|
LogWatchingState::initialize(self.path, self.fs_watcher).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::env::VarError;
|
||||||
|
|
||||||
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
|
|
||||||
use log_file_watcher::{LogFileKind, LogWatcherConfiguration};
|
use log_file_watcher::{LogFileKind, LogWatcherConfiguration};
|
||||||
|
|
||||||
use crate::logs::log_file_pattern::{LogFilePath, parse_log_file_pattern_from_env};
|
use crate::logs::log_file_pattern::{LogFilePath, parse_log_file_pattern_from_str};
|
||||||
use crate::metrics::Metrics;
|
use crate::metrics::Metrics;
|
||||||
|
|
||||||
mod access_log_parser;
|
mod access_log_parser;
|
||||||
@ -8,36 +13,27 @@ mod filesystem_watcher;
|
|||||||
mod log_file_pattern;
|
mod log_file_pattern;
|
||||||
mod log_file_watcher;
|
mod log_file_watcher;
|
||||||
|
|
||||||
pub fn find_log_files(environment_variable_name: &str, log_kind: &str) -> Option<Vec<LogFilePath>> {
|
pub fn find_log_files(environment_variable_name: &str, log_kind: &str) -> Result<Vec<LogFilePath>> {
|
||||||
let log_file_pattern = match parse_log_file_pattern_from_env(environment_variable_name) {
|
let log_file_pattern_str = env::var(environment_variable_name).map_err(|err| match err {
|
||||||
Ok(pattern) => pattern,
|
VarError::NotPresent => anyhow!("Environment variable {} must be set", environment_variable_name),
|
||||||
Err(error) => {
|
VarError::NotUnicode(_) => anyhow!("Environment variable {} contains invalid characters", environment_variable_name)
|
||||||
println!("Error: {}", error);
|
})?;
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let log_files = match log_file_pattern.search() {
|
let log_file_pattern = parse_log_file_pattern_from_str(&log_file_pattern_str).with_context(|| format!("Could not parse pattern: {}", log_file_pattern_str))?;
|
||||||
Ok(files) => files,
|
let log_files = log_file_pattern.search().with_context(|| format!("Could not search files: {}", log_file_pattern_str))?;
|
||||||
Err(error) => {
|
|
||||||
println!("Error searching {} files: {}", log_kind, error);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if log_files.is_empty() {
|
if log_files.is_empty() {
|
||||||
println!("Found no matching {} files.", log_kind);
|
bail!("No files match pattern: {}", log_file_pattern_str);
|
||||||
return None;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for log_file in &log_files {
|
for log_file in &log_files {
|
||||||
println!("Found {} file: {} (label \"{}\")", log_kind, log_file.path.display(), log_file.label);
|
println!("Found {} file: {} (label \"{}\")", log_kind, log_file.path.display(), log_file.label);
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(log_files)
|
Ok(log_files)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start_log_watcher(access_log_files: Vec<LogFilePath>, error_log_files: Vec<LogFilePath>, metrics: Metrics) -> bool {
|
pub async fn start_log_watcher(access_log_files: Vec<LogFilePath>, error_log_files: Vec<LogFilePath>, metrics: Metrics) -> Result<()> {
|
||||||
let mut watcher = LogWatcherConfiguration::new();
|
let mut watcher = LogWatcherConfiguration::new();
|
||||||
|
|
||||||
for log_file in access_log_files.into_iter() {
|
for log_file in access_log_files.into_iter() {
|
||||||
|
47
src/main.rs
47
src/main.rs
@ -1,9 +1,9 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
use std::net::{IpAddr, SocketAddr};
|
use std::net::{IpAddr, SocketAddr};
|
||||||
use std::process::ExitCode;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Context};
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
|
|
||||||
use crate::metrics::Metrics;
|
use crate::metrics::Metrics;
|
||||||
@ -17,49 +17,22 @@ const ACCESS_LOG_FILE_PATTERN: &str = "ACCESS_LOG_FILE_PATTERN";
|
|||||||
const ERROR_LOG_FILE_PATTERN: &str = "ERROR_LOG_FILE_PATTERN";
|
const ERROR_LOG_FILE_PATTERN: &str = "ERROR_LOG_FILE_PATTERN";
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() -> ExitCode {
|
async fn main() -> anyhow::Result<()> {
|
||||||
let host = env::var("HTTP_HOST").unwrap_or(String::from("127.0.0.1"));
|
let host = env::var("HTTP_HOST").unwrap_or(String::from("127.0.0.1"));
|
||||||
let bind_ip = match IpAddr::from_str(&host) {
|
let bind_ip = IpAddr::from_str(&host).map_err(|_| anyhow!("Invalid HTTP host: {}", host))?;
|
||||||
Ok(addr) => addr,
|
|
||||||
Err(_) => {
|
|
||||||
println!("Invalid HTTP host: {}", host);
|
|
||||||
return ExitCode::FAILURE;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("Initializing exporter...");
|
println!("Initializing exporter...");
|
||||||
|
|
||||||
let access_log_files = match logs::find_log_files(ACCESS_LOG_FILE_PATTERN, "access log") {
|
let access_log_files = logs::find_log_files(ACCESS_LOG_FILE_PATTERN, "access log").context("Could not find access log files")?;
|
||||||
Some(files) => files,
|
let error_log_files = logs::find_log_files(ERROR_LOG_FILE_PATTERN, "error log").context("Could not find error log files")?;
|
||||||
None => return ExitCode::FAILURE,
|
|
||||||
};
|
|
||||||
|
|
||||||
let error_log_files = match logs::find_log_files(ERROR_LOG_FILE_PATTERN, "error log") {
|
|
||||||
Some(files) => files,
|
|
||||||
None => return ExitCode::FAILURE,
|
|
||||||
};
|
|
||||||
|
|
||||||
let server = match WebServer::try_bind(SocketAddr::new(bind_ip, 9240)) {
|
|
||||||
Some(server) => server,
|
|
||||||
None => return ExitCode::FAILURE
|
|
||||||
};
|
|
||||||
|
|
||||||
|
let server = WebServer::try_bind(SocketAddr::new(bind_ip, 9240)).context("Could not configure web server")?;
|
||||||
let (metrics_registry, metrics) = Metrics::new();
|
let (metrics_registry, metrics) = Metrics::new();
|
||||||
|
|
||||||
if !logs::start_log_watcher(access_log_files, error_log_files, metrics).await {
|
logs::start_log_watcher(access_log_files, error_log_files, metrics).await.context("Could not start watching logs")?;
|
||||||
return ExitCode::FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
tokio::spawn(server.serve(Mutex::new(metrics_registry)));
|
tokio::spawn(server.serve(Mutex::new(metrics_registry)));
|
||||||
|
|
||||||
match signal::ctrl_c().await {
|
signal::ctrl_c().await.with_context(|| "Could not register CTRL-C handler")?;
|
||||||
Ok(_) => {
|
println!("Received CTRL-C, shutting down...");
|
||||||
println!("Received CTRL-C, shutting down...");
|
Ok(())
|
||||||
ExitCode::SUCCESS
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Error registering CTRL-C handler: {}", e);
|
|
||||||
ExitCode::FAILURE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ use std::net::SocketAddr;
|
|||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use anyhow::Context;
|
||||||
use hyper::{Body, Error, Method, Request, Response, Server, StatusCode};
|
use hyper::{Body, Error, Method, Request, Response, Server, StatusCode};
|
||||||
use hyper::http::Result;
|
use hyper::http::Result;
|
||||||
use hyper::server::Builder;
|
use hyper::server::Builder;
|
||||||
@ -19,23 +20,17 @@ pub struct WebServer {
|
|||||||
|
|
||||||
impl WebServer {
|
impl WebServer {
|
||||||
//noinspection HttpUrlsUsage
|
//noinspection HttpUrlsUsage
|
||||||
pub fn try_bind(addr: SocketAddr) -> Option<WebServer> {
|
pub fn try_bind(addr: SocketAddr) -> anyhow::Result<WebServer> {
|
||||||
println!("[WebServer] Starting web server on {0} with metrics endpoint: http://{0}/metrics", addr);
|
println!("[WebServer] Starting web server on {0} with metrics endpoint: http://{0}/metrics", addr);
|
||||||
let builder = match Server::try_bind(&addr) {
|
|
||||||
Ok(builder) => builder,
|
|
||||||
Err(e) => {
|
|
||||||
println!("[WebServer] Could not bind to {}: {}", addr, e);
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
let builder = Server::try_bind(&addr).with_context(|| format!("Could not bind to {}", addr))?;
|
||||||
let builder = builder.tcp_keepalive(Some(Duration::from_secs(60)));
|
let builder = builder.tcp_keepalive(Some(Duration::from_secs(60)));
|
||||||
let builder = builder.http1_only(true);
|
let builder = builder.http1_only(true);
|
||||||
let builder = builder.http1_keepalive(true);
|
let builder = builder.http1_keepalive(true);
|
||||||
let builder = builder.http1_max_buf_size(MAX_BUFFER_SIZE);
|
let builder = builder.http1_max_buf_size(MAX_BUFFER_SIZE);
|
||||||
let builder = builder.http1_header_read_timeout(Duration::from_secs(10));
|
let builder = builder.http1_header_read_timeout(Duration::from_secs(10));
|
||||||
|
|
||||||
Some(WebServer { builder })
|
Ok(WebServer { builder })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn serve(self, metrics_registry: Mutex<Registry>) {
|
pub async fn serve(self, metrics_registry: Mutex<Registry>) {
|
||||||
|
Loading…
Reference in New Issue
Block a user