Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add basic counter metrics for IMC #5006

Merged
merged 7 commits into from
Jun 18, 2024
9 changes: 5 additions & 4 deletions crates/analytics/src/api_event/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use common_utils::errors::ReportSwitchExt;
use error_stack::ResultExt;
use router_env::{
instrument, logger,
metrics::add_attributes,
tracing::{self, Instrument},
};

Expand Down Expand Up @@ -135,10 +136,10 @@ pub async fn get_api_event_metrics(
.change_context(AnalyticsError::UnknownError)?
{
let data = data?;
let attributes = &[
metrics::request::add_attributes("metric_type", metric.to_string()),
metrics::request::add_attributes("source", pool.to_string()),
];
let attributes = &add_attributes([
("metric_type", metric.to_string()),
("source", pool.to_string()),
]);

let value = u64::try_from(data.len());
if let Ok(val) = value {
Expand Down
9 changes: 5 additions & 4 deletions crates/analytics/src/disputes/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use api_models::analytics::{
use error_stack::ResultExt;
use router_env::{
logger,
metrics::add_attributes,
tracing::{self, Instrument},
};

Expand Down Expand Up @@ -70,10 +71,10 @@ pub async fn get_metrics(
.change_context(AnalyticsError::UnknownError)?
{
let data = data?;
let attributes = &[
metrics::request::add_attributes("metric_type", metric.to_string()),
metrics::request::add_attributes("source", pool.to_string()),
];
let attributes = &add_attributes([
("metric_type", metric.to_string()),
("source", pool.to_string()),
]);

let value = u64::try_from(data.len());
if let Ok(val) = value {
Expand Down
19 changes: 7 additions & 12 deletions crates/analytics/src/metrics/request.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
pub fn add_attributes<T: Into<router_env::opentelemetry::Value>>(
key: &'static str,
value: T,
) -> router_env::opentelemetry::KeyValue {
router_env::opentelemetry::KeyValue::new(key, value)
}
use std::time;

use router_env::metrics::add_attributes;

#[inline]
pub async fn record_operation_time<F, R, T>(
Expand All @@ -17,19 +14,17 @@ where
T: ToString,
{
let (result, time) = time_future(future).await;
let attributes = &[
add_attributes("metric_name", metric_name.to_string()),
add_attributes("source", source.to_string()),
];
let attributes = &add_attributes([
("metric_name", metric_name.to_string()),
("source", source.to_string()),
]);
let value = time.as_secs_f64();
metric.record(&super::CONTEXT, value, attributes);

router_env::logger::debug!("Attributes: {:?}, Time: {}", attributes, value);
result
}

use std::time;

#[inline]
pub async fn time_future<F, R>(future: F) -> (R, time::Duration)
where
Expand Down
17 changes: 9 additions & 8 deletions crates/analytics/src/payments/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use common_utils::errors::CustomResult;
use error_stack::ResultExt;
use router_env::{
instrument, logger,
metrics::add_attributes,
tracing::{self, Instrument},
};

Expand Down Expand Up @@ -120,10 +121,10 @@ pub async fn get_metrics(
match task_type {
TaskType::MetricTask(metric, data) => {
let data = data?;
let attributes = &[
metrics::request::add_attributes("metric_type", metric.to_string()),
metrics::request::add_attributes("source", pool.to_string()),
];
let attributes = &add_attributes([
("metric_type", metric.to_string()),
("source", pool.to_string()),
]);

let value = u64::try_from(data.len());
if let Ok(val) = value {
Expand Down Expand Up @@ -172,10 +173,10 @@ pub async fn get_metrics(
}
TaskType::DistributionTask(distribution, data) => {
let data = data?;
let attributes = &[
metrics::request::add_attributes("distribution_type", distribution.to_string()),
metrics::request::add_attributes("source", pool.to_string()),
];
let attributes = &add_attributes([
("distribution_type", distribution.to_string()),
("source", pool.to_string()),
]);

let value = u64::try_from(data.len());
if let Ok(val) = value {
Expand Down
9 changes: 5 additions & 4 deletions crates/analytics/src/refunds/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use api_models::analytics::{
use error_stack::ResultExt;
use router_env::{
logger,
metrics::add_attributes,
tracing::{self, Instrument},
};

Expand Down Expand Up @@ -69,10 +70,10 @@ pub async fn get_metrics(
.change_context(AnalyticsError::UnknownError)?
{
let data = data?;
let attributes = &[
metrics::request::add_attributes("metric_type", metric.to_string()),
metrics::request::add_attributes("source", pool.to_string()),
];
let attributes = &add_attributes([
("metric_type", metric.to_string()),
("source", pool.to_string()),
]);

let value = u64::try_from(data.len());
if let Ok(val) = value {
Expand Down
3 changes: 2 additions & 1 deletion crates/hyperswitch_interfaces/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use common_utils::{
};
use hyperswitch_domain_models::router_data::{ErrorResponse, RouterData};
use masking::Maskable;
use router_env::metrics::add_attributes;
use serde_json::json;

use crate::{
Expand Down Expand Up @@ -87,7 +88,7 @@ pub trait ConnectorIntegration<T, Req, Resp>: ConnectorIntegrationAny<T, Req, Re
metrics::UNIMPLEMENTED_FLOW.add(
&metrics::CONTEXT,
1,
&[metrics::add_attributes("connector", req.connector.clone())],
&add_attributes([("connector", req.connector.clone())]),
);
Ok(None)
}
Expand Down
10 changes: 1 addition & 9 deletions crates/hyperswitch_interfaces/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
//! Metrics interface

use router_env::{counter_metric, global_meter, metrics_context, opentelemetry};
use router_env::{counter_metric, global_meter, metrics_context};

metrics_context!(CONTEXT);
global_meter!(GLOBAL_METER, "ROUTER_API");

counter_metric!(UNIMPLEMENTED_FLOW, GLOBAL_METER);

/// fn add attributes
pub fn add_attributes<T: Into<opentelemetry::Value>>(
key: &'static str,
value: T,
) -> opentelemetry::KeyValue {
opentelemetry::KeyValue::new(key, value)
}
3 changes: 2 additions & 1 deletion crates/router/src/connector/boku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use diesel_models::enums;
use error_stack::{report, Report, ResultExt};
use masking::{ExposeInterface, PeekInterface, Secret, WithType};
use ring::hmac;
use router_env::metrics::add_attributes;
use roxmltree;
use time::OffsetDateTime;
use transformers as boku;
Expand Down Expand Up @@ -665,7 +666,7 @@ fn get_xml_deserialized(
metrics::RESPONSE_DESERIALIZATION_FAILURE.add(
&metrics::CONTEXT,
1,
&[metrics::request::add_attributes("connector", "boku")],
&add_attributes([("connector", "boku")]),
);

let response_data = String::from_utf8(res.response.to_vec())
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/braintree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl ConnectorCommon for Braintree {
Err(error_msg) => {
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
logger::error!(deserialization_error =? error_msg);
utils::handle_json_response_deserialization_failure(res, "braintree".to_owned())
utils::handle_json_response_deserialization_failure(res, "braintree")
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/router/src/connector/cybersource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,7 @@ impl ConnectorCommon for Cybersource {
Err(error_msg) => {
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
router_env::logger::error!(deserialization_error =? error_msg);
crate::utils::handle_json_response_deserialization_failure(
res,
"cybersource".to_owned(),
)
crate::utils::handle_json_response_deserialization_failure(res, "cybersource")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/noon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl ConnectorCommon for Noon {
Err(error_message) => {
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
logger::error!(deserialization_error =? error_message);
utils::handle_json_response_deserialization_failure(res, "noon".to_owned())
utils::handle_json_response_deserialization_failure(res, "noon")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/payme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl ConnectorCommon for Payme {
Err(error_msg) => {
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
router_env::logger::error!(deserialization_error =? error_msg);
handle_json_response_deserialization_failure(res, "payme".to_owned())
handle_json_response_deserialization_failure(res, "payme")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/rapyd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl ConnectorCommon for Rapyd {
Err(error_msg) => {
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
logger::error!(deserialization_error =? error_msg);
utils::handle_json_response_deserialization_failure(res, "rapyd".to_owned())
utils::handle_json_response_deserialization_failure(res, "rapyd")
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/router/src/connector/threedsecureio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ impl ConnectorCommon for Threedsecureio {
}
Err(err) => {
router_env::logger::error!(deserialization_error =? err);
utils::handle_json_response_deserialization_failure(
res,
"threedsecureio".to_owned(),
)
utils::handle_json_response_deserialization_failure(res, "threedsecureio")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/router/src/connector/trustpay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl ConnectorCommon for Trustpay {
Err(error_msg) => {
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
logger::error!(deserialization_error =? error_msg);
utils::handle_json_response_deserialization_failure(res, "trustpay".to_owned())
utils::handle_json_response_deserialization_failure(res, "trustpay")
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use error_stack::{report, FutureExt, ResultExt};
use futures::future::try_join_all;
use masking::{PeekInterface, Secret};
use pm_auth::connector::plaid::transformers::PlaidAuthType;
use router_env::metrics::add_attributes;
use uuid::Uuid;

use crate::{
Expand Down Expand Up @@ -1009,10 +1010,10 @@ pub async fn create_payment_connector(
metrics::MCA_CREATE.add(
&metrics::CONTEXT,
1,
&[
metrics::request::add_attributes("connector", req.connector_name.to_string()),
metrics::request::add_attributes("merchant", merchant_id.to_string()),
],
&add_attributes([
("connector", req.connector_name.to_string()),
("merchant", merchant_id.to_string()),
]),
);

let mca_response = mca.try_into()?;
Expand Down
6 changes: 3 additions & 3 deletions crates/router/src/core/api_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use common_utils::date_time;
use diesel_models::{api_keys::ApiKey, enums as storage_enums};
use error_stack::{report, ResultExt};
use masking::{PeekInterface, StrongSecret};
use router_env::{instrument, tracing};
use router_env::{instrument, metrics::add_attributes, tracing};

use crate::{
configs::settings,
Expand Down Expand Up @@ -151,7 +151,7 @@ pub async fn create_api_key(
metrics::API_KEY_CREATED.add(
&metrics::CONTEXT,
1,
&[metrics::request::add_attributes("merchant", merchant_id)],
&add_attributes([("merchant", merchant_id)]),
);

// Add process to process_tracker for email reminder, only if expiry is set to future date
Expand Down Expand Up @@ -236,7 +236,7 @@ pub async fn add_api_key_expiry_task(
metrics::TASKS_ADDED_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::request::add_attributes("flow", "ApiKeyExpiry")],
&add_attributes([("flow", "ApiKeyExpiry")]),
);

Ok(())
Expand Down
9 changes: 3 additions & 6 deletions crates/router/src/core/mandate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use common_utils::{ext_traits::Encode, id_type};
use diesel_models::{enums as storage_enums, Mandate};
use error_stack::{report, ResultExt};
use futures::future;
use router_env::{instrument, logger, tracing};
use router_env::{instrument, logger, metrics::add_attributes, tracing};

use super::payments::helpers as payment_helper;
use crate::{
Expand Down Expand Up @@ -404,10 +404,7 @@ where
metrics::SUBSEQUENT_MANDATE_PAYMENT.add(
&metrics::CONTEXT,
1,
&[metrics::request::add_attributes(
"connector",
mandate.connector,
)],
&add_attributes([("connector", mandate.connector)]),
);
Ok(Some(mandate_id.clone()))
}
Expand Down Expand Up @@ -463,7 +460,7 @@ where
metrics::MANDATE_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::request::add_attributes("connector", connector)],
&add_attributes([("connector", connector)]),
);
Ok(Some(res_mandate_id))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use euclid::{
use hyperswitch_constraint_graph as cgraph;
use kgraph_utils::transformers::IntoDirValue;
use masking::Secret;
use router_env::{instrument, tracing};
use router_env::{instrument, metrics::add_attributes, tracing};
use strum::IntoEnumIterator;

use super::surcharge_decision_configs::{
Expand Down Expand Up @@ -3958,7 +3958,7 @@ impl TempLockerCardSupport {
metrics::TASKS_ADDED_COUNT.add(
&metrics::CONTEXT,
1,
&[request::add_attributes("flow", "DeleteTokenizeData")],
&add_attributes([("flow", "DeleteTokenizeData")]),
);
Ok(card)
}
Expand Down
7 changes: 2 additions & 5 deletions crates/router/src/core/payment_methods/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use common_utils::{
};
use error_stack::{report, ResultExt};
use masking::PeekInterface;
use router_env::{instrument, tracing};
use router_env::{instrument, metrics::add_attributes, tracing};
use scheduler::{types::process_data, utils as process_tracker_utils};

#[cfg(feature = "payouts")]
Expand Down Expand Up @@ -1232,10 +1232,7 @@ pub async fn retry_delete_tokenize(
metrics::TASKS_RESET_COUNT.add(
&metrics::CONTEXT,
1,
&[metrics::request::add_attributes(
"flow",
"DeleteTokenizeData",
)],
&add_attributes([("flow", "DeleteTokenizeData")]),
);
retry_schedule
}
Expand Down
Loading
Loading