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(response-log): add logging to the response for stripe compatibility layer (#1470)

Shankar Singh C96c71e1b1bbf
diff --git a/crates/router/src/compatibility/stripe/customers.rs b/crates/router/src/compatibility/stripe/customers.rs
index b96cdd3375..40c55422c0 100644
--- a/crates/router/src/compatibility/stripe/customers.rs
+++ b/crates/router/src/compatibility/stripe/customers.rs
@@ -2,7 +2,7 @@ pub mod types;
use actix_web::{web, HttpRequest, HttpResponse};
use error_stack::report;
-use router_env::{instrument, tracing};
+use router_env::{instrument, tracing, Flow};
use crate::{
compatibility::{stripe::errors, wrap},
@@ -12,7 +12,7 @@ use crate::{
types::api::customers as customer_types,
};
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::CustomersCreate))]
pub async fn customer_create(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -28,6 +28,8 @@ pub async fn customer_create(
let create_cust_req: customer_types::CustomerRequest = payload.into();
+ let flow = Flow::CustomersCreate;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -38,6 +40,7 @@ pub async fn customer_create(
types::CreateCustomerResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
create_cust_req,
@@ -49,7 +52,7 @@ pub async fn customer_create(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::CustomersRetrieve))]
pub async fn customer_retrieve(
state: web::Data<routes::AppState>,
req: HttpRequest,
@@ -59,6 +62,8 @@ pub async fn customer_retrieve(
customer_id: path.into_inner(),
};
+ let flow = Flow::CustomersRetrieve;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -69,6 +74,7 @@ pub async fn customer_retrieve(
types::CustomerRetrieveResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -80,7 +86,7 @@ pub async fn customer_retrieve(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::CustomersUpdate))]
pub async fn customer_update(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -99,6 +105,8 @@ pub async fn customer_update(
let mut cust_update_req: customer_types::CustomerRequest = payload.into();
cust_update_req.customer_id = customer_id;
+ let flow = Flow::CustomersUpdate;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -109,6 +117,7 @@ pub async fn customer_update(
types::CustomerUpdateResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
cust_update_req,
@@ -120,7 +129,7 @@ pub async fn customer_update(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::CustomersDelete))]
pub async fn customer_delete(
state: web::Data<routes::AppState>,
req: HttpRequest,
@@ -130,6 +139,8 @@ pub async fn customer_delete(
customer_id: path.into_inner(),
};
+ let flow = Flow::CustomersDelete;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -140,6 +151,7 @@ pub async fn customer_delete(
types::CustomerDeleteResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -149,7 +161,7 @@ pub async fn customer_delete(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::CustomerPaymentMethodsList))]
pub async fn list_customer_payment_method_api(
state: web::Data<routes::AppState>,
req: HttpRequest,
@@ -157,6 +169,8 @@ pub async fn list_customer_payment_method_api(
) -> HttpResponse {
let customer_id = path.into_inner();
+ let flow = Flow::CustomerPaymentMethodsList;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -167,6 +181,7 @@ pub async fn list_customer_payment_method_api(
types::CustomerPaymentMethodListResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
customer_id.as_ref(),
diff --git a/crates/router/src/compatibility/stripe/payment_intents.rs b/crates/router/src/compatibility/stripe/payment_intents.rs
index f8d2b32c63..26fa136d09 100644
--- a/crates/router/src/compatibility/stripe/payment_intents.rs
+++ b/crates/router/src/compatibility/stripe/payment_intents.rs
@@ -3,7 +3,7 @@ pub mod types;
use actix_web::{web, HttpRequest, HttpResponse};
use api_models::payments as payment_types;
use error_stack::report;
-use router_env::{instrument, tracing};
+use router_env::{instrument, tracing, Flow};
use crate::{
compatibility::{stripe::errors, wrap},
@@ -13,7 +13,7 @@ use crate::{
types::api::{self as api_types},
};
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsCreate))]
pub async fn payment_intents_create(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -33,6 +33,8 @@ pub async fn payment_intents_create(
Err(err) => return api::log_and_return_error_response(err),
};
+ let flow = Flow::PaymentsCreate;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -43,6 +45,7 @@ pub async fn payment_intents_create(
types::StripePaymentIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
create_payment_req,
@@ -61,7 +64,7 @@ pub async fn payment_intents_create(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsRetrieve))]
pub async fn payment_intents_retrieve(
state: web::Data<routes::AppState>,
req: HttpRequest,
@@ -81,6 +84,8 @@ pub async fn payment_intents_retrieve(
Err(err) => return api::log_and_return_error_response(report!(err)),
};
+ let flow = Flow::PaymentsRetrieve;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -91,6 +96,7 @@ pub async fn payment_intents_retrieve(
types::StripePaymentIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -109,7 +115,7 @@ pub async fn payment_intents_retrieve(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsRetrieve))]
pub async fn payment_intents_retrieve_with_gateway_creds(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -138,6 +144,8 @@ pub async fn payment_intents_retrieve_with_gateway_creds(
Err(err) => return api::log_and_return_error_response(report!(err)),
};
+ let flow = Flow::PaymentsRetrieve;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -148,6 +156,7 @@ pub async fn payment_intents_retrieve_with_gateway_creds(
types::StripePaymentIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -166,7 +175,7 @@ pub async fn payment_intents_retrieve_with_gateway_creds(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsUpdate))]
pub async fn payment_intents_update(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -196,6 +205,8 @@ pub async fn payment_intents_update(
Err(err) => return api::log_and_return_error_response(report!(err)),
};
+ let flow = Flow::PaymentsUpdate;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -206,6 +217,7 @@ pub async fn payment_intents_update(
types::StripePaymentIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -224,7 +236,7 @@ pub async fn payment_intents_update(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsConfirm))]
pub async fn payment_intents_confirm(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -256,6 +268,8 @@ pub async fn payment_intents_confirm(
Err(err) => return api::log_and_return_error_response(err),
};
+ let flow = Flow::PaymentsConfirm;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -266,6 +280,7 @@ pub async fn payment_intents_confirm(
types::StripePaymentIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -284,6 +299,7 @@ pub async fn payment_intents_confirm(
.await
}
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsCapture))]
pub async fn payment_intents_capture(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -305,6 +321,8 @@ pub async fn payment_intents_capture(
..stripe_payload
};
+ let flow = Flow::PaymentsCapture;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -315,6 +333,7 @@ pub async fn payment_intents_capture(
types::StripePaymentIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
capture_payload,
@@ -333,7 +352,7 @@ pub async fn payment_intents_capture(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsCancel))]
pub async fn payment_intents_cancel(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -359,6 +378,8 @@ pub async fn payment_intents_cancel(
Err(err) => return api::log_and_return_error_response(report!(err)),
};
+ let flow = Flow::PaymentsCancel;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -369,6 +390,7 @@ pub async fn payment_intents_cancel(
types::StripePaymentIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -387,7 +409,7 @@ pub async fn payment_intents_cancel(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsList))]
#[cfg(feature = "olap")]
pub async fn payment_intent_list(
state: web::Data<routes::AppState>,
@@ -398,6 +420,9 @@ pub async fn payment_intent_list(
Ok(p) => p,
Err(err) => return api::log_and_return_error_response(err),
};
+
+ let flow = Flow::PaymentsList;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -408,6 +433,7 @@ pub async fn payment_intent_list(
types::StripePaymentIntentListResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
diff --git a/crates/router/src/compatibility/stripe/refunds.rs b/crates/router/src/compatibility/stripe/refunds.rs
index a42f31cc17..b288a72485 100644
--- a/crates/router/src/compatibility/stripe/refunds.rs
+++ b/crates/router/src/compatibility/stripe/refunds.rs
@@ -2,7 +2,7 @@ pub mod types;
use actix_web::{web, HttpRequest, HttpResponse};
use error_stack::report;
-use router_env::{instrument, tracing};
+use router_env::{instrument, tracing, Flow};
use crate::{
compatibility::{stripe::errors, wrap},
@@ -12,7 +12,7 @@ use crate::{
types::api::refunds as refund_types,
};
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::RefundsCreate))]
pub async fn refund_create(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -29,6 +29,8 @@ pub async fn refund_create(
let create_refund_req: refund_types::RefundRequest = payload.into();
+ let flow = Flow::RefundsCreate;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -39,6 +41,7 @@ pub async fn refund_create(
types::StripeRefundResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
create_refund_req,
@@ -48,7 +51,7 @@ pub async fn refund_create(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::RefundsRetrieve))]
pub async fn refund_retrieve_with_gateway_creds(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -62,6 +65,9 @@ pub async fn refund_retrieve_with_gateway_creds(
Ok(payload) => payload,
Err(err) => return api::log_and_return_error_response(err),
};
+
+ let flow = Flow::RefundsRetrieve;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -72,6 +78,7 @@ pub async fn refund_retrieve_with_gateway_creds(
types::StripeRefundResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
refund_request,
@@ -88,7 +95,7 @@ pub async fn refund_retrieve_with_gateway_creds(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::RefundsRetrieve))]
pub async fn refund_retrieve(
state: web::Data<routes::AppState>,
req: HttpRequest,
@@ -99,6 +106,9 @@ pub async fn refund_retrieve(
force_sync: Some(true),
merchant_connector_details: None,
};
+
+ let flow = Flow::RefundsRetrieve;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -109,6 +119,7 @@ pub async fn refund_retrieve(
types::StripeRefundResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
refund_request,
@@ -125,7 +136,7 @@ pub async fn refund_retrieve(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::RefundsUpdate))]
pub async fn refund_update(
state: web::Data<routes::AppState>,
req: HttpRequest,
@@ -135,6 +146,7 @@ pub async fn refund_update(
let refund_id = path.into_inner();
let payload = form_payload.into_inner();
let create_refund_update_req: refund_types::RefundUpdateRequest = payload.into();
+ let flow = Flow::RefundsUpdate;
wrap::compatibility_api_wrap::<
_,
@@ -146,6 +158,7 @@ pub async fn refund_update(
types::StripeRefundResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
create_refund_update_req,
diff --git a/crates/router/src/compatibility/stripe/setup_intents.rs b/crates/router/src/compatibility/stripe/setup_intents.rs
index d907c45d14..1b9a2fb90b 100644
--- a/crates/router/src/compatibility/stripe/setup_intents.rs
+++ b/crates/router/src/compatibility/stripe/setup_intents.rs
@@ -3,7 +3,7 @@ pub mod types;
use actix_web::{web, HttpRequest, HttpResponse};
use api_models::payments as payment_types;
use error_stack::report;
-use router_env::{instrument, tracing};
+use router_env::{instrument, tracing, Flow};
use crate::{
compatibility::{stripe::errors, wrap},
@@ -13,7 +13,7 @@ use crate::{
types::api as api_types,
};
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsCreate))]
pub async fn setup_intents_create(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -34,6 +34,8 @@ pub async fn setup_intents_create(
Err(err) => return api::log_and_return_error_response(err),
};
+ let flow = Flow::PaymentsCreate;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -44,6 +46,7 @@ pub async fn setup_intents_create(
types::StripeSetupIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
create_payment_req,
@@ -62,7 +65,7 @@ pub async fn setup_intents_create(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsRetrieve))]
pub async fn setup_intents_retrieve(
state: web::Data<routes::AppState>,
req: HttpRequest,
@@ -82,6 +85,8 @@ pub async fn setup_intents_retrieve(
Err(err) => return api::log_and_return_error_response(report!(err)),
};
+ let flow = Flow::PaymentsRetrieve;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -92,6 +97,7 @@ pub async fn setup_intents_retrieve(
types::StripeSetupIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -110,7 +116,7 @@ pub async fn setup_intents_retrieve(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsUpdate))]
pub async fn setup_intents_update(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -141,6 +147,8 @@ pub async fn setup_intents_update(
Err(err) => return api::log_and_return_error_response(err),
};
+ let flow = Flow::PaymentsUpdate;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -151,6 +159,7 @@ pub async fn setup_intents_update(
types::StripeSetupIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
@@ -169,7 +178,7 @@ pub async fn setup_intents_update(
.await
}
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(flow = ?Flow::PaymentsConfirm))]
pub async fn setup_intents_confirm(
state: web::Data<routes::AppState>,
qs_config: web::Data<serde_qs::Config>,
@@ -201,6 +210,8 @@ pub async fn setup_intents_confirm(
Err(err) => return api::log_and_return_error_response(err),
};
+ let flow = Flow::PaymentsConfirm;
+
wrap::compatibility_api_wrap::<
_,
_,
@@ -211,6 +222,7 @@ pub async fn setup_intents_confirm(
types::StripeSetupIntentResponse,
errors::StripeErrorCode,
>(
+ flow,
state.get_ref(),
&req,
payload,
diff --git a/crates/router/src/compatibility/wrap.rs b/crates/router/src/compatibility/wrap.rs
index 19e6c4f296..9e0fbb5399 100644
--- a/crates/router/src/compatibility/wrap.rs
+++ b/crates/router/src/compatibility/wrap.rs
@@ -1,18 +1,19 @@
-use std::future::Future;
+use std::{future::Future, time::Instant};
use actix_web::{HttpRequest, HttpResponse, Responder};
use common_utils::errors::ErrorSwitch;
-use router_env::{instrument, tracing};
+use router_env::{instrument, tracing, Tag};
use serde::Serialize;
use crate::{
core::errors::{self, RouterResult},
- routes::app::AppStateInfo,
+ routes::{app::AppStateInfo, metrics},
services::{self, api, authentication as auth, logger},
};
#[instrument(skip(request, payload, state, func, api_authentication))]
pub async fn compatibility_api_wrap<'a, 'b, A, U, T, Q, F, Fut, S, E>(
+ flow: impl router_env::types::FlowMetric,
state: &'b A,
request: &'a HttpRequest,
payload: T,
@@ -31,20 +32,27 @@ where
T: std::fmt::Debug,
A: AppStateInfo,
{
- let resp: common_utils::errors::CustomResult<_, E> = api::server_wrap_util(
- &router_env::Flow::CompatibilityLayerRequest,
- state,
- request,
- payload,
- func,
- api_authentication,
+ let request_method = request.method().as_str();
+ let url_path = request.path();
+ tracing::Span::current().record("request_method", request_method);
+ tracing::Span::current().record("request_url_path", url_path);
+
+ let start_instant = Instant::now();
+ logger::info!(tag = ?Tag::BeginRequest, payload = ?payload);
+
+ let res = match metrics::request::record_request_time_metric(
+ api::server_wrap_util(&flow, state, request, payload, func, api_authentication),
+ &flow,
)
- .await;
- match resp {
- Ok(api::ApplicationResponse::Json(router_resp)) => {
- let pg_resp = S::try_from(router_resp);
- match pg_resp {
- Ok(pg_resp) => match serde_json::to_string(&pg_resp) {
+ .await
+ .map(|response| {
+ logger::info!(api_response =? response);
+ response
+ }) {
+ Ok(api::ApplicationResponse::Json(response)) => {
+ let response = S::try_from(response);
+ match response {
+ Ok(response) => match serde_json::to_string(&response) {
Ok(res) => api::http_response_json(res),
Err(_) => api::http_response_err(
r#"{
@@ -88,10 +96,17 @@ where
)
.respond_to(request)
.map_into_boxed_body(),
+ Err(error) => api::log_and_return_error_response(error),
+ };
- Err(error) => {
- logger::error!(api_response_error=?error);
- api::log_and_return_error_response(error)
- }
- }
+ let response_code = res.status().as_u16();
+ let end_instant = Instant::now();
+ let request_duration = end_instant.saturating_duration_since(start_instant);
+ logger::info!(
+ tag = ?Tag::EndRequest,
+ status_code = response_code,
+ time_taken_ms = request_duration.as_millis(),
+ );
+
+ res
}
diff --git a/crates/router_env/src/logger/types.rs b/crates/router_env/src/logger/types.rs
index e47b5d1c5f..8ccfb73011 100644
--- a/crates/router_env/src/logger/types.rs
+++ b/crates/router_env/src/logger/types.rs
@@ -184,8 +184,6 @@ pub enum Flow {
AttachDisputeEvidence,
/// Retrieve Dispute Evidence flow
RetrieveDisputeEvidence,
- /// Request to compatibility layer
- CompatibilityLayerRequest,
}
///