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 fa3e31b
Show file tree
Hide file tree
Showing 3 changed files with 52 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
2 changes: 1 addition & 1 deletion roles/metrics-export.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ end
local http_handlers = {
json = function(req)
local json_exporter = require('metrics.plugins.json')
return req:render({ text = json_exporter.export() })
return req:render({ json = json_exporter.export() })
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) -- Проверка 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=utf-8",
},
}

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 fa3e31b

Please sign in to comment.