-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce "Webhook" sink (#51)
Introduces a new sink that outputs events to a remote endpoint using HTTP calls. Co-authored-by: Mark Stopka <mark.stopka@perlur.cloud>
- Loading branch information
1 parent
dfe7ce6
commit e47e021
Showing
11 changed files
with
216 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
pub mod terminal; | ||
|
||
#[cfg(feature = "webhook")] | ||
pub mod webhook; | ||
|
||
#[cfg(feature = "tuisink")] | ||
pub mod tui; | ||
|
||
|
This file contains 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 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,4 @@ | ||
mod run; | ||
mod setup; | ||
|
||
pub use setup::*; |
This file contains 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,77 @@ | ||
use std::time::Duration; | ||
|
||
use reqwest::blocking::Client; | ||
use serde::Serialize; | ||
|
||
use crate::framework::{Error, Event, StageReceiver}; | ||
|
||
use super::ErrorPolicy; | ||
|
||
#[derive(Serialize)] | ||
struct RequestBody { | ||
#[serde(flatten)] | ||
event: Event, | ||
variant: String, | ||
timestamp: Option<u64>, | ||
} | ||
|
||
impl From<Event> for RequestBody { | ||
fn from(event: Event) -> Self { | ||
let timestamp = event.context.timestamp.map(|x| x * 1000); | ||
let variant = event.data.to_string(); | ||
|
||
RequestBody { | ||
event, | ||
timestamp, | ||
variant, | ||
} | ||
} | ||
} | ||
|
||
fn execute_fallible_request( | ||
client: &Client, | ||
url: &str, | ||
body: &RequestBody, | ||
policy: &ErrorPolicy, | ||
retry_quota: usize, | ||
backoff_delay: Duration, | ||
) -> Result<(), Error> { | ||
let request = client.post(url).json(body).build()?; | ||
|
||
let result = client | ||
.execute(request) | ||
.and_then(|res| res.error_for_status()); | ||
|
||
match (result, policy, retry_quota) { | ||
(Ok(_), _, _) => { | ||
log::info!("successful http request to webhook"); | ||
Ok(()) | ||
} | ||
(Err(x), ErrorPolicy::Exit, 0) => Err(x.into()), | ||
(Err(x), ErrorPolicy::Continue, 0) => { | ||
log::warn!("failed to send webhook request: {:?}", x); | ||
Ok(()) | ||
} | ||
(Err(x), _, quota) => { | ||
log::warn!("failed attempt to execute webhook request: {:?}", x); | ||
std::thread::sleep(backoff_delay); | ||
execute_fallible_request(client, url, body, policy, quota - 1, backoff_delay) | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn request_loop( | ||
input: StageReceiver, | ||
client: &Client, | ||
url: &str, | ||
error_policy: &ErrorPolicy, | ||
max_retries: usize, | ||
backoff_delay: Duration, | ||
) -> Result<(), Error> { | ||
loop { | ||
let event = input.recv().unwrap(); | ||
let body = RequestBody::from(event); | ||
|
||
execute_fallible_request(client, url, &body, error_policy, max_retries, backoff_delay)?; | ||
} | ||
} |
This file contains 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,91 @@ | ||
use std::{collections::HashMap, time::Duration}; | ||
|
||
use reqwest::header::{self, HeaderMap, HeaderName, HeaderValue}; | ||
use serde_derive::Deserialize; | ||
|
||
use crate::framework::{BootstrapResult, Error, SinkConfig, StageReceiver}; | ||
|
||
use super::run::request_loop; | ||
|
||
static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); | ||
|
||
#[derive(Debug, Deserialize, Clone)] | ||
pub enum ErrorPolicy { | ||
Continue, | ||
Exit, | ||
} | ||
|
||
#[derive(Default, Debug, Deserialize)] | ||
pub struct Config { | ||
url: String, | ||
authorization: Option<String>, | ||
headers: Option<HashMap<String, String>>, | ||
timeout: Option<u64>, | ||
error_policy: Option<ErrorPolicy>, | ||
max_retries: Option<usize>, | ||
backoff_delay: Option<u64>, | ||
} | ||
|
||
fn build_headers_map(config: &Config) -> Result<HeaderMap, Error> { | ||
let mut headers = HeaderMap::new(); | ||
|
||
headers.insert( | ||
header::CONTENT_TYPE, | ||
HeaderValue::try_from("application/json")?, | ||
); | ||
|
||
if let Some(auth_value) = &config.authorization { | ||
let auth_value = HeaderValue::try_from(auth_value)?; | ||
headers.insert(header::AUTHORIZATION, auth_value); | ||
} | ||
|
||
if let Some(custom) = &config.headers { | ||
for (name, value) in custom.iter() { | ||
let name = HeaderName::try_from(name)?; | ||
let value = HeaderValue::try_from(value)?; | ||
headers.insert(name, value); | ||
} | ||
} | ||
|
||
Ok(headers) | ||
} | ||
|
||
const DEFAULT_MAX_RETRIES: usize = 20; | ||
const DEFAULT_BACKOFF_DELAY: u64 = 5_000; | ||
|
||
impl SinkConfig for Config { | ||
fn bootstrap(&self, input: StageReceiver) -> BootstrapResult { | ||
let client = reqwest::blocking::ClientBuilder::new() | ||
.user_agent(APP_USER_AGENT) | ||
.default_headers(build_headers_map(self)?) | ||
.timeout(Duration::from_millis(self.timeout.unwrap_or(30000))) | ||
.build()?; | ||
|
||
let url = self.url.clone(); | ||
|
||
let error_policy = self | ||
.error_policy | ||
.as_ref() | ||
.cloned() | ||
.unwrap_or(ErrorPolicy::Exit); | ||
|
||
let max_retries = self.max_retries.unwrap_or(DEFAULT_MAX_RETRIES); | ||
|
||
let backoff_delay = | ||
Duration::from_millis(self.backoff_delay.unwrap_or(DEFAULT_BACKOFF_DELAY)); | ||
|
||
let handle = std::thread::spawn(move || { | ||
request_loop( | ||
input, | ||
&client, | ||
&url, | ||
&error_policy, | ||
max_retries, | ||
backoff_delay, | ||
) | ||
.expect("request loop failed") | ||
}); | ||
|
||
Ok(handle) | ||
} | ||
} |