Skip to content

Commit

Permalink
fix(metrics): remove unused labels, make the metrics smaller (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
wklken committed Oct 23, 2023
1 parent b3c7dd2 commit 8b80aec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
37 changes: 8 additions & 29 deletions src/apisix/plugins/bk-response-check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ function _M.init()
metric_api_requests_total = prometheus_registry:counter(
"apigateway_api_requests_total",
"How many HTTP requests processed, partitioned by status code, method and HTTP path.", {
"gateway",
"api_name",
"stage_name",
"resource_name",
"service_name",
"method",
"matched_uri",
"status",
"proxy_phase",
"proxy_error",
Expand All @@ -78,13 +74,9 @@ function _M.init()
metric_api_request_duration = prometheus_registry:histogram(
"apigateway_api_request_duration_milliseconds",
"How long it took to process the request, partitioned by status code, method and HTTP path.", {
"gateway",
"api_name",
"stage_name",
"resource_name",
"service_name",
"method",
"matched_uri",
}, {
100,
300,
Expand All @@ -95,12 +87,10 @@ function _M.init()

metric_app_requests_total = prometheus_registry:counter(
"apigateway_app_requests_total", "How many HTTP requests per app_code/api/resource.", {
"gateway",
"app_code",
"api_name",
"stage_name",
"resource_name",
"service_name",
}
)
end
Expand All @@ -116,19 +106,18 @@ function _M.log(conf, ctx)
local api_name = ctx.var.bk_gateway_name or ""
local stage_name = ctx.var.bk_stage_name or ""
local resource_name = ctx.var.bk_resource_name or ""
local service_name = ctx.var.bk_service_name or ""
local instance = ctx.var.instance_id or ""
local method = ctx.var.method
local proxy_phase = ctx.var.proxy_phase or ""
local status = ctx.var.status
local proxy_error = ctx.var.proxy_error or "0"

-- NOTE: change from path to matched_uri, to decrease the metrics(use /a/{id} instead of /a/123)
-- local path = ctx.var.uri
local matched_uri = ""
if ctx.curr_req_matched then
matched_uri = ctx.curr_req_matched._path or ""
end
-- 2023-10-18
-- remove unused labels: service_name/method/matched_uri
-- remove gateway=instance label, use cluster_id and namespace to identify the gateway instance

-- TODO:
-- 1. api_name to gateway_name
-- 2. all *_name to *_id
-- 3. make the name shorter `bk_apigateway_apigateway_api_request_duration_milliseconds_bucket`

local status_label = ""
if status then
Expand All @@ -137,13 +126,9 @@ function _M.log(conf, ctx)

metric_api_requests_total:inc(
1, {
instance,
api_name,
stage_name,
resource_name,
service_name,
method,
matched_uri,
status_label,
proxy_phase,
proxy_error,
Expand All @@ -153,26 +138,20 @@ function _M.log(conf, ctx)
if ctx.var.request_time then
metric_api_request_duration:observe(
ctx.var.request_time * 1000, {
instance,
api_name,
stage_name,
resource_name,
service_name,
method,
matched_uri,
}
)
end

if ctx.var.bk_app_code then
metric_app_requests_total:inc(
1, {
instance,
ctx.var.bk_app_code,
api_name,
stage_name,
resource_name,
service_name,
}
)
end
Expand Down
12 changes: 2 additions & 10 deletions src/apisix/tests/test-bk-response-check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ describe(
"should log the metrics", function()
ctx = {
var = {
gateway = "gateway",
api_name = "api_name",
stage_name = "stage_name",
resource_name = "resource_name",
service_name = "service_name",
method = "method",
matched_uri = "matched_uri",
status = 200,
proxy_phase = "proxy_phase",
proxy_error = "proxy_error",
Expand All @@ -86,19 +82,15 @@ describe(

local api_requests_total = prometheus.registry["apigateway_api_requests_total"]
local expected_label_names = {
'gateway',
'api_name',
'stage_name',
'resource_name',
'service_name',
'method',
'matched_uri',
'status',
'proxy_phase',
'proxy_error' ,
}
local expected_key = 'apigateway_api_requests_total{gateway="",api_name="",stage_name="",' ..
'resource_name="",service_name="",method="method",matched_uri="matched_uri",status="200",' ..
local expected_key = 'apigateway_api_requests_total{api_name="",stage_name="",' ..
'resource_name="",status="200",' ..
'proxy_phase="proxy_phase",proxy_error="proxy_error"}'

assert.is_same(expected_label_names, api_requests_total["label_names"])
Expand Down

0 comments on commit 8b80aec

Please sign in to comment.