0

我有一个具有如下 URL 的 Node JS 应用程序:

/app/:vehicleNumber/details

它在 URL 中动态获取车辆编号。

用于 HTTP 调用的基于 prom-client 的指标 API 将所有 URL 作为单独的 URL 返回,而不是将它们作为单个 URL 并以 vehicleNumber 作为变量。

http_request_duration_seconds_bucket{le="0.003",status_code="200",method="GET",path="/app/GJ98T0006/details"}
http_request_duration_seconds_bucket{le="0.003",status_code="200",method="GET",path="/app/KA28T6511/details"}
.....

我想要基于单个 URL 的计数。例如

http_request_duration_seconds_bucket{le="0.003",status_code="200",method="GET",path="/app/{var}/details"}

当 UUID 存在于 URL 中但不存在于 URL 中的车辆编号时,就会发生这种情况。

4

1 回答 1

1

假设您使用的是 Express,您可能需要以下内容:

let path = null;
if (req.route && req.route.path) {
    path = req.baseUrl + req.route.path;
} else if (req.baseUrl) {
    path = req.baseUrl;
} else {
    /*
     * This request probably matched no routes. The
     * cardinality of the set of URLs that will match no
     * routes is infinite, so we'll just use a static value.
     */
    path = '(no match)';
}

path/app/:vehicleNumber/details。对于您的用例,该req.route部分可能不是必需的,但它不会受到伤害——如果您正在使用它express.Router({ mergeParams: true })来组合您的路线,它就会发挥作用。

于 2021-10-20T23:17:44.683 回答