-
-
Notifications
You must be signed in to change notification settings - Fork 148
feat(alerts): foundational implementation #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
12aff33
Alerting foundations
de-sh 952fd45
Improve alerts implementation with suggestions
de-sh 54c9c1a
make post requests to webhook/targets
de-sh 4b09fb0
Add license header
de-sh 02a93c9
Merge branch 'main' into alerting
de-sh 5ea9d80
Fix merge issues
de-sh 9d530a8
fmt
de-sh ced4865
Change type of value to serde json value
nitisht 420e614
Fix mutability issues that failed alerts
de-sh 0e685a1
Merge remote-tracking branch 'up/main' into alerting
de-sh 5999b3d
clean up
de-sh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Parseable Server (C) 2022 Parseable, Inc. | ||
* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
use log::{error, info}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::error::Error; | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Alerts { | ||
pub alerts: Vec<Alert>, | ||
} | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Alert { | ||
pub name: String, | ||
pub message: String, | ||
pub rule: Rule, | ||
pub targets: Vec<Target>, | ||
} | ||
|
||
impl Alert { | ||
// TODO: spawn async tasks to call webhooks if alert rules are met | ||
// This is done to ensure that threads aren't blocked by calls to the webhook | ||
pub async fn check_alert(&mut self, event: &serde_json::Value) -> Result<(), Error> { | ||
if self.rule.resolves(event).await { | ||
info!("Alert triggered; name: {}", self.name); | ||
for target in self.targets.clone() { | ||
let msg = self.message.clone(); | ||
actix_web::rt::spawn(async move { | ||
target.call(&msg); | ||
}); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Rule { | ||
pub field: String, | ||
/// Field that determines what comparison operator is to be used | ||
#[serde(default)] | ||
pub operator: Operator, | ||
pub value: serde_json::Value, | ||
pub repeats: u32, | ||
#[serde(skip)] | ||
repeated: u32, | ||
pub within: String, | ||
} | ||
|
||
impl Rule { | ||
// TODO: utilise `within` to set a range for validity of rule to trigger alert | ||
pub async fn resolves(&mut self, event: &serde_json::Value) -> bool { | ||
let comparison = match self.operator { | ||
Operator::EqualTo => event.get(&self.field).unwrap() == &self.value, | ||
// TODO: currently this is a hack, ensure checks are performed in the right way | ||
Operator::GreaterThan => { | ||
event.get(&self.field).unwrap().as_f64().unwrap() > (self.value).as_f64().unwrap() | ||
} | ||
Operator::LessThan => { | ||
event.get(&self.field).unwrap().as_f64().unwrap() < (self.value).as_f64().unwrap() | ||
} | ||
}; | ||
|
||
// If truthy, increment count of repeated | ||
if comparison { | ||
self.repeated += 1; | ||
} | ||
|
||
// If enough repetitions made, return true | ||
if self.repeated >= self.repeats { | ||
self.repeated = 0; | ||
return true; | ||
} | ||
|
||
false | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub enum Operator { | ||
EqualTo, | ||
GreaterThan, | ||
LessThan, | ||
} | ||
|
||
impl Default for Operator { | ||
fn default() -> Self { | ||
Self::EqualTo | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Target { | ||
pub name: String, | ||
#[serde(rename = "server_url")] | ||
pub server_url: String, | ||
#[serde(rename = "api_key")] | ||
pub api_key: String, | ||
} | ||
|
||
impl Target { | ||
pub fn call(&self, msg: &str) { | ||
if let Err(e) = ureq::post(&self.server_url) | ||
.set("Content-Type", "text/plain; charset=iso-8859-1") | ||
.set("X-API-Key", &self.api_key) | ||
.send_string(msg) | ||
{ | ||
error!("Couldn't make call to webhook, error: {}", e) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.