mirror of
https://codeberg.org/icewind/prometheus-edge-trigger.git
synced 2026-06-03 18:24:10 +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)",
|
"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)",
|
"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 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)",
|
"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)",
|
"toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ toml = "0.5"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
env_logger = "0.7"
|
env_logger = "0.7"
|
||||||
err-derive = "0.2.1"
|
err-derive = "0.2.1"
|
||||||
|
serde_json = "1.0.45"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
maplit = "1.0.2"
|
maplit = "1.0.2"
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,12 @@ pub enum ParameterError {
|
||||||
MdnsError(#[error(source)] mdns::Error),
|
MdnsError(#[error(source)] mdns::Error),
|
||||||
#[error(display = "requested mdns host not found")]
|
#[error(display = "requested mdns host not found")]
|
||||||
MdnsHostNotFound,
|
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)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
|
@ -22,6 +28,11 @@ pub enum Parameter {
|
||||||
Value {
|
Value {
|
||||||
value: String,
|
value: String,
|
||||||
},
|
},
|
||||||
|
Service {
|
||||||
|
file: String,
|
||||||
|
key: String,
|
||||||
|
value: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parameter {
|
impl Parameter {
|
||||||
|
|
@ -36,7 +47,19 @@ impl Parameter {
|
||||||
None => Err(ParameterError::MdnsHostNotFound)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -83,3 +106,9 @@ pub enum Method {
|
||||||
Put,
|
Put,
|
||||||
Post,
|
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> {
|
async fn main() -> Result<(), MainError> {
|
||||||
env_logger::init();
|
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 file = File::open(path).await?;
|
||||||
|
|
||||||
let mut contents = vec![];
|
let mut contents = vec![];
|
||||||
|
|
@ -22,7 +25,7 @@ async fn main() -> Result<(), MainError> {
|
||||||
|
|
||||||
Ok(trigger_manager.run_triggers().await?)
|
Ok(trigger_manager.run_triggers().await?)
|
||||||
} else {
|
} else {
|
||||||
println!("Usage {} config.toml", std::env::args().next().unwrap());
|
println!("Usage {} config.toml", bin);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -6,7 +6,7 @@ use futures_util::future::try_join_all;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use tokio::time::delay_for;
|
use tokio::time::delay_for;
|
||||||
use log::{info, warn};
|
use log::{info, error};
|
||||||
use err_derive::Error;
|
use err_derive::Error;
|
||||||
|
|
||||||
pub struct TriggerManager {
|
pub struct TriggerManager {
|
||||||
|
|
@ -70,12 +70,12 @@ impl TriggerManager {
|
||||||
Ok(Some(new_edge)) if new_edge == edge => {
|
Ok(Some(new_edge)) if new_edge == edge => {
|
||||||
info!("[{}] Edge still valid, triggering", trigger.name);
|
info!("[{}] Edge still valid, triggering", trigger.name);
|
||||||
if let Err(e) = run_action(&trigger.action, &self.http_client).await {
|
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;
|
delay_for(delay_duration).await;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("[{}]: {}", trigger.name, e);
|
error!("[{}]: {}", trigger.name, e);
|
||||||
delay_for(error_delay).await;
|
delay_for(error_delay).await;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|
@ -88,7 +88,7 @@ impl TriggerManager {
|
||||||
delay_for(delay_duration).await;
|
delay_for(delay_duration).await;
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("[{}]: {}", trigger.name, e);
|
error!("[{}]: {}", trigger.name, e);
|
||||||
delay_for(error_delay).await;
|
delay_for(error_delay).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue