feat(router): add metrics for webhooks (#1266)

Co-authored-by: Prajjwal Kumar <prajjwal.kumar@juspay.in>
This commit is contained in:
ItsMeShashank
2023-07-03 15:09:16 +05:30
committed by GitHub
parent 6739b59bc8
commit d528132932
2 changed files with 63 additions and 0 deletions

View File

@ -1,3 +1,4 @@
pub use router_env::opentelemetry::KeyValue;
use router_env::{counter_metric, global_meter, metrics_context};
metrics_context!(CONTEXT);
@ -31,3 +32,10 @@ counter_metric!(
ATTACH_EVIDENCE_DISPUTE_STATUS_VALIDATION_FAILURE_METRIC,
GLOBAL_METER
);
counter_metric!(WEBHOOK_INCOMING_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_INCOMING_FILTERED_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_SOURCE_VERIFIED_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_OUTGOING_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_OUTGOING_RECEIVED_COUNT, GLOBAL_METER);
counter_metric!(WEBHOOK_OUTGOING_NOT_RECEIVED_COUNT, GLOBAL_METER);

View File

@ -26,6 +26,7 @@ use crate::{
};
const OUTGOING_WEBHOOK_TIMEOUT_SECS: u64 = 5;
const MERCHANT_ID: &str = "merchant_id";
#[instrument(skip_all)]
pub async fn payments_incoming_webhook_flow<W: api::OutgoingWebhookType>(
@ -594,6 +595,14 @@ pub async fn trigger_webhook_to_merchant<W: api::OutgoingWebhookType>(
let response =
services::api::send_request(state, request, Some(OUTGOING_WEBHOOK_TIMEOUT_SECS)).await;
metrics::WEBHOOK_OUTGOING_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::KeyValue::new(
MERCHANT_ID,
merchant_account.merchant_id.clone(),
)],
);
logger::debug!(outgoing_webhook_response=?response);
match response {
@ -603,6 +612,14 @@ pub async fn trigger_webhook_to_merchant<W: api::OutgoingWebhookType>(
}
Ok(res) => {
if res.status().is_success() {
metrics::WEBHOOK_OUTGOING_RECEIVED_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::KeyValue::new(
MERCHANT_ID,
merchant_account.merchant_id.clone(),
)],
);
let update_event = storage::EventUpdate::UpdateWebhookNotified {
is_webhook_notified: Some(true),
};
@ -612,6 +629,14 @@ pub async fn trigger_webhook_to_merchant<W: api::OutgoingWebhookType>(
.await
.change_context(errors::WebhooksFlowError::WebhookEventUpdationFailed)?;
} else {
metrics::WEBHOOK_OUTGOING_NOT_RECEIVED_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::KeyValue::new(
MERCHANT_ID,
merchant_account.merchant_id.clone(),
)],
);
// [#217]: Schedule webhook for retry.
Err(errors::WebhooksFlowError::NotReceivedByMerchant).into_report()?;
}
@ -630,6 +655,15 @@ pub async fn webhooks_core<W: api::OutgoingWebhookType>(
connector_name: &str,
body: actix_web::web::Bytes,
) -> RouterResponse<serde_json::Value> {
metrics::WEBHOOK_INCOMING_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::KeyValue::new(
MERCHANT_ID,
merchant_account.merchant_id.clone(),
)],
);
let connector = api::ConnectorData::get_connector_by_name(
&state.conf.connectors,
connector_name,
@ -687,6 +721,18 @@ pub async fn webhooks_core<W: api::OutgoingWebhookType>(
.await
.switch()
.attach_printable("There was an issue in incoming webhook source verification")?;
if source_verified {
metrics::WEBHOOK_SOURCE_VERIFIED_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::KeyValue::new(
MERCHANT_ID,
merchant_account.merchant_id.clone(),
)],
);
}
logger::info!(source_verified=?source_verified);
let object_ref_id = connector
.get_webhook_object_reference_id(&request_details)
@ -758,6 +804,15 @@ pub async fn webhooks_core<W: api::OutgoingWebhookType>(
.into_report()
.attach_printable("Unsupported Flow Type received in incoming webhooks")?,
}
} else {
metrics::WEBHOOK_INCOMING_FILTERED_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::KeyValue::new(
MERCHANT_ID,
merchant_account.merchant_id.clone(),
)],
);
}
let response = connector