Skip to content

Commit

Permalink
fix: content-type header for metrics endpoints
Browse files Browse the repository at this point in the history
    Issue #13:
    - JSON endpoints now return the correct Content-Type: `application/json; charset=utf-8`.

    Additionally:
    - Added tests to verify the correctness of the Content-Type header.

    This fix ensures proper header formatting and alignment with client expectations.
  • Loading branch information
vakhov committed Dec 28, 2024
1 parent fc13434 commit f18401a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
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
12 changes: 11 additions & 1 deletion roles/metrics-export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ 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()

local content_type = 'application/json'

if req.httpd.options.charset then
content_type = string.format('%s; charset=%s', content_type, req.httpd.options.charset)
end

local response = req:render({ text = json_data })
response.headers['content-type'] = content_type
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

0 comments on commit f18401a

Please sign in to comment.