Skip to content

Commit 7b579e8

Browse files
committed
fix(api): use matched path to reduce metric cardinality
1 parent cbf9bdc commit 7b579e8

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

engine/packages/api-builder/src/middleware.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ pub async fn http_logging_middleware(
9191

9292
let method = req.method().clone();
9393
let uri = req.uri().clone();
94-
let path = uri.path().to_string();
94+
// Used matched path if it exists
95+
let path = if let Some(path) = req.extensions().get::<MatchedPath>() {
96+
path.as_str().to_string()
97+
} else {
98+
uri.path().to_string()
99+
};
95100
let protocol = req.version();
96101

97102
// Log request metadata
@@ -111,10 +116,6 @@ pub async fn http_logging_middleware(
111116
.with_label_values(&[router_name, method.as_str(), path.as_str()])
112117
.inc();
113118

114-
// Clone values for the async block
115-
let method_clone = method.clone();
116-
let path_clone = path.clone();
117-
118119
// Process the request
119120
let response = async move {
120121
let mut response = next.run(req).await;
@@ -185,7 +186,7 @@ pub async fn http_logging_middleware(
185186
);
186187

187188
// Update metrics
188-
metrics::API_REQUEST_PENDING.with_label_values(&[router_name, method_clone.as_str(), path_clone.as_str()]).dec();
189+
metrics::API_REQUEST_PENDING.with_label_values(&[router_name, method.as_str(), path.as_str()]).dec();
189190

190191
let error_str: String = if status.is_success() {
191192
String::new()
@@ -195,12 +196,12 @@ pub async fn http_logging_middleware(
195196
String::new()
196197
};
197198
metrics::API_REQUEST_DURATION
198-
.with_label_values(&[router_name, method_clone.as_str(), path_clone.as_str(), status.as_str(), error_str.as_str()])
199+
.with_label_values(&[router_name, method.as_str(), path.as_str(), status.as_str(), error_str.as_str()])
199200
.observe(duration);
200201

201202
if !status.is_success() {
202203
metrics::API_REQUEST_ERRORS
203-
.with_label_values(&[router_name, method_clone.as_str(), path_clone.as_str(), status.as_str(), error_str.as_str()])
204+
.with_label_values(&[router_name, method.as_str(), path.as_str(), status.as_str(), error_str.as_str()])
204205
.inc();
205206
}
206207

0 commit comments

Comments
 (0)