Skip to content

Commit

Permalink
fix: skip sending status code after body is sent
Browse files Browse the repository at this point in the history
This commit fixes a bug where the plugin tries to send HTTP status code
after the response body is sent for `/metrics` endpoint.

This resulted in error logs in Kong.
A test has been added to ensure that no error logs are generated when
`/metrics` is scraped by Prometheus.

Another test has been added to ensure that the response body only
contains either metrics or comments describing the metrics.
This test is added to catch regression for
#4077, where Kong injected HTML
generated by the default Lapis handler.

Fixes #32 
From #33
  • Loading branch information
hbagdi committed Dec 14, 2018
1 parent bb649ad commit 3e4c63d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
1 change: 0 additions & 1 deletion kong/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ local function collect()
end

prometheus:collect()
return kong.response.exit(200)
end


Expand Down
33 changes: 32 additions & 1 deletion spec/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Plugin: prometheus (access)", function()
proxy_client:close()
end
if admin_client then
proxy_client:close()
admin_client:close()
end

helpers.stop_kong()
Expand Down Expand Up @@ -96,4 +96,35 @@ describe("Plugin: prometheus (access)", function()
assert.not_match("[error]", line, nil, true)
end
end)

it("does not log error during a scrape", function()
-- cleanup logs
local test_error_log_path = helpers.test_conf.nginx_err_logs
os.execute(":> " .. test_error_log_path)

local res = assert(admin_client:send {
method = "GET",
path = "/metrics",
})
assert.res_status(200, res)

-- make sure no errors
local logs = pl_file.read(test_error_log_path)
for line in logs:gmatch("[^\r\n]+") do
assert.not_match("[error]", line, nil, true)
end
end)

it("scrape response has metrics and comments only", function()
local res = assert(admin_client:send {
method = "GET",
path = "/metrics",
})
local body = assert.res_status(200, res)

for line in body:gmatch("[^\r\n]+") do
assert.matches("^[#|kong]", line)
end

end)
end)

0 comments on commit 3e4c63d

Please sign in to comment.