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 auth_user_id to api_events (#13462)

Mudit Bhutanif7a1099fddae
diff --git a/crates/analytics/docs/clickhouse/scripts/api_events.sql b/crates/analytics/docs/clickhouse/scripts/api_events.sql
index 83e400b1bb..40a50ce1ae 100644
--- a/crates/analytics/docs/clickhouse/scripts/api_events.sql
+++ b/crates/analytics/docs/clickhouse/scripts/api_events.sql
@@ -8,6 +8,7 @@ CREATE TABLE api_events_queue (
`payment_method_type` Nullable(String),
`customer_id` Nullable(String),
`user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
`connector` Nullable(String),
`request_id` String,
`flow_type` LowCardinality(String),
@@ -42,6 +43,7 @@ CREATE TABLE api_events (
`payment_method_type` Nullable(String),
`customer_id` Nullable(String),
`user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
`connector` Nullable(String),
`request_id` String,
`flow_type` LowCardinality(String),
@@ -84,6 +86,7 @@ CREATE TABLE api_events_audit (
`payment_method_type` Nullable(String),
`customer_id` Nullable(String),
`user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
`connector` Nullable(String),
`request_id` String,
`flow_type` LowCardinality(String),
@@ -116,6 +119,7 @@ CREATE TABLE api_events_payout_audit (
`payment_method_type` Nullable(String),
`customer_id` Nullable(String),
`user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
`connector` Nullable(String),
`request_id` String,
`flow_type` LowCardinality(String),
@@ -168,6 +172,7 @@ CREATE MATERIALIZED VIEW api_events_audit_mv TO api_events_audit (
`payment_method_type` Nullable(String),
`customer_id` Nullable(String),
`user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
`connector` Nullable(String),
`request_id` String,
`flow_type` LowCardinality(String),
@@ -198,6 +203,7 @@ SELECT
payment_method_type,
customer_id,
user_id,
+ auth_user_id,
connector,
request_id,
flow_type,
@@ -232,6 +238,7 @@ CREATE MATERIALIZED VIEW api_events_payout_audit_mv TO api_events_payout_audit (
`payment_method_type` Nullable(String),
`customer_id` Nullable(String),
`user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
`connector` Nullable(String),
`request_id` String,
`flow_type` LowCardinality(String),
@@ -260,6 +267,7 @@ SELECT
payment_method_type,
customer_id,
user_id,
+ auth_user_id,
connector,
request_id,
flow_type,
@@ -295,6 +303,7 @@ CREATE MATERIALIZED VIEW api_events_mv TO api_events (
`payment_method_type` Nullable(String),
`customer_id` Nullable(String),
`user_id` Nullable(String),
+ `auth_user_id` Nullable(String),
`connector` Nullable(String),
`request_id` String,
`flow_type` LowCardinality(String),
@@ -326,6 +335,7 @@ SELECT
payment_method_type,
customer_id,
user_id,
+ auth_user_id,
connector,
request_id,
flow_type,
diff --git a/crates/analytics/src/api_event/events.rs b/crates/analytics/src/api_event/events.rs
index 4d09bfa6f7..90705e0e07 100644
--- a/crates/analytics/src/api_event/events.rs
+++ b/crates/analytics/src/api_event/events.rs
@@ -147,6 +147,7 @@ pub struct ApiLogsResult {
pub payment_method_type: Option<String>,
pub customer_id: Option<String>,
pub user_id: Option<String>,
+ pub auth_user_id: Option<String>,
pub connector: Option<String>,
pub request_id: Option<String>,
pub flow_type: String,
diff --git a/crates/router/src/events/api_logs.rs b/crates/router/src/events/api_logs.rs
index 618b635934..c49a013924 100644
--- a/crates/router/src/events/api_logs.rs
+++ b/crates/router/src/events/api_logs.rs
@@ -34,6 +34,7 @@ pub struct ApiEvent {
status_code: i64,
#[serde(flatten)]
auth_type: AuthenticationType,
+ auth_user_id: Option<String>,
request: String,
user_agent: Option<String>,
ip_addr: Option<String>,
@@ -61,6 +62,7 @@ impl ApiEvent {
response: Option<serde_json::Value>,
hs_latency: Option<u128>,
auth_type: AuthenticationType,
+ auth_user_id: Option<String>,
error: Option<serde_json::Value>,
event_type: ApiEventsType,
http_req: &HttpRequest,
@@ -78,6 +80,7 @@ impl ApiEvent {
request: request.to_string(),
response: response.map(|resp| resp.to_string()),
auth_type,
+ auth_user_id,
error,
ip_addr: http_req
.connection_info()
diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs
index 5366b5fe48..6d1f7438d7 100644
--- a/crates/router/src/services/api.rs
+++ b/crates/router/src/services/api.rs
@@ -365,6 +365,7 @@ where
state.infra_components.as_ref(),
);
+ let auth_user_id = auth_type.get_user_id();
let api_event = ApiEvent::new(
tenant_id,
Some(merchant_id.clone()),
@@ -376,6 +377,7 @@ where
serialized_response,
overhead_latency,
auth_type,
+ auth_user_id,
error,
event_type.unwrap_or(ApiEventsType::Miscellaneous),
request,
diff --git a/crates/router/src/services/authentication.rs b/crates/router/src/services/authentication.rs
index 1dc9ae51cb..6277a1749f 100644
--- a/crates/router/src/services/authentication.rs
+++ b/crates/router/src/services/authentication.rs
@@ -245,6 +245,29 @@ impl AuthenticationType {
| Self::NoAuth => None,
}
}
+
+ pub fn get_user_id(&self) -> Option<String> {
+ match self {
+ Self::OrganizationJwt { user_id, .. }
+ | Self::MerchantJwtWithProfileId { user_id, .. }
+ | Self::UserJwt { user_id, .. }
+ | Self::SinglePurposeJwt { user_id, .. }
+ | Self::SinglePurposeOrLoginJwt { user_id, .. } => Some(user_id.clone()),
+ Self::MerchantJwt { user_id, .. } => user_id.clone(),
+ Self::ApiKey { .. }
+ | Self::AdminApiKey
+ | Self::AdminApiAuthWithMerchantId { .. }
+ | Self::BasicAuth { .. }
+ | Self::MerchantId { .. }
+ | Self::PublishableKey { .. }
+ | Self::SdkAuthorization { .. }
+ | Self::WebhookAuth { .. }
+ | Self::InternalMerchantIdProfileId { .. }
+ | Self::EmbeddedJwt { .. }
+ | Self::InternalApiKey
+ | Self::NoAuth => None,
+ }
+ }
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, serde::Deserialize, strum::Display)]