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

fix(prometheus): expose metrics in no service route #11781

Merged
merged 6 commits into from Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,3 @@
message: "Expose metrics for serviceless routes"
type: bugfix
scope: Plugin
7 changes: 3 additions & 4 deletions kong/plugins/prometheus/exporter.lua
Expand Up @@ -273,17 +273,16 @@ local function log(message, serialized)
return
end

local service_name
local service_name = ""
if message and message.service then
service_name = message.service.name or message.service.host
else
-- do not record any stats if the service is not present
return
end

local route_name
if message and message.route then
route_name = message.route.name or message.route.id
else
return
end

local consumer = ""
Expand Down
52 changes: 52 additions & 0 deletions spec/03-plugins/26-prometheus/05-metrics_spec.lua
Expand Up @@ -64,6 +64,25 @@ for _, strategy in helpers.each_strategy() do
}
}

local route1 = bp.routes:insert{
name = "serverless",
protocols = {"https"},
hosts = {"status.example.com"},
paths = {"/serverless"},
no_service = true,
}

assert(bp.plugins:insert {
name = "request-termination",
route = { id = route1.id },
config = {
status_code = 200,
message = "request terminated by request-termination plugin",
echo = true,
},
})


bp.plugins:insert{
name = "prometheus", -- globally enabled
config = {
Expand Down Expand Up @@ -158,5 +177,38 @@ for _, strategy in helpers.each_strategy() do
assert.matches('kong_nginx_connections_total{node_id="' .. UUID_PATTERN .. '",subsystem="' .. ngx.config.subsystem .. '",state="%w+"} %d+', body)
end)

it("expose metrics in no service route", function()
local res = assert(proxy_ssl_client:send{
method = "GET",
path = "/serverless",
headers = {
["Host"] = "status.example.com"
}
})
assert.res_status(200, res)

local res = assert(proxy_ssl_client:send{
method = "GET",
path = "/metrics",
headers = {
["Host"] = "status.example.com"
}
})
assert.res_status(200, res)

helpers.wait_until(function()
local res = assert(admin_ssl_client:send{
method = "GET",
path = "/metrics"
})
local body = assert.res_status(200, res)

assert.matches('kong_nginx_metric_errors_total 0', body, nil, true)

return body:find('kong_http_requests_total{service="",route="serverless",code="200",source="kong",consumer=""} 1',
nil, true)
end)
end)

end)
end
11 changes: 9 additions & 2 deletions spec/fixtures/blueprints.lua
Expand Up @@ -180,8 +180,15 @@ function _M.new(db)
end)

res.routes = new_blueprint(db.routes, function(overrides)
return {
service = overrides.service or res.services:insert(),
local service
if overrides.no_service then
service = nil
overrides.no_service = nil
else
service = overrides.service or res.services:insert()
end
return {
service = service,
}
end)

Expand Down