Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(events): add basic event handler to collect application events (#2602)
Sampras Lopes5d88dbc92ce4
diff --git a/crates/router/src/events.rs b/crates/router/src/events.rsnew file mode 100644index 0000000000..f573a4970b--- /dev/null+++ b/crates/router/src/events.rs@@ -0,0 +1,25 @@+use serde::Serialize;++pub mod event_logger;++pub trait EventHandler: Sync + Send + dyn_clone::DynClone {+ fn log_event<T: Event>(&self, event: T, previous: Option<T>);+}++#[derive(Debug, Serialize)]+#[serde(rename_all = "snake_case")]+pub enum EventType {+ PaymentIntent,+ PaymentAttempt,+ Refund,+ ApiLogs,+}++pub trait Event+where+ Self: Serialize,+{+ fn event_type() -> EventType;++ fn key(&self) -> String;+}diff --git a/crates/router/src/events/event_logger.rs b/crates/router/src/events/event_logger.rsnew file mode 100644index 0000000000..d8254b2cc4--- /dev/null+++ b/crates/router/src/events/event_logger.rs@@ -0,0 +1,15 @@+use super::{Event, EventHandler};+use crate::services::logger;++#[derive(Clone, Debug, Default)]+pub struct EventLogger {}++impl EventHandler for EventLogger {+ fn log_event<T: Event>(&self, event: T, previous: Option<T>) {+ if let Some(prev) = previous {+ logger::info!(previous = ?serde_json::to_string(&prev).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), current = ?serde_json::to_string(&event).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), event_type =? T::event_type(), event_id =? event.key(), log_type = "event");+ } else {+ logger::info!(current = ?serde_json::to_string(&event).unwrap_or(r#"{ "error": "Serialization failed" }"#.to_string()), event_type =? T::event_type(), event_id =? event.key(), log_type = "event");+ }+ }+}diff --git a/crates/router/src/lib.rs b/crates/router/src/lib.rsindex 5cea2a0123..11efec6405 100644--- a/crates/router/src/lib.rs+++ b/crates/router/src/lib.rs@@ -15,6 +15,7 @@ pub(crate) mod macros;pub mod routes;pub mod workflows;+pub mod events;pub mod middleware;pub mod openapi;pub mod services;diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rsindex 7f18220438..5ebafe9a66 100644--- a/crates/router/src/routes/app.rs+++ b/crates/router/src/routes/app.rs@@ -25,15 +25,17 @@ use super::{ephemeral_key::*, payment_methods::*, webhooks::*};use crate::{configs::settings,db::{StorageImpl, StorageInterface},+ events::{event_logger::EventLogger, EventHandler},routes::cards_info::card_iin_info,services::get_store,};#[derive(Clone)]-pub struct AppState {+pub struct AppStateBase<E: EventHandler> {pub flow_name: String,pub store: Box<dyn StorageInterface>,pub conf: Arc<settings::Settings>,+ pub event_handler: E,#[cfg(feature = "email")]pub email_client: Arc<dyn EmailClient>,#[cfg(feature = "kms")]@@ -41,6 +43,8 @@ pub struct AppState {pub api_client: Box<dyn crate::services::ApiClient>,}+pub type AppState = AppStateBase<EventLogger>;+impl scheduler::SchedulerAppState for AppState {fn get_db(&self) -> Box<dyn SchedulerInterface> {self.store.get_scheduler_db()@@ -48,8 +52,10 @@ impl scheduler::SchedulerAppState for AppState {}pub trait AppStateInfo {+ type Event: EventHandler;fn conf(&self) -> settings::Settings;fn store(&self) -> Box<dyn StorageInterface>;+ fn event_handler(&self) -> &Self::Event;#[cfg(feature = "email")]fn email_client(&self) -> Arc<dyn EmailClient>;fn add_request_id(&mut self, request_id: Option<String>);@@ -59,6 +65,7 @@ pub trait AppStateInfo {}impl AppStateInfo for AppState {+ type Event = EventLogger;fn conf(&self) -> settings::Settings {self.conf.as_ref().to_owned()}@@ -69,6 +76,9 @@ impl AppStateInfo for AppState {fn email_client(&self) -> Arc<dyn EmailClient> {self.email_client.to_owned()}+ fn event_handler(&self) -> &Self::Event {+ &self.event_handler+ }fn add_request_id(&mut self, request_id: Option<String>) {self.api_client.add_request_id(request_id);}@@ -137,6 +147,7 @@ impl AppState {#[cfg(feature = "kms")]kms_secrets: Arc::new(kms_secrets),api_client,+ event_handler: EventLogger::default(),}}