feat(events): adding infra level components to api-events (#8214)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Shivansh Mathur
2025-06-04 11:28:15 +05:30
committed by GitHub
parent 69ebef30a1
commit ec908d38ac
10 changed files with 53 additions and 3 deletions

View File

@ -540,5 +540,6 @@ pub(crate) async fn fetch_raw_secrets(
revenue_recovery: conf.revenue_recovery,
debit_routing_config: conf.debit_routing_config,
clone_connector_allowlist: conf.clone_connector_allowlist,
infra_values: conf.infra_values,
}
}

View File

@ -158,6 +158,8 @@ pub struct Settings<S: SecretState> {
#[cfg(feature = "v2")]
pub revenue_recovery: revenue_recovery::RevenueRecoverySettings,
pub clone_connector_allowlist: Option<CloneConnectorAllowlistConfig>,
#[serde(default)]
pub infra_values: Option<HashMap<String, String>>,
}
#[derive(Debug, Deserialize, Clone, Default)]

View File

@ -103,7 +103,7 @@ pub async fn incoming_webhooks_wrapper<W: types::OutgoingWebhookType>(
let response_value = serde_json::to_value(&webhooks_response_tracker)
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Could not convert webhook effect to string")?;
let infra = state.infra_components.clone();
let api_event = ApiEvent::new(
state.tenant.tenant_id.clone(),
Some(merchant_context.get_merchant_account().get_id().clone()),
@ -119,6 +119,7 @@ pub async fn incoming_webhooks_wrapper<W: types::OutgoingWebhookType>(
api_event,
req,
req.method(),
infra,
);
state.event_handler().log_event(&api_event);
Ok(application_response)

View File

@ -97,6 +97,8 @@ pub async fn incoming_webhooks_wrapper<W: types::OutgoingWebhookType>(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Could not convert webhook effect to string")?;
let infra = state.infra_components.clone();
let api_event = ApiEvent::new(
state.tenant.tenant_id.clone(),
Some(merchant_context.get_merchant_account().get_id().clone()),
@ -112,6 +114,7 @@ pub async fn incoming_webhooks_wrapper<W: types::OutgoingWebhookType>(
api_event,
req,
req.method(),
infra,
);
state.event_handler().log_event(&api_event);
Ok(application_response)

View File

@ -43,6 +43,8 @@ pub struct ApiEvent {
event_type: ApiEventsType,
hs_latency: Option<u128>,
http_method: String,
#[serde(flatten)]
infra_components: Option<serde_json::Value>,
}
impl ApiEvent {
@ -62,6 +64,7 @@ impl ApiEvent {
event_type: ApiEventsType,
http_req: &HttpRequest,
http_method: &http::Method,
infra_components: Option<serde_json::Value>,
) -> Self {
Self {
tenant_id,
@ -87,6 +90,7 @@ impl ApiEvent {
event_type,
hs_latency,
http_method: http_method.to_string(),
infra_components,
}
}
}

View File

@ -130,6 +130,7 @@ pub struct SessionState {
pub theme_storage_client: Arc<dyn FileStorageInterface>,
pub locale: String,
pub crm_client: Arc<dyn CrmInterface>,
pub infra_components: Option<serde_json::Value>,
}
impl scheduler::SchedulerSessionState for SessionState {
fn get_db(&self) -> Box<dyn SchedulerInterface> {
@ -242,6 +243,7 @@ pub struct AppState {
pub grpc_client: Arc<GrpcClients>,
pub theme_storage_client: Arc<dyn FileStorageInterface>,
pub crm_client: Arc<dyn CrmInterface>,
pub infra_components: Option<serde_json::Value>,
}
impl scheduler::SchedulerAppState for AppState {
fn get_tenants(&self) -> Vec<id_type::TenantId> {
@ -406,7 +408,7 @@ impl AppState {
let crm_client = conf.crm.get_crm_client().await;
let grpc_client = conf.grpc_client.get_grpc_client_interface().await;
let infra_component_values = Self::process_env_mappings(conf.infra_values.clone());
Self {
flow_name: String::from("default"),
stores,
@ -427,6 +429,7 @@ impl AppState {
grpc_client,
theme_storage_client,
crm_client,
infra_components: infra_component_values,
}
})
.await
@ -521,8 +524,29 @@ impl AppState {
theme_storage_client: self.theme_storage_client.clone(),
locale: locale.unwrap_or(common_utils::consts::DEFAULT_LOCALE.to_string()),
crm_client: self.crm_client.clone(),
infra_components: self.infra_components.clone(),
})
}
pub fn process_env_mappings(
mappings: Option<HashMap<String, String>>,
) -> Option<serde_json::Value> {
let result: HashMap<String, String> = mappings?
.into_iter()
.filter_map(|(key, env_var)| std::env::var(&env_var).ok().map(|value| (key, value)))
.collect();
if result.is_empty() {
None
} else {
Some(serde_json::Value::Object(
result
.into_iter()
.map(|(k, v)| (k, serde_json::Value::String(v)))
.collect(),
))
}
}
}
pub struct Health;

View File

@ -602,7 +602,6 @@ where
.switch(),
)?
};
let locale = utils::get_locale_from_header(&incoming_request_header.clone());
let mut session_state =
Arc::new(app_state.clone()).get_session_state(&tenant_id, Some(locale), || {
@ -701,6 +700,8 @@ where
}
};
let infra = state.infra_components.clone();
let api_event = ApiEvent::new(
tenant_id,
Some(merchant_id.clone()),
@ -716,7 +717,9 @@ where
event_type.unwrap_or(ApiEventsType::Miscellaneous),
request,
request.method(),
infra.clone(),
);
state.event_handler().log_event(&api_event);
output