mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-02 04:04:43 +08:00
feat(events): add basic event handler to collect application events (#2602)
This commit is contained in:
25
crates/router/src/events.rs
Normal file
25
crates/router/src/events.rs
Normal file
@ -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;
|
||||
}
|
||||
15
crates/router/src/events/event_logger.rs
Normal file
15
crates/router/src/events/event_logger.rs
Normal file
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user