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(router): add metrics for webhooks (#1266)

ItsMeShashankd52813293226
diff --git a/crates/router/src/core/metrics.rs b/crates/router/src/core/metrics.rs
index ec3b0cd37b..d9dcd56547 100644
--- a/crates/router/src/core/metrics.rs
+++ b/crates/router/src/core/metrics.rs
@@ -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);
diff --git a/crates/router/src/core/webhooks.rs b/crates/router/src/core/webhooks.rs
index 744c39626a..ce7fa8f25d 100644
--- a/crates/router/src/core/webhooks.rs
+++ b/crates/router/src/core/webhooks.rs
@@ -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