Skip to content

[wip] fix: content-type header for metrics endpoints #30

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ tags
doc/apidoc/
.idea
*.swp
.DS_Store
10 changes: 9 additions & 1 deletion roles/metrics-export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ end
local http_handlers = {
json = function(req)
local json_exporter = require('metrics.plugins.json')
return req:render({ text = json_exporter.export() })
local json_data = json_exporter.export()

if type(json_data) ~= "string" then
json_data = require('json').encode(json_data)
end

local response = req:render({ json = {} })
response.body = json_data
return response
end,
prometheus = function(...)
local http_handler = require('metrics.plugins.prometheus').collect_http
Expand Down
50 changes: 50 additions & 0 deletions test/unit/http_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1140,3 +1140,53 @@ for name, case in pairs(test_tls_cases) do
end
end
end

local function assert_content_type(uri, expected_content_type, tls_opts)
local response = http_client:get(uri, tls_opts)
t.assert_equals(response.status, 200)
t.assert_equals(response.headers['content-type'], expected_content_type)
end

local test_content_type_cases = {
['json'] = {
cfg = {
http = {
{
listen = 8081,
endpoints = {
{
path = "/json_metrics",
format = "json",
},
},
},
},
},
expected_url = "http://127.0.0.1:8081/json_metrics",
expected_content_type = "application/json; charset=utf-8",
},
['prometheus'] = {
cfg = {
http = {
{
listen = 8081,
endpoints = {
{
path = "/prometheus_metrics",
format = "prometheus",
},
},
},
},
},
expected_url = "http://127.0.0.1:8081/prometheus_metrics",
expected_content_type = "text/plain; charset=utf8",
},
}

for name, case in pairs(test_content_type_cases) do
g['test_content_type_' .. name] = function(cg)
cg.role.apply(case.cfg)
assert_content_type(case.expected_url, case.expected_content_type)
end
end