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

fix(chat): append request id to headers for chat request (#8680)

Apoorv Dixit3113d2b5dc3a
diff --git a/crates/common_utils/src/consts.rs b/crates/common_utils/src/consts.rs
index 3ef0ef555e..28508749f3 100644
--- a/crates/common_utils/src/consts.rs
+++ b/crates/common_utils/src/consts.rs
@@ -152,6 +152,9 @@ pub const APPLEPAY_VALIDATION_URL: &str =
/// Request ID
pub const X_REQUEST_ID: &str = "x-request-id";
+/// Chat Session ID
+pub const X_CHAT_SESSION_ID: &str = "x-chat-session-id";
+
/// Merchant ID Header
pub const X_MERCHANT_ID: &str = "x-merchant-id";
diff --git a/crates/router/src/core/chat.rs b/crates/router/src/core/chat.rs
index c30e27f048..837a259bed 100644
--- a/crates/router/src/core/chat.rs
+++ b/crates/router/src/core/chat.rs
@@ -11,18 +11,19 @@ use router_env::{instrument, logger, tracing};
use crate::{
db::errors::chat::ChatErrors,
- routes::SessionState,
+ routes::{app::SessionStateInfo, SessionState},
services::{authentication as auth, ApplicationResponse},
};
-#[instrument(skip_all)]
+#[instrument(skip_all, fields(?session_id))]
pub async fn get_data_from_hyperswitch_ai_workflow(
state: SessionState,
user_from_token: auth::UserFromToken,
req: chat_api::ChatRequest,
+ session_id: Option<&str>,
) -> CustomResult<ApplicationResponse<chat_api::ChatResponse>, ChatErrors> {
let url = format!("{}/webhook", state.conf.chat.hyperswitch_ai_host);
-
+ let request_id = state.get_request_id();
let request_body = chat_domain::HyperswitchAiDataRequest {
query: chat_domain::GetDataMessage {
message: req.message,
@@ -33,12 +34,20 @@ pub async fn get_data_from_hyperswitch_ai_workflow(
};
logger::info!("Request for AI service: {:?}", request_body);
- let request = RequestBuilder::new()
+ let mut request_builder = RequestBuilder::new()
.method(Method::Post)
.url(&url)
.attach_default_headers()
- .set_body(RequestContent::Json(Box::new(request_body.clone())))
- .build();
+ .set_body(RequestContent::Json(Box::new(request_body.clone())));
+
+ if let Some(request_id) = request_id {
+ request_builder = request_builder.header(consts::X_REQUEST_ID, &request_id);
+ }
+ if let Some(session_id) = session_id {
+ request_builder = request_builder.header(consts::X_CHAT_SESSION_ID, session_id);
+ }
+
+ let request = request_builder.build();
let response = http_client::send_request(
&state.conf.proxy,
diff --git a/crates/router/src/routes/chat.rs b/crates/router/src/routes/chat.rs
index 555bf007d2..e3970cc6a4 100644
--- a/crates/router/src/routes/chat.rs
+++ b/crates/router/src/routes/chat.rs
@@ -6,6 +6,7 @@ use router_env::{instrument, tracing, Flow};
use super::AppState;
use crate::{
core::{api_locking, chat as chat_core},
+ routes::metrics,
services::{
api,
authentication::{self as auth},
@@ -20,13 +21,21 @@ pub async fn get_data_from_hyperswitch_ai_workflow(
payload: web::Json<chat_api::ChatRequest>,
) -> HttpResponse {
let flow = Flow::GetDataFromHyperswitchAiFlow;
+ let session_id = http_req
+ .headers()
+ .get(common_utils::consts::X_CHAT_SESSION_ID)
+ .and_then(|header_value| header_value.to_str().ok());
Box::pin(api::server_wrap(
flow.clone(),
state,
&http_req,
payload.into_inner(),
|state, user: auth::UserFromToken, payload, _| {
- chat_core::get_data_from_hyperswitch_ai_workflow(state, user, payload)
+ metrics::CHAT_REQUEST_COUNT.add(
+ 1,
+ router_env::metric_attributes!(("merchant_id", user.merchant_id.clone())),
+ );
+ chat_core::get_data_from_hyperswitch_ai_workflow(state, user, payload, session_id)
},
// At present, the AI service retrieves data scoped to the merchant level
&auth::JWTAuth {
diff --git a/crates/router/src/routes/metrics.rs b/crates/router/src/routes/metrics.rs
index e8aeef3e40..defb73fa13 100644
--- a/crates/router/src/routes/metrics.rs
+++ b/crates/router/src/routes/metrics.rs
@@ -140,3 +140,6 @@ histogram_metric_f64!(CHECK_NETWORK_TOKEN_STATUS_TIME, GLOBAL_METER);
// A counter to indicate allowed payment method types mismatch
counter_metric!(PAYMENT_METHOD_TYPES_MISCONFIGURATION_METRIC, GLOBAL_METER);
+
+// AI chat metric to track number of chat request
+counter_metric!(CHAT_REQUEST_COUNT, GLOBAL_METER);