mirror of
https://codeberg.org/icewind/prometheus-edge-trigger.git
synced 2026-06-03 10:14:12 +02:00
allow use prometheus service discovery files as params
This commit is contained in:
parent
eb5dd8b3fe
commit
5aae476618
5 changed files with 41 additions and 7 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -704,6 +704,7 @@ dependencies = [
|
|||
"prometheus-edge-detector 0.1.0 (git+https://github.com/icewind1991/prometheus-edge-detector)",
|
||||
"reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ toml = "0.5"
|
|||
log = "0.4"
|
||||
env_logger = "0.7"
|
||||
err-derive = "0.2.1"
|
||||
serde_json = "1.0.45"
|
||||
|
||||
[dev-dependencies]
|
||||
maplit = "1.0.2"
|
||||
|
|
|
|||
|
|
@ -9,6 +9,12 @@ pub enum ParameterError {
|
|||
MdnsError(#[error(source)] mdns::Error),
|
||||
#[error(display = "requested mdns host not found")]
|
||||
MdnsHostNotFound,
|
||||
#[error(display = "error reading file: {}", _0)]
|
||||
FilesystemError(#[error(source)] std::io::Error),
|
||||
#[error(display = "malformed service file: {}", _0)]
|
||||
Service(#[error(source)] serde_json::Error),
|
||||
#[error(display = "requested service not found")]
|
||||
ServiceNotFound,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
|
|
@ -22,6 +28,11 @@ pub enum Parameter {
|
|||
Value {
|
||||
value: String,
|
||||
},
|
||||
Service {
|
||||
file: String,
|
||||
key: String,
|
||||
value: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl Parameter {
|
||||
|
|
@ -36,7 +47,19 @@ impl Parameter {
|
|||
None => Err(ParameterError::MdnsHostNotFound)
|
||||
}
|
||||
}
|
||||
Parameter::Value { value } => Ok(value.clone())
|
||||
Parameter::Value { value } => Ok(value.clone()),
|
||||
Parameter::Service {
|
||||
file, key, value
|
||||
} => {
|
||||
let content = tokio::fs::read(file).await?;
|
||||
let services: Vec<Service> = serde_json::from_slice(&content)?;
|
||||
services.into_iter().find_map(|service| {
|
||||
service.labels.get(key)
|
||||
.filter(|val| *val == value)
|
||||
.and_then(|_| service.targets.get(0))
|
||||
.cloned()
|
||||
}).ok_or(ParameterError::ServiceNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -82,4 +105,10 @@ pub enum Method {
|
|||
Get,
|
||||
Put,
|
||||
Post,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Service {
|
||||
targets: Vec<String>,
|
||||
labels: HashMap<String, String>,
|
||||
}
|
||||
|
|
@ -12,7 +12,10 @@ mod trigger;
|
|||
async fn main() -> Result<(), MainError> {
|
||||
env_logger::init();
|
||||
|
||||
if let Some(path) = std::env::args().nth(1) {
|
||||
let mut args = std::env::args();
|
||||
let bin = args.next().unwrap();
|
||||
|
||||
if let Some(path) = args.next() {
|
||||
let mut file = File::open(path).await?;
|
||||
|
||||
let mut contents = vec![];
|
||||
|
|
@ -22,7 +25,7 @@ async fn main() -> Result<(), MainError> {
|
|||
|
||||
Ok(trigger_manager.run_triggers().await?)
|
||||
} else {
|
||||
println!("Usage {} config.toml", std::env::args().next().unwrap());
|
||||
println!("Usage {} config.toml", bin);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ use futures_util::future::try_join_all;
|
|||
use std::time::{Duration, SystemTime};
|
||||
use reqwest::Client;
|
||||
use tokio::time::delay_for;
|
||||
use log::{info, warn};
|
||||
use log::{info, error};
|
||||
use err_derive::Error;
|
||||
|
||||
pub struct TriggerManager {
|
||||
|
|
@ -70,12 +70,12 @@ impl TriggerManager {
|
|||
Ok(Some(new_edge)) if new_edge == edge => {
|
||||
info!("[{}] Edge still valid, triggering", trigger.name);
|
||||
if let Err(e) = run_action(&trigger.action, &self.http_client).await {
|
||||
warn!("[{}]: {}", trigger.name, e);
|
||||
error!("[{}]: {}", trigger.name, e);
|
||||
}
|
||||
delay_for(delay_duration).await;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("[{}]: {}", trigger.name, e);
|
||||
error!("[{}]: {}", trigger.name, e);
|
||||
delay_for(error_delay).await;
|
||||
}
|
||||
_ => {
|
||||
|
|
@ -88,7 +88,7 @@ impl TriggerManager {
|
|||
delay_for(delay_duration).await;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("[{}]: {}", trigger.name, e);
|
||||
error!("[{}]: {}", trigger.name, e);
|
||||
delay_for(error_delay).await;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue