Skip to content

Commit

Permalink
Merge branch 'main' into propogate_error_in_tokenization
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjunKarthik authored Oct 16, 2023
2 parents ef0ef60 + 5d88dbc commit e05a89b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
25 changes: 25 additions & 0 deletions crates/router/src/events.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions crates/router/src/events/event_logger.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
1 change: 1 addition & 0 deletions crates/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 12 additions & 1 deletion crates/router/src/routes/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,37 @@ 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")]
pub kms_secrets: Arc<settings::ActiveKmsSecrets>,
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()
}
}

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>);
Expand All @@ -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()
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -137,6 +147,7 @@ impl AppState {
#[cfg(feature = "kms")]
kms_secrets: Arc::new(kms_secrets),
api_client,
event_handler: EventLogger::default(),
}
}

Expand Down

0 comments on commit e05a89b

Please sign in to comment.