work on alerting logic

This commit is contained in:
mbecker20
2023-08-13 01:54:14 -04:00
parent 7aa048a512
commit f1639126df
11 changed files with 380 additions and 292 deletions

View File

@@ -1,6 +1,8 @@
#[macro_use]
extern crate log;
use std::time::Duration;
use anyhow::{anyhow, Context};
use reqwest::StatusCode;
use resolver_api::HasResponse;
@@ -24,18 +26,35 @@ impl PeripheryClient {
}
pub async fn request<T: HasResponse>(&self, request: T) -> anyhow::Result<T::Response> {
self.health_check().await?;
self.request_inner(request, None).await
}
pub async fn health_check(&self) -> anyhow::Result<()> {
self.request_inner(requests::GetHealth {}, Some(Duration::from_secs(1)))
.await?;
Ok(())
}
async fn request_inner<T: HasResponse>(
&self,
request: T,
timeout: Option<Duration>,
) -> anyhow::Result<T::Response> {
let req_type = T::req_type();
trace!("sending request | type: {req_type} | body: {request:?}");
let res = self
let mut req = self
.reqwest
.post(&self.address)
.json(&json!({
"type": req_type,
"params": request
}))
.header("authorization", &self.passkey)
.send()
.await?;
.header("authorization", &self.passkey);
if let Some(timeout) = timeout {
req = req.timeout(timeout);
}
let res = req.send().await?;
let status = res.status();
debug!("got response | type: {req_type} | {status} | body: {res:?}",);
if status == StatusCode::OK {
@@ -50,9 +69,4 @@ impl PeripheryClient {
Err(anyhow!("request failed | {status} | {text}"))
}
}
pub async fn health_check(&self) -> anyhow::Result<()> {
self.request(requests::GetHealth {}).await?;
Ok(())
}
}

View File

@@ -1,15 +1,49 @@
use derive_variants::EnumVariants;
use mungos::{
derive::{MungosIndexed, StringObjectId},
mongodb::bson::{doc, serde_helpers::hex_string_as_object_id},
};
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use crate::{MongoId, I64};
use super::{
deployment::DockerContainerState,
server::stats::{StatsState, SystemProcess},
update::ResourceTarget,
};
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, EnumVariants)]
#[variant_derive(Serialize, Deserialize, Debug, Clone, Copy)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, MungosIndexed, StringObjectId)]
#[doc_index(doc! { "target.type": 1 })]
#[sparse_doc_index(doc! { "target.id": 1 })]
pub struct AlertRecord {
#[serde(
default,
rename = "_id",
skip_serializing_if = "String::is_empty",
with = "hex_string_as_object_id"
)]
pub id: MongoId,
#[index]
pub start_ts: I64,
#[index]
pub resolved: bool,
#[index]
pub alert_type: AlertVariant,
pub target: ResourceTarget,
pub alert: Alert,
pub resolved_ts: Option<I64>,
}
#[typeshare]
#[derive(Serialize, Deserialize, Debug, Clone, EnumVariants, MungosIndexed)]
#[variant_derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[serde(tag = "type", content = "data")]
pub enum Alert {
ServerUnreachable {
@@ -58,4 +92,18 @@ pub enum Alert {
from: DockerContainerState,
to: DockerContainerState,
},
None {},
}
impl Default for Alert {
fn default() -> Self {
Alert::None {}
}
}
#[allow(clippy::derivable_impls)]
impl Default for AlertVariant {
fn default() -> Self {
AlertVariant::None
}
}