Skip to content

Commit d2092dc

Browse files
authored
refactor: add basic counter metrics for IMC (#5006)
1 parent 010e6fe commit d2092dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+301
-316
lines changed

crates/analytics/src/api_event/core.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use common_utils::errors::ReportSwitchExt;
1212
use error_stack::ResultExt;
1313
use router_env::{
1414
instrument, logger,
15+
metrics::add_attributes,
1516
tracing::{self, Instrument},
1617
};
1718

@@ -135,10 +136,10 @@ pub async fn get_api_event_metrics(
135136
.change_context(AnalyticsError::UnknownError)?
136137
{
137138
let data = data?;
138-
let attributes = &[
139-
metrics::request::add_attributes("metric_type", metric.to_string()),
140-
metrics::request::add_attributes("source", pool.to_string()),
141-
];
139+
let attributes = &add_attributes([
140+
("metric_type", metric.to_string()),
141+
("source", pool.to_string()),
142+
]);
142143

143144
let value = u64::try_from(data.len());
144145
if let Ok(val) = value {

crates/analytics/src/disputes/core.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use api_models::analytics::{
1111
use error_stack::ResultExt;
1212
use router_env::{
1313
logger,
14+
metrics::add_attributes,
1415
tracing::{self, Instrument},
1516
};
1617

@@ -70,10 +71,10 @@ pub async fn get_metrics(
7071
.change_context(AnalyticsError::UnknownError)?
7172
{
7273
let data = data?;
73-
let attributes = &[
74-
metrics::request::add_attributes("metric_type", metric.to_string()),
75-
metrics::request::add_attributes("source", pool.to_string()),
76-
];
74+
let attributes = &add_attributes([
75+
("metric_type", metric.to_string()),
76+
("source", pool.to_string()),
77+
]);
7778

7879
let value = u64::try_from(data.len());
7980
if let Ok(val) = value {

crates/analytics/src/metrics/request.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
pub fn add_attributes<T: Into<router_env::opentelemetry::Value>>(
2-
key: &'static str,
3-
value: T,
4-
) -> router_env::opentelemetry::KeyValue {
5-
router_env::opentelemetry::KeyValue::new(key, value)
6-
}
1+
use std::time;
2+
3+
use router_env::metrics::add_attributes;
74

85
#[inline]
96
pub async fn record_operation_time<F, R, T>(
@@ -17,19 +14,17 @@ where
1714
T: ToString,
1815
{
1916
let (result, time) = time_future(future).await;
20-
let attributes = &[
21-
add_attributes("metric_name", metric_name.to_string()),
22-
add_attributes("source", source.to_string()),
23-
];
17+
let attributes = &add_attributes([
18+
("metric_name", metric_name.to_string()),
19+
("source", source.to_string()),
20+
]);
2421
let value = time.as_secs_f64();
2522
metric.record(&super::CONTEXT, value, attributes);
2623

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

31-
use std::time;
32-
3328
#[inline]
3429
pub async fn time_future<F, R>(future: F) -> (R, time::Duration)
3530
where

crates/analytics/src/payments/core.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use common_utils::errors::CustomResult;
1313
use error_stack::ResultExt;
1414
use router_env::{
1515
instrument, logger,
16+
metrics::add_attributes,
1617
tracing::{self, Instrument},
1718
};
1819

@@ -120,10 +121,10 @@ pub async fn get_metrics(
120121
match task_type {
121122
TaskType::MetricTask(metric, data) => {
122123
let data = data?;
123-
let attributes = &[
124-
metrics::request::add_attributes("metric_type", metric.to_string()),
125-
metrics::request::add_attributes("source", pool.to_string()),
126-
];
124+
let attributes = &add_attributes([
125+
("metric_type", metric.to_string()),
126+
("source", pool.to_string()),
127+
]);
127128

128129
let value = u64::try_from(data.len());
129130
if let Ok(val) = value {
@@ -172,10 +173,10 @@ pub async fn get_metrics(
172173
}
173174
TaskType::DistributionTask(distribution, data) => {
174175
let data = data?;
175-
let attributes = &[
176-
metrics::request::add_attributes("distribution_type", distribution.to_string()),
177-
metrics::request::add_attributes("source", pool.to_string()),
178-
];
176+
let attributes = &add_attributes([
177+
("distribution_type", distribution.to_string()),
178+
("source", pool.to_string()),
179+
]);
179180

180181
let value = u64::try_from(data.len());
181182
if let Ok(val) = value {

crates/analytics/src/refunds/core.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use api_models::analytics::{
1111
use error_stack::ResultExt;
1212
use router_env::{
1313
logger,
14+
metrics::add_attributes,
1415
tracing::{self, Instrument},
1516
};
1617

@@ -69,10 +70,10 @@ pub async fn get_metrics(
6970
.change_context(AnalyticsError::UnknownError)?
7071
{
7172
let data = data?;
72-
let attributes = &[
73-
metrics::request::add_attributes("metric_type", metric.to_string()),
74-
metrics::request::add_attributes("source", pool.to_string()),
75-
];
73+
let attributes = &add_attributes([
74+
("metric_type", metric.to_string()),
75+
("source", pool.to_string()),
76+
]);
7677

7778
let value = u64::try_from(data.len());
7879
if let Ok(val) = value {

crates/hyperswitch_interfaces/src/api.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use common_utils::{
66
};
77
use hyperswitch_domain_models::router_data::{ErrorResponse, RouterData};
88
use masking::Maskable;
9+
use router_env::metrics::add_attributes;
910
use serde_json::json;
1011

1112
use crate::{
@@ -87,7 +88,7 @@ pub trait ConnectorIntegration<T, Req, Resp>: ConnectorIntegrationAny<T, Req, Re
8788
metrics::UNIMPLEMENTED_FLOW.add(
8889
&metrics::CONTEXT,
8990
1,
90-
&[metrics::add_attributes("connector", req.connector.clone())],
91+
&add_attributes([("connector", req.connector.clone())]),
9192
);
9293
Ok(None)
9394
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
//! Metrics interface
22
3-
use router_env::{counter_metric, global_meter, metrics_context, opentelemetry};
3+
use router_env::{counter_metric, global_meter, metrics_context};
44

55
metrics_context!(CONTEXT);
66
global_meter!(GLOBAL_METER, "ROUTER_API");
77

88
counter_metric!(UNIMPLEMENTED_FLOW, GLOBAL_METER);
9-
10-
/// fn add attributes
11-
pub fn add_attributes<T: Into<opentelemetry::Value>>(
12-
key: &'static str,
13-
value: T,
14-
) -> opentelemetry::KeyValue {
15-
opentelemetry::KeyValue::new(key, value)
16-
}

crates/router/src/connector/boku.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use diesel_models::enums;
66
use error_stack::{report, Report, ResultExt};
77
use masking::{ExposeInterface, PeekInterface, Secret, WithType};
88
use ring::hmac;
9+
use router_env::metrics::add_attributes;
910
use roxmltree;
1011
use time::OffsetDateTime;
1112
use transformers as boku;
@@ -665,7 +666,7 @@ fn get_xml_deserialized(
665666
metrics::RESPONSE_DESERIALIZATION_FAILURE.add(
666667
&metrics::CONTEXT,
667668
1,
668-
&[metrics::request::add_attributes("connector", "boku")],
669+
&add_attributes([("connector", "boku")]),
669670
);
670671

671672
let response_data = String::from_utf8(res.response.to_vec())

crates/router/src/connector/braintree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl ConnectorCommon for Braintree {
155155
Err(error_msg) => {
156156
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
157157
logger::error!(deserialization_error =? error_msg);
158-
utils::handle_json_response_deserialization_failure(res, "braintree".to_owned())
158+
utils::handle_json_response_deserialization_failure(res, "braintree")
159159
}
160160
}
161161
}

crates/router/src/connector/cybersource.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,7 @@ impl ConnectorCommon for Cybersource {
229229
Err(error_msg) => {
230230
event_builder.map(|event| event.set_error(serde_json::json!({"error": res.response.escape_ascii().to_string(), "status_code": res.status_code})));
231231
router_env::logger::error!(deserialization_error =? error_msg);
232-
crate::utils::handle_json_response_deserialization_failure(
233-
res,
234-
"cybersource".to_owned(),
235-
)
232+
crate::utils::handle_json_response_deserialization_failure(res, "cybersource")
236233
}
237234
}
238235
}

0 commit comments

Comments
 (0)