Skip to content

Commit

Permalink
feat(routing): Routing prometheus metrics (#2870)
Browse files Browse the repository at this point in the history
Co-authored-by: Prajjwal Kumar <[email protected]>
  • Loading branch information
Sarthak1799 and prajjwalkumar17 authored Nov 22, 2023
1 parent 341374b commit 4e15d77
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
33 changes: 33 additions & 0 deletions crates/router/src/core/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,36 @@ counter_metric!(
WEBHOOK_EVENT_TYPE_IDENTIFICATION_FAILURE_COUNT,
GLOBAL_METER
);

counter_metric!(ROUTING_CREATE_REQUEST_RECEIVED, GLOBAL_METER);
counter_metric!(ROUTING_CREATE_SUCCESS_RESPONSE, GLOBAL_METER);
counter_metric!(ROUTING_MERCHANT_DICTIONARY_RETRIEVE, GLOBAL_METER);
counter_metric!(
ROUTING_MERCHANT_DICTIONARY_RETRIEVE_SUCCESS_RESPONSE,
GLOBAL_METER
);
counter_metric!(ROUTING_LINK_CONFIG, GLOBAL_METER);
counter_metric!(ROUTING_LINK_CONFIG_SUCCESS_RESPONSE, GLOBAL_METER);
counter_metric!(ROUTING_RETRIEVE_CONFIG, GLOBAL_METER);
counter_metric!(ROUTING_RETRIEVE_CONFIG_SUCCESS_RESPONSE, GLOBAL_METER);
counter_metric!(ROUTING_RETRIEVE_DEFAULT_CONFIG, GLOBAL_METER);
counter_metric!(
ROUTING_RETRIEVE_DEFAULT_CONFIG_SUCCESS_RESPONSE,
GLOBAL_METER
);
counter_metric!(ROUTING_RETRIEVE_LINK_CONFIG, GLOBAL_METER);
counter_metric!(ROUTING_RETRIEVE_LINK_CONFIG_SUCCESS_RESPONSE, GLOBAL_METER);
counter_metric!(ROUTING_UNLINK_CONFIG, GLOBAL_METER);
counter_metric!(ROUTING_UNLINK_CONFIG_SUCCESS_RESPONSE, GLOBAL_METER);
counter_metric!(ROUTING_UPDATE_CONFIG, GLOBAL_METER);
counter_metric!(ROUTING_UPDATE_CONFIG_SUCCESS_RESPONSE, GLOBAL_METER);
counter_metric!(ROUTING_UPDATE_CONFIG_FOR_PROFILE, GLOBAL_METER);
counter_metric!(
ROUTING_UPDATE_CONFIG_FOR_PROFILE_SUCCESS_RESPONSE,
GLOBAL_METER
);
counter_metric!(ROUTING_RETRIEVE_CONFIG_FOR_PROFILE, GLOBAL_METER);
counter_metric!(
ROUTING_RETRIEVE_CONFIG_FOR_PROFILE_SUCCESS_RESPONSE,
GLOBAL_METER
);
47 changes: 45 additions & 2 deletions crates/router/src/core/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
consts,
core::{
errors::{RouterResponse, StorageErrorExt},
utils as core_utils,
metrics, utils as core_utils,
},
routes::AppState,
types::domain,
Expand All @@ -35,6 +35,7 @@ pub async fn retrieve_merchant_routing_dictionary(
merchant_account: domain::MerchantAccount,
#[cfg(feature = "business_profile_routing")] query_params: RoutingRetrieveQuery,
) -> RouterResponse<routing_types::RoutingKind> {
metrics::ROUTING_MERCHANT_DICTIONARY_RETRIEVE.add(&metrics::CONTEXT, 1, &[]);
#[cfg(feature = "business_profile_routing")]
{
let routing_metadata = state
Expand All @@ -51,11 +52,18 @@ pub async fn retrieve_merchant_routing_dictionary(
.map(ForeignInto::foreign_into)
.collect::<Vec<_>>();

metrics::ROUTING_MERCHANT_DICTIONARY_RETRIEVE_SUCCESS_RESPONSE.add(
&metrics::CONTEXT,
1,
&[],
);
Ok(service_api::ApplicationResponse::Json(
routing_types::RoutingKind::RoutingAlgorithm(result),
))
}
#[cfg(not(feature = "business_profile_routing"))]
metrics::ROUTING_MERCHANT_DICTIONARY_RETRIEVE_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
#[cfg(not(feature = "business_profile_routing"))]
Ok(service_api::ApplicationResponse::Json(
routing_types::RoutingKind::Config(
helpers::get_merchant_routing_dictionary(
Expand All @@ -73,6 +81,7 @@ pub async fn create_routing_config(
key_store: domain::MerchantKeyStore,
request: routing_types::RoutingConfigRequest,
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
metrics::ROUTING_CREATE_REQUEST_RECEIVED.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

let name = request
Expand Down Expand Up @@ -147,6 +156,7 @@ pub async fn create_routing_config(

let new_record = record.foreign_into();

metrics::ROUTING_CREATE_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(new_record))
}

Expand Down Expand Up @@ -213,6 +223,7 @@ pub async fn create_routing_config(
)
.await?;

metrics::ROUTING_CREATE_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(new_record))
}
}
Expand All @@ -223,6 +234,7 @@ pub async fn link_routing_config(
#[cfg(not(feature = "business_profile_routing"))] key_store: domain::MerchantKeyStore,
algorithm_id: String,
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
metrics::ROUTING_LINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();
#[cfg(feature = "business_profile_routing")]
{
Expand Down Expand Up @@ -268,6 +280,7 @@ pub async fn link_routing_config(
helpers::update_business_profile_active_algorithm_ref(db, business_profile, routing_ref)
.await?;

metrics::ROUTING_LINK_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(
routing_algorithm.foreign_into(),
))
Expand Down Expand Up @@ -317,6 +330,7 @@ pub async fn link_routing_config(
.await?;
helpers::update_merchant_active_algorithm_ref(db, &key_store, routing_ref).await?;

metrics::ROUTING_LINK_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
}
}
Expand All @@ -326,6 +340,7 @@ pub async fn retrieve_routing_config(
merchant_account: domain::MerchantAccount,
algorithm_id: RoutingAlgorithmId,
) -> RouterResponse<routing_types::MerchantRoutingAlgorithm> {
metrics::ROUTING_RETRIEVE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();
#[cfg(feature = "business_profile_routing")]
{
Expand All @@ -350,6 +365,8 @@ pub async fn retrieve_routing_config(
.foreign_try_into()
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("unable to parse routing algorithm")?;

metrics::ROUTING_RETRIEVE_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
}

Expand Down Expand Up @@ -387,6 +404,7 @@ pub async fn retrieve_routing_config(
modified_at: record.modified_at,
};

metrics::ROUTING_RETRIEVE_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
}
}
Expand All @@ -396,6 +414,7 @@ pub async fn unlink_routing_config(
#[cfg(not(feature = "business_profile_routing"))] key_store: domain::MerchantKeyStore,
#[cfg(feature = "business_profile_routing")] request: routing_types::RoutingConfigRequest,
) -> RouterResponse<routing_types::RoutingDictionaryRecord> {
metrics::ROUTING_UNLINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();
#[cfg(feature = "business_profile_routing")]
{
Expand Down Expand Up @@ -451,6 +470,12 @@ pub async fn unlink_routing_config(
routing_algorithm,
)
.await?;

metrics::ROUTING_UNLINK_CONFIG_SUCCESS_RESPONSE.add(
&metrics::CONTEXT,
1,
&[],
);
Ok(service_api::ApplicationResponse::Json(response))
}
None => Err(errors::ApiErrorResponse::PreconditionFailed {
Expand Down Expand Up @@ -559,6 +584,7 @@ pub async fn unlink_routing_config(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to update routing algorithm ref in merchant account")?;

metrics::ROUTING_UNLINK_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
}
}
Expand All @@ -568,6 +594,7 @@ pub async fn update_default_routing_config(
merchant_account: domain::MerchantAccount,
updated_config: Vec<routing_types::RoutableConnectorChoice>,
) -> RouterResponse<Vec<routing_types::RoutableConnectorChoice>> {
metrics::ROUTING_UPDATE_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();
let default_config =
helpers::get_merchant_default_config(db, &merchant_account.merchant_id).await?;
Expand Down Expand Up @@ -606,25 +633,35 @@ pub async fn update_default_routing_config(
)
.await?;

metrics::ROUTING_UPDATE_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(updated_config))
}

pub async fn retrieve_default_routing_config(
state: AppState,
merchant_account: domain::MerchantAccount,
) -> RouterResponse<Vec<routing_types::RoutableConnectorChoice>> {
metrics::ROUTING_RETRIEVE_DEFAULT_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

helpers::get_merchant_default_config(db, &merchant_account.merchant_id)
.await
.map(service_api::ApplicationResponse::Json)
.map(|conn_choice| {
metrics::ROUTING_RETRIEVE_DEFAULT_CONFIG_SUCCESS_RESPONSE.add(
&metrics::CONTEXT,
1,
&[],
);
service_api::ApplicationResponse::Json(conn_choice)
})
}

pub async fn retrieve_linked_routing_config(
state: AppState,
merchant_account: domain::MerchantAccount,
#[cfg(feature = "business_profile_routing")] query_params: RoutingRetrieveLinkQuery,
) -> RouterResponse<routing_types::LinkedRoutingConfigRetrieveResponse> {
metrics::ROUTING_RETRIEVE_LINK_CONFIG.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

#[cfg(feature = "business_profile_routing")]
Expand Down Expand Up @@ -672,6 +709,7 @@ pub async fn retrieve_linked_routing_config(
}
}

metrics::ROUTING_RETRIEVE_LINK_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(
routing_types::LinkedRoutingConfigRetrieveResponse::ProfileBased(active_algorithms),
))
Expand Down Expand Up @@ -718,6 +756,7 @@ pub async fn retrieve_linked_routing_config(
routing_types::RoutingRetrieveResponse { algorithm },
);

metrics::ROUTING_RETRIEVE_LINK_CONFIG_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
}
}
Expand All @@ -726,6 +765,7 @@ pub async fn retrieve_default_routing_config_for_profiles(
state: AppState,
merchant_account: domain::MerchantAccount,
) -> RouterResponse<Vec<routing_types::ProfileDefaultRoutingConfig>> {
metrics::ROUTING_RETRIEVE_CONFIG_FOR_PROFILE.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

let all_profiles = db
Expand Down Expand Up @@ -755,6 +795,7 @@ pub async fn retrieve_default_routing_config_for_profiles(
)
.collect::<Vec<_>>();

metrics::ROUTING_RETRIEVE_CONFIG_FOR_PROFILE_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(default_configs))
}

Expand All @@ -764,6 +805,7 @@ pub async fn update_default_routing_config_for_profile(
updated_config: Vec<routing_types::RoutableConnectorChoice>,
profile_id: String,
) -> RouterResponse<routing_types::ProfileDefaultRoutingConfig> {
metrics::ROUTING_UPDATE_CONFIG_FOR_PROFILE.add(&metrics::CONTEXT, 1, &[]);
let db = state.store.as_ref();

let business_profile = core_utils::validate_and_get_business_profile(
Expand Down Expand Up @@ -829,6 +871,7 @@ pub async fn update_default_routing_config_for_profile(
)
.await?;

metrics::ROUTING_UPDATE_CONFIG_FOR_PROFILE_SUCCESS_RESPONSE.add(&metrics::CONTEXT, 1, &[]);
Ok(service_api::ApplicationResponse::Json(
routing_types::ProfileDefaultRoutingConfig {
profile_id: business_profile.profile_id,
Expand Down

0 comments on commit 4e15d77

Please sign in to comment.