refactor(euclid): log refactors for dynamic routing (#6052)

Co-authored-by: Aprabhat19 <amishaprabhat@gmail.com>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: Amisha Prabhat <55580080+Aprabhat19@users.noreply.github.com>
Co-authored-by: Chethan Rao <70657455+Chethan-rao@users.noreply.github.com>
This commit is contained in:
Prajjwal Kumar
2024-09-26 22:01:48 +05:30
committed by GitHub
parent cfcf9187b9
commit 7e03da7578
4 changed files with 70 additions and 51 deletions

View File

@ -1715,6 +1715,8 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
)?;
#[cfg(all(feature = "v1", feature = "dynamic_routing"))]
{
if let Some(dynamic_routing_algorithm) = business_profile.dynamic_routing_algorithm.clone()
{
let state = state.clone();
let business_profile = business_profile.clone();
@ -1726,6 +1728,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
&payment_attempt,
routable_connectors,
&business_profile,
dynamic_routing_algorithm,
)
.await
.map_err(|e| logger::error!(dynamic_routing_metrics_error=?e))
@ -1734,6 +1737,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
.in_current_span(),
);
}
}
payment_data.payment_intent = payment_intent;
payment_data.payment_attempt = payment_attempt;
router_data.payment_method_status.and_then(|status| {

View File

@ -1253,6 +1253,27 @@ pub async fn toggle_success_based_routing(
),
};
// redact cache for success based routing configs
let cache_key = format!(
"{}_{}",
business_profile.get_id().get_string_repr(),
algorithm_id.get_string_repr()
);
let cache_entries_to_redact =
vec![cache::CacheKind::SuccessBasedDynamicRoutingCache(
cache_key.into(),
)];
let _ = cache::publish_into_redact_channel(
state.store.get_cache_store().as_ref(),
cache_entries_to_redact,
)
.await
.map_err(|e| {
logger::error!(
"unable to publish into the redact channel for evicting the success based routing config cache {e:?}"
)
});
let record = db
.find_routing_algorithm_by_profile_id_algorithm_id(
business_profile.get_id(),
@ -1351,7 +1372,7 @@ pub async fn success_based_routing_update_configs(
cache_entries_to_redact,
)
.await
.map_err(|e| logger::error!("unable to redact the success based routing config cache {e:?}"));
.map_err(|e| logger::error!("unable to publish into the redact channel for evicting the success based routing config cache {e:?}"));
let new_record = record.foreign_into();

View File

@ -594,13 +594,8 @@ pub async fn refresh_success_based_routing_cache(
pub async fn fetch_success_based_routing_configs(
state: &SessionState,
business_profile: &domain::Profile,
dynamic_routing_algorithm: serde_json::Value,
) -> RouterResult<routing_types::SuccessBasedRoutingConfig> {
let dynamic_routing_algorithm = business_profile.dynamic_routing_algorithm.clone().ok_or(
errors::ApiErrorResponse::GenericNotFoundError {
message: "unable to find dynamic_routing_algorithm in business profile".to_string(),
},
)?;
let dynamic_routing_algorithm_ref = dynamic_routing_algorithm
.parse_value::<routing_types::DynamicRoutingAlgorithmRef>("DynamicRoutingAlgorithmRef")
.change_context(errors::ApiErrorResponse::InternalServerError)
@ -613,8 +608,12 @@ pub async fn fetch_success_based_routing_configs(
.to_string(),
})?
.algorithm_id
// error can be possible when the feature is toggled off.
.ok_or(errors::ApiErrorResponse::GenericNotFoundError {
message: "unable to find algorithm id in success based algorithm config".to_string(),
message: format!(
"unable to find algorithm_id in success based algorithm config as the feature is disabled for profile_id: {}",
business_profile.get_id().get_string_repr()
),
})?;
let key = format!(
@ -658,6 +657,7 @@ pub async fn push_metrics_for_success_based_routing(
payment_attempt: &storage::PaymentAttempt,
routable_connectors: Vec<routing_types::RoutableConnectorChoice>,
business_profile: &domain::Profile,
dynamic_routing_algorithm: serde_json::Value,
) -> RouterResult<()> {
let client = state
.grpc_client
@ -675,10 +675,10 @@ pub async fn push_metrics_for_success_based_routing(
)?;
let success_based_routing_configs =
fetch_success_based_routing_configs(state, business_profile)
fetch_success_based_routing_configs(state, business_profile, dynamic_routing_algorithm)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("unable to retrieve success_rate configs")?;
.attach_printable("unable to retrieve success_rate based dynamic routing configs")?;
let tenant_business_profile_id = format!(
"{}:{}",
@ -749,41 +749,35 @@ pub async fn push_metrics_for_success_based_routing(
"currency",
payment_attempt
.currency
.ok_or(errors::ApiErrorResponse::InternalServerError)
.attach_printable("payment currency not found in payment_attempt")?
.to_string(),
.map_or_else(|| "None".to_string(), |currency| currency.to_string()),
),
(
"payment_method",
payment_attempt
.payment_method
.ok_or(errors::ApiErrorResponse::InternalServerError)
.attach_printable("payment method not found in payment_attempt")?
.to_string(),
payment_attempt.payment_method.map_or_else(
|| "None".to_string(),
|payment_method| payment_method.to_string(),
),
),
(
"payment_method_type",
payment_attempt
.payment_method_type
.ok_or(errors::ApiErrorResponse::InternalServerError)
.attach_printable("payment method type not found in payment_attempt")?
.to_string(),
payment_attempt.payment_method_type.map_or_else(
|| "None".to_string(),
|payment_method_type| payment_method_type.to_string(),
),
),
(
"capture_method",
payment_attempt
.capture_method
.ok_or(errors::ApiErrorResponse::InternalServerError)
.attach_printable("capture method not found in payment_attempt")?
.to_string(),
payment_attempt.capture_method.map_or_else(
|| "None".to_string(),
|capture_method| capture_method.to_string(),
),
),
(
"authentication_type",
payment_attempt
.authentication_type
.ok_or(errors::ApiErrorResponse::InternalServerError)
.attach_printable("authentication type not found in payment_attempt")?
.to_string(),
payment_attempt.authentication_type.map_or_else(
|| "None".to_string(),
|authentication_type| authentication_type.to_string(),
),
),
("payment_status", payment_attempt.status.to_string()),
("conclusive_classification", outcome.to_string()),

View File

@ -1014,7 +1014,7 @@ pub async fn toggle_success_based_routing(
&auth::JWTAuthProfileFromRoute {
profile_id: wrapper.profile_id,
required_permission: Permission::RoutingWrite,
minimum_entity_level: EntityType::Merchant,
minimum_entity_level: EntityType::Profile,
},
req.headers(),
),