Skip to content

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

feat(connector_events): added api to fetch connector event logs (#3319)

ivor-juspay68a3a280676c
diff --git a/crates/analytics/src/clickhouse.rs b/crates/analytics/src/clickhouse.rs
index b8fd5e6a35..f81c29c801 100644
--- a/crates/analytics/src/clickhouse.rs
+++ b/crates/analytics/src/clickhouse.rs
@@ -21,6 +21,7 @@ use crate::{
filters::ApiEventFilter,
metrics::{latency::LatencyAvg, ApiEventMetricRow},
},
+ connector_events::events::ConnectorEventsResult,
outgoing_webhook_event::events::OutgoingWebhookLogsResult,
sdk_events::events::SdkEventsResult,
types::TableEngine,
@@ -121,6 +122,7 @@ impl AnalyticsDataSource for ClickhouseClient {
}
AnalyticsCollection::SdkEvents => TableEngine::BasicTree,
AnalyticsCollection::ApiEvents => TableEngine::BasicTree,
+ AnalyticsCollection::ConnectorEvents => TableEngine::BasicTree,
AnalyticsCollection::OutgoingWebhookEvent => TableEngine::BasicTree,
}
}
@@ -147,6 +149,7 @@ impl super::sdk_events::events::SdkEventsFilterAnalytics for ClickhouseClient {}
impl super::api_event::events::ApiLogsFilterAnalytics for ClickhouseClient {}
impl super::api_event::filters::ApiEventFilterAnalytics for ClickhouseClient {}
impl super::api_event::metrics::ApiEventMetricAnalytics for ClickhouseClient {}
+impl super::connector_events::events::ConnectorEventLogAnalytics for ClickhouseClient {}
impl super::outgoing_webhook_event::events::OutgoingWebhookLogsFilterAnalytics
for ClickhouseClient
{
@@ -188,6 +191,18 @@ impl TryInto<SdkEventsResult> for serde_json::Value {
}
}
+impl TryInto<ConnectorEventsResult> for serde_json::Value {
+ type Error = Report<ParsingError>;
+
+ fn try_into(self) -> Result<ConnectorEventsResult, Self::Error> {
+ serde_json::from_value(self)
+ .into_report()
+ .change_context(ParsingError::StructParseFailure(
+ "Failed to parse ConnectorEventsResult in clickhouse results",
+ ))
+ }
+}
+
impl TryInto<PaymentMetricRow> for serde_json::Value {
type Error = Report<ParsingError>;
@@ -344,6 +359,7 @@ impl ToSql<ClickhouseClient> for AnalyticsCollection {
Self::SdkEvents => Ok("sdk_events_dist".to_string()),
Self::ApiEvents => Ok("api_audit_log".to_string()),
Self::PaymentIntent => Ok("payment_intents_dist".to_string()),
+ Self::ConnectorEvents => Ok("connector_events_audit".to_string()),
Self::OutgoingWebhookEvent => Ok("outgoing_webhook_events_audit".to_string()),
}
}
diff --git a/crates/analytics/src/connector_events.rs b/crates/analytics/src/connector_events.rs
new file mode 100644
index 0000000000..c7c31306a2
--- /dev/null
+++ b/crates/analytics/src/connector_events.rs
@@ -0,0 +1,5 @@
+mod core;
+pub mod events;
+pub trait ConnectorEventAnalytics: events::ConnectorEventLogAnalytics {}
+
+pub use self::core::connector_events_core;
diff --git a/crates/analytics/src/connector_events/core.rs b/crates/analytics/src/connector_events/core.rs
new file mode 100644
index 0000000000..15f841af5f
--- /dev/null
+++ b/crates/analytics/src/connector_events/core.rs
@@ -0,0 +1,27 @@
+use api_models::analytics::connector_events::ConnectorEventsRequest;
+use common_utils::errors::ReportSwitchExt;
+use error_stack::{IntoReport, ResultExt};
+
+use super::events::{get_connector_events, ConnectorEventsResult};
+use crate::{errors::AnalyticsResult, types::FiltersError, AnalyticsProvider};
+
+pub async fn connector_events_core(
+ pool: &AnalyticsProvider,
+ req: ConnectorEventsRequest,
+ merchant_id: String,
+) -> AnalyticsResult<Vec<ConnectorEventsResult>> {
+ let data = match pool {
+ AnalyticsProvider::Sqlx(_) => Err(FiltersError::NotImplemented(
+ "Connector Events not implemented for SQLX",
+ ))
+ .into_report()
+ .attach_printable("SQL Analytics is not implemented for Connector Events"),
+ AnalyticsProvider::Clickhouse(ckh_pool)
+ | AnalyticsProvider::CombinedSqlx(_, ckh_pool)
+ | AnalyticsProvider::CombinedCkh(_, ckh_pool) => {
+ get_connector_events(&merchant_id, req, ckh_pool).await
+ }
+ }
+ .switch()?;
+ Ok(data)
+}
diff --git a/crates/analytics/src/connector_events/events.rs b/crates/analytics/src/connector_events/events.rs
new file mode 100644
index 0000000000..096520777e
--- /dev/null
+++ b/crates/analytics/src/connector_events/events.rs
@@ -0,0 +1,63 @@
+use api_models::analytics::{
+ connector_events::{ConnectorEventsRequest, QueryType},
+ Granularity,
+};
+use common_utils::errors::ReportSwitchExt;
+use error_stack::ResultExt;
+use time::PrimitiveDateTime;
+
+use crate::{
+ query::{Aggregate, GroupByClause, QueryBuilder, ToSql, Window},
+ types::{AnalyticsCollection, AnalyticsDataSource, FiltersError, FiltersResult, LoadRow},
+};
+pub trait ConnectorEventLogAnalytics: LoadRow<ConnectorEventsResult> {}
+
+pub async fn get_connector_events<T>(
+ merchant_id: &String,
+ query_param: ConnectorEventsRequest,
+ pool: &T,
+) -> FiltersResult<Vec<ConnectorEventsResult>>
+where
+ T: AnalyticsDataSource + ConnectorEventLogAnalytics,
+ PrimitiveDateTime: ToSql<T>,
+ AnalyticsCollection: ToSql<T>,
+ Granularity: GroupByClause<T>,
+ Aggregate<&'static str>: ToSql<T>,
+ Window<&'static str>: ToSql<T>,
+{
+ let mut query_builder: QueryBuilder<T> =
+ QueryBuilder::new(AnalyticsCollection::ConnectorEvents);
+ query_builder.add_select_column("*").switch()?;
+
+ query_builder
+ .add_filter_clause("merchant_id", merchant_id)
+ .switch()?;
+ match query_param.query_param {
+ QueryType::Payment { payment_id } => query_builder
+ .add_filter_clause("payment_id", payment_id)
+ .switch()?,
+ }
+ //TODO!: update the execute_query function to return reports instead of plain errors...
+ query_builder
+ .execute_query::<ConnectorEventsResult, _>(pool)
+ .await
+ .change_context(FiltersError::QueryBuildingError)?
+ .change_context(FiltersError::QueryExecutionFailure)
+}
+
+#[derive(Debug, serde::Serialize, serde::Deserialize)]
+pub struct ConnectorEventsResult {
+ pub merchant_id: String,
+ pub payment_id: String,
+ pub connector_name: Option<String>,
+ pub request_id: Option<String>,
+ pub flow: String,
+ pub request: String,
+ pub response: Option<String>,
+ pub error: Option<String>,
+ pub status_code: u16,
+ pub latency: Option<u128>,
+ #[serde(with = "common_utils::custom_serde::iso8601")]
+ pub created_at: PrimitiveDateTime,
+ pub method: Option<String>,
+}
diff --git a/crates/analytics/src/lib.rs b/crates/analytics/src/lib.rs
index 8529807a1a..501bd58527 100644
--- a/crates/analytics/src/lib.rs
+++ b/crates/analytics/src/lib.rs
@@ -7,6 +7,7 @@ mod query;
pub mod refunds;
pub mod api_event;
+pub mod connector_events;
pub mod outgoing_webhook_event;
pub mod sdk_events;
mod sqlx;
diff --git a/crates/analytics/src/sqlx.rs b/crates/analytics/src/sqlx.rs
index e32b85a536..7ab8a2aa4b 100644
--- a/crates/analytics/src/sqlx.rs
+++ b/crates/analytics/src/sqlx.rs
@@ -429,6 +429,8 @@ impl ToSql<SqlxClient> for AnalyticsCollection {
Self::ApiEvents => Err(error_stack::report!(ParsingError::UnknownError)
.attach_printable("ApiEvents table is not implemented for Sqlx"))?,
Self::PaymentIntent => Ok("payment_intent".to_string()),
+ Self::ConnectorEvents => Err(error_stack::report!(ParsingError::UnknownError)
+ .attach_printable("ConnectorEvents table is not implemented for Sqlx"))?,
Self::OutgoingWebhookEvent => Err(error_stack::report!(ParsingError::UnknownError)
.attach_printable("OutgoingWebhookEvents table is not implemented for Sqlx"))?,
}
diff --git a/crates/analytics/src/types.rs b/crates/analytics/src/types.rs
index 8da4655e25..18e9e9f433 100644
--- a/crates/analytics/src/types.rs
+++ b/crates/analytics/src/types.rs
@@ -26,6 +26,7 @@ pub enum AnalyticsCollection {
SdkEvents,
ApiEvents,
PaymentIntent,
+ ConnectorEvents,
OutgoingWebhookEvent,
}
diff --git a/crates/api_models/src/analytics.rs b/crates/api_models/src/analytics.rs
index e0d3fa671b..c6ca215f9f 100644
--- a/crates/api_models/src/analytics.rs
+++ b/crates/api_models/src/analytics.rs
@@ -12,6 +12,7 @@ use self::{
pub use crate::payments::TimeRange;
pub mod api_event;
+pub mod connector_events;
pub mod outgoing_webhook_event;
pub mod payments;
pub mod refunds;
diff --git a/crates/api_models/src/analytics/connector_events.rs b/crates/api_models/src/analytics/connector_events.rs
new file mode 100644
index 0000000000..b2974b0a33
--- /dev/null
+++ b/crates/api_models/src/analytics/connector_events.rs
@@ -0,0 +1,11 @@
+#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
+#[serde(tag = "type")]
+pub enum QueryType {
+ Payment { payment_id: String },
+}
+
+#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
+pub struct ConnectorEventsRequest {
+ #[serde(flatten)]
+ pub query_param: QueryType,
+}
diff --git a/crates/api_models/src/events.rs b/crates/api_models/src/events.rs
index 26a9d222d6..43a72b7e39 100644
--- a/crates/api_models/src/events.rs
+++ b/crates/api_models/src/events.rs
@@ -17,10 +17,12 @@ use common_utils::{
impl_misc_api_event_type,
};
+#[allow(unused_imports)]
use crate::{
admin::*,
analytics::{
- api_event::*, outgoing_webhook_event::OutgoingWebhookLogsRequest, sdk_events::*, *,
+ api_event::*, connector_events::ConnectorEventsRequest,
+ outgoing_webhook_event::OutgoingWebhookLogsRequest, sdk_events::*, *,
},
api_keys::*,
cards_info::*,
@@ -94,6 +96,7 @@ impl_misc_api_event_type!(
GetApiEventMetricRequest,
SdkEventsRequest,
ReportRequest,
+ ConnectorEventsRequest,
OutgoingWebhookLogsRequest
);
diff --git a/crates/router/src/analytics.rs b/crates/router/src/analytics.rs
index c62de5bd29..3f0febcc59 100644
--- a/crates/router/src/analytics.rs
+++ b/crates/router/src/analytics.rs
@@ -3,7 +3,8 @@ pub use analytics::*;
pub mod routes {
use actix_web::{web, Responder, Scope};
use analytics::{
- api_event::api_events_core, errors::AnalyticsError, lambda_utils::invoke_lambda,
+ api_event::api_events_core, connector_events::connector_events_core,
+ errors::AnalyticsError, lambda_utils::invoke_lambda,
outgoing_webhook_event::outgoing_webhook_events_core, sdk_events::sdk_events_core,
};
use api_models::analytics::{
@@ -71,6 +72,10 @@ pub mod routes {
)
.service(web::resource("api_event_logs").route(web::get().to(get_api_events)))
.service(web::resource("sdk_event_logs").route(web::post().to(get_sdk_events)))
+ .service(
+ web::resource("connector_event_logs")
+ .route(web::get().to(get_connector_events)),
+ )
.service(
web::resource("outgoing_webhook_event_logs")
.route(web::get().to(get_outgoing_webhook_events)),
@@ -585,4 +590,26 @@ pub mod routes {
))
.await
}
+
+ pub async fn get_connector_events(
+ state: web::Data<AppState>,
+ req: actix_web::HttpRequest,
+ json_payload: web::Query<api_models::analytics::connector_events::ConnectorEventsRequest>,
+ ) -> impl Responder {
+ let flow = AnalyticsFlow::GetConnectorEvents;
+ Box::pin(api::server_wrap(
+ flow,
+ state,
+ &req,
+ json_payload.into_inner(),
+ |state, auth: AuthenticationData, req| async move {
+ connector_events_core(&state.pool, req, auth.merchant_account.merchant_id)
+ .await
+ .map(ApplicationResponse::Json)
+ },
+ &auth::JWTAuth(Permission::Analytics),
+ api_locking::LockAction::NotApplicable,
+ ))
+ .await
+ }
}
diff --git a/crates/router_env/src/lib.rs b/crates/router_env/src/lib.rs
index 0127d07170..9139b5eed4 100644
--- a/crates/router_env/src/lib.rs
+++ b/crates/router_env/src/lib.rs
@@ -52,6 +52,7 @@ pub enum AnalyticsFlow {
GenerateRefundReport,
GetApiEventMetrics,
GetApiEventFilters,
+ GetConnectorEvents,
GetOutgoingWebhookEvents,
}