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(analytics): add source, destination and execution_mode dimensions, plus ucs_api_events (#12714)

gopikrishna0001d03429b7bce
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs
index 28eafeb459..026ca6fc29 100644
--- a/crates/common_enums/src/enums.rs
+++ b/crates/common_enums/src/enums.rs
@@ -2876,6 +2876,33 @@ pub enum ExecutionMode {
NotApplicable,
}
+#[derive(Clone, Copy, Debug, serde::Serialize)]
+#[serde(rename_all = "snake_case")]
+/// Whether a connector event is the real call or a shadow mirror.
+pub enum EventExecutionMode {
+ Primary,
+ Shadow,
+}
+
+impl From<ExecutionMode> for EventExecutionMode {
+ fn from(mode: ExecutionMode) -> Self {
+ match mode {
+ ExecutionMode::Shadow => Self::Shadow,
+ ExecutionMode::Primary | ExecutionMode::NotApplicable => Self::Primary,
+ }
+ }
+}
+
+#[derive(Clone, Copy, Debug, serde::Serialize)]
+#[serde(rename_all = "snake_case")]
+/// Where a connector event's call was sent.
+pub enum EventDestination {
+ /// A direct call to the connector.
+ Connector,
+ /// A call to the Unified Connector Service.
+ UnifiedConnectorService,
+}
+
#[derive(
Clone,
Copy,
diff --git a/crates/hyperswitch_interfaces/src/api_client.rs b/crates/hyperswitch_interfaces/src/api_client.rs
index 72a0c2f418..0ba33bad6e 100644
--- a/crates/hyperswitch_interfaces/src/api_client.rs
+++ b/crates/hyperswitch_interfaces/src/api_client.rs
@@ -305,6 +305,9 @@ where
req.dispute_id.clone(),
req.payout_id.clone(),
status_code,
+ common_enums::EventDestination::Connector,
+ // Direct connector call: a live call, never a shadow mirror.
+ common_enums::EventExecutionMode::Primary,
);
match response {
diff --git a/crates/hyperswitch_interfaces/src/events/connector_api_logs.rs b/crates/hyperswitch_interfaces/src/events/connector_api_logs.rs
index d0335c1b9c..6bd961bb45 100644
--- a/crates/hyperswitch_interfaces/src/events/connector_api_logs.rs
+++ b/crates/hyperswitch_interfaces/src/events/connector_api_logs.rs
@@ -23,6 +23,10 @@ pub struct ConnectorEvent {
pub request_id: String,
latency: u128,
status_code: u16,
+ /// Whether this call went to the connector directly or to the Unified Connector Service.
+ destination: common_enums::EventDestination,
+ /// Whether this call is the real execution or a shadow mirror.
+ execution_mode: common_enums::EventExecutionMode,
#[serde(flatten)]
connector_event_type: common_utils::events::ConnectorEventsType,
}
@@ -45,6 +49,8 @@ impl ConnectorEvent {
dispute_id: Option<String>,
payout_id: Option<String>,
status_code: u16,
+ destination: common_enums::EventDestination,
+ execution_mode: common_enums::EventExecutionMode,
) -> Self {
let connector_event_type = common_utils::events::ConnectorEventsType::new(
payment_id, refund_id, payout_id, dispute_id,
@@ -69,6 +75,8 @@ impl ConnectorEvent {
.unwrap_or("NO_REQUEST_ID".to_string()),
latency,
status_code,
+ destination,
+ execution_mode,
connector_event_type,
}
}
diff --git a/crates/router/src/core/unified_connector_service.rs b/crates/router/src/core/unified_connector_service.rs
index df1de36d0b..5c3c005882 100644
--- a/crates/router/src/core/unified_connector_service.rs
+++ b/crates/router/src/core/unified_connector_service.rs
@@ -2632,6 +2632,53 @@ pub fn build_webhook_secrets_from_merchant_connector_account(
}
}
+/// Emit the connector event for a Hyperswitch -> UCS gRPC call, tagged
+/// `destination = unified_connector_service` (same `connector_events` stream as a direct call).
+#[allow(clippy::too_many_arguments)]
+fn emit_ucs_connector_event(
+ state: &SessionState,
+ flow_type: &'static str,
+ connector_name: String,
+ payment_id: String,
+ merchant_id: id_type::MerchantId,
+ refund_id: Option<String>,
+ dispute_id: Option<String>,
+ payout_id: Option<String>,
+ grpc_request_body: serde_json::Value,
+ status_code: u16,
+ response_body: Option<serde_json::Value>,
+ external_latency: u128,
+ execution_mode: ExecutionMode,
+) {
+ let mut connector_event = ConnectorEvent::new(
+ state.tenant.tenant_id.clone(),
+ connector_name,
+ flow_type,
+ grpc_request_body,
+ "grpc://unified-connector-service".to_string(),
+ Method::Post,
+ payment_id,
+ merchant_id,
+ state.request_id.as_ref(),
+ external_latency,
+ refund_id,
+ dispute_id,
+ payout_id,
+ status_code,
+ common_enums::EventDestination::UnifiedConnectorService,
+ common_enums::EventExecutionMode::from(execution_mode),
+ );
+
+ if let Some(body) = response_body {
+ match status_code {
+ 400..=599 => connector_event.set_error_response_body(&body),
+ _ => connector_event.set_response_body(&body),
+ }
+ }
+
+ state.event_handler.log_event(&connector_event);
+}
+
/// UCS Event Logging Wrapper Function
/// This function wraps UCS calls with comprehensive event logging.
/// It logs the actual gRPC request/response data, timing, and error information.
@@ -2733,40 +2780,24 @@ where
}
};
- // Only emit connector event during primary mode
- if let ExecutionMode::Primary = execution_mode {
- let mut connector_event = ConnectorEvent::new(
- state.tenant.tenant_id.clone(),
- connector_name,
- std::any::type_name::<T>(),
- grpc_request_body,
- "grpc://unified-connector-service".to_string(),
- Method::Post,
- payment_id,
- merchant_id,
- state.request_id.as_ref(),
- external_latency,
- refund_id,
- dispute_id,
- payout_id,
- status_code,
- );
-
- // Set response body based on status code
- if let Some(body) = response_body {
- match status_code {
- 400..=599 => {
- connector_event.set_error_response_body(&body);
- }
- _ => {
- connector_event.set_response_body(&body);
- }
- }
- }
-
- // Emit event
- state.event_handler.log_event(&connector_event);
- }
+ // Emit the Hyperswitch -> UCS connector event (shared with the other wrapper); it lands in
+ // connector_events with `destination = unified_connector_service`, `execution_mode` primary
+ // or shadow.
+ emit_ucs_connector_event(
+ state,
+ std::any::type_name::<T>(),
+ connector_name,
+ payment_id,
+ merchant_id,
+ refund_id,
+ dispute_id,
+ payout_id,
+ grpc_request_body,
+ status_code,
+ response_body,
+ external_latency,
+ execution_mode,
+ );
// Set external latency on router data
router_result.map(|mut router_data| {
@@ -2881,40 +2912,24 @@ where
}
};
- // Only emit connector event during primary mode
- if let ExecutionMode::Primary = execution_mode {
- let mut connector_event = ConnectorEvent::new(
- state.tenant.tenant_id.clone(),
- connector_name,
- std::any::type_name::<T>(),
- grpc_request_body,
- "grpc://unified-connector-service".to_string(),
- Method::Post,
- payment_id,
- merchant_id,
- state.request_id.as_ref(),
- external_latency,
- refund_id,
- dispute_id,
- payout_id,
- status_code,
- );
-
- // Set response body based on status code
- if let Some(body) = response_body {
- match status_code {
- 400..=599 => {
- connector_event.set_error_response_body(&body);
- }
- _ => {
- connector_event.set_response_body(&body);
- }
- }
- }
-
- // Emit event
- state.event_handler.log_event(&connector_event);
- }
+ // Emit the Hyperswitch -> UCS connector event (shared with the other wrapper); it lands in
+ // connector_events with `destination = unified_connector_service`, `execution_mode` primary
+ // or shadow.
+ emit_ucs_connector_event(
+ state,
+ std::any::type_name::<T>(),
+ connector_name,
+ payment_id,
+ merchant_id,
+ refund_id,
+ dispute_id,
+ payout_id,
+ grpc_request_body,
+ status_code,
+ response_body,
+ external_latency,
+ execution_mode,
+ );
// Set external latency on router data
router_result.map(|mut router_data| {
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index e67afe8259..235eb5ea73 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -192,24 +192,13 @@ impl SessionState {
ExecutionMode::Shadow => Some("shadow"),
ExecutionMode::NotApplicable => None,
};
- // For shadow mode, disable event publishing in UCS
- let config_override = match unified_connector_service_execution_mode {
- ExecutionMode::Shadow => Some(
- serde_json::json!({
- "events": {
- "enabled": false
- }
- })
- .to_string(),
- ),
- _ => None,
- };
GrpcHeadersUcs::builder()
.tenant_id(tenant_id)
.request_id(request_id)
.shadow_mode(shadow_mode)
.proxy_name(proxy_name)
- .config_override(config_override)
+ // no override: UCS emits in all modes, distinguished downstream by `execution_mode`
+ .config_override(None)
}
#[cfg(all(feature = "revenue_recovery", feature = "v2"))]
pub fn get_recovery_grpc_headers(&self) -> GrpcRecoveryHeaders {