peshming/src/config.rs
Jan Christian Grünhage 854ddced3a chore!: replace prometheus with metrics
As part of the overall modernization of this application, this commit
replaces the prometheus crate with the generic metrics facade crate, and
the metrics_exporter_prometheus crate for exporting the metrics to
prometheus.

This also removes some useless http metrics that were cargo-culted in
from the prometheus crate example.

BREAKING CHANGE: http metrics are removed by this change
2022-03-17 16:25:40 +01:00

151 lines
5.5 KiB
Rust

/********************************************************************************
* Prometheus exporter for monitoring network connectivity using icmp pings *
* *
* Copyright (C) 2019-2020 Jan Christian Grünhage *
* Copyright (C) 2020 Famedly GmbH *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
********************************************************************************/
use anyhow::{Context, Result};
use clap::{clap_app, crate_authors, crate_description, crate_name, crate_version};
use log::info;
use metrics::{describe_histogram, register_histogram, Unit};
use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle};
use serde::Deserialize;
use serde_with::{serde_as, DurationMilliSeconds};
use std::{collections::HashMap, time::Duration};
pub(crate) struct App {
pub(crate) config: Config,
pub(crate) handle: PrometheusHandle,
}
#[derive(Deserialize, Clone)]
pub(crate) struct Config {
pub(crate) listener: std::net::SocketAddr,
pub(crate) ping: PingConfig,
#[serde(default)]
pub(crate) log: LogConfig,
}
#[serde_as]
#[derive(Deserialize, Clone)]
pub(crate) struct PingConfig {
#[serde_as(as = "DurationMilliSeconds<f64>")]
#[serde(default = "default_timeout")]
pub(crate) timeout: Duration,
pub(crate) hosts: HashMap<std::net::IpAddr, u64>,
}
fn default_timeout() -> Duration {
Duration::from_secs(3)
}
#[derive(Deserialize, Clone)]
pub(crate) struct LogConfig {
pub(crate) level: log::LevelFilter,
}
impl Default for LogConfig {
fn default() -> Self {
Self {
level: log::LevelFilter::Error,
}
}
}
fn setup_clap() -> clap::ArgMatches<'static> {
clap_app!(myapp =>
(name: crate_name!())
(version: crate_version!())
(author: crate_authors!())
(about: crate_description!())
(@arg config: +required "Set config file")
(@arg v: -v --verbose ... "Be verbose (you can add this up to 4 times for more logs).
By default, only errors are logged, so no output is a good thing.")
)
.get_matches()
}
fn setup_fern(level: log::LevelFilter) {
match fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{}][{}] {}",
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
record.level(),
message
))
})
.level(level)
.chain(std::io::stdout())
.apply()
{
Err(_) => {
eprintln!("error setting up logging!");
}
_ => info!("logging set up properly"),
}
}
fn read_config(path: &str) -> Result<Config> {
let config_file_content = std::fs::read_to_string(path).context("Couldn't read config file")?;
Ok(toml::from_str(&config_file_content).context("Couldn't parse config file")?)
}
pub(crate) fn setup_app() -> Result<App> {
let clap = setup_clap();
let config_path = clap
.value_of("config")
.context("Got no config file. clap should've catched this")?;
let config = read_config(config_path).context("Couldn't read config file!")?;
setup_fern(determine_level(clap.occurrences_of("v"), config.log.level));
let handle = setup_prometheus(&config)?;
Ok(App { config, handle })
}
pub(crate) fn setup_prometheus(config: &Config) -> Result<PrometheusHandle> {
let handle = PrometheusBuilder::new()
.set_buckets_for_metric(Matcher::Full("ping_rtt_milliseconds".into()), &vec![
0.5, 1.0, 5.0, 10.0, 15.0, 20.0, 25.0, 50.0, 75.0, 100.0, 150.0, 200.0, 250.0, 300.0,
350.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 900.0, 1000.0,
1250.0, 1500.0, 1750.0, 2000.0,
])?
.install_recorder()?;
for target in config.ping.hosts.keys() {
register_histogram!("ping_rtt_milliseconds", "target" => target.to_string());
}
describe_histogram!(
"ping_rtt_milliseconds",
Unit::Milliseconds,
"The ping round trip time in milliseconds"
);
Ok(handle)
}
fn determine_level(verbose_occurrences: u64, config_level: log::LevelFilter) -> log::LevelFilter {
let cli_level = match verbose_occurrences {
0 => log::LevelFilter::Error,
1 => log::LevelFilter::Warn,
2 => log::LevelFilter::Info,
3 => log::LevelFilter::Debug,
_ => log::LevelFilter::Trace,
};
Ord::max(cli_level, config_level)
}