Skip to content

roles: add log_requests option #211

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added

- `log_requests` option into `roles.httpd`.

### Changed

### Fixed
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,16 @@ roles_cfg:
listen: 8081
additional:
listen: '127.0.0.1:8082'
log_requests: 'verbose'
```

Server address should be provided either as a URI or as a single port
(in this case, `0.0.0.0` address is used).

You can configure HTTP request logging level using `log_requests` parameter (e.g. "verbose").
Supported values are "debug", "verbose", "info", "warn" and "error".
By default, requests are logged at "info" level.

User can access every working HTTP server from the configuration by name,
using `require('roles.httpd').get_server(name)` method.
If the `name` argument is `nil`, the default server is returned
Expand Down
20 changes: 20 additions & 0 deletions roles/httpd.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local checks = require('checks')
local urilib = require('uri')
local log = require('log')
local http_server = require('http.server')

local M = {
Expand Down Expand Up @@ -75,7 +76,26 @@ end
-- parse_params returns table with set options from config to pass
-- it into new() function.
local function parse_params(node)
local log_requests = node.log_requests
if type(log_requests) == "string" then
local level = log_requests:lower()
if level == "error" then
log_requests = log.error
elseif level == "warn" then
log_requests = log.warn
elseif level == "info" then
log_requests = log.info
elseif level == "verbose" then
log_requests = log.verbose
elseif level == "debug" then
log_requests = log.debug
else
error("invalid log_requests: " .. log_requests)
end
end

return {
log_requests = log_requests,
ssl_cert_file = node.ssl_cert_file,
ssl_key_file = node.ssl_key_file,
ssl_password = node.ssl_password,
Expand Down
58 changes: 56 additions & 2 deletions test/integration/httpd_role_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@ local http_client = require('http.client').new()

local helpers = require('test.helpers')

local g = t.group(nil, t.helpers.matrix({use_tls = {true, false}}))
local LOG_LEVEL = {
INFO = 5,
VERBOSE = 6,
DEBUG = 7,
}

local g = t.group(nil, t.helpers.matrix({
use_tls = {true, false},
log_level = {
LOG_LEVEL.INFO,
LOG_LEVEL.VERBOSE,
LOG_LEVEL.DEBUG,
},
}))

local ssl_data_dir = fio.abspath(fio.pathjoin(helpers.get_testdir_path(), "ssl_data"))

Expand All @@ -36,10 +49,16 @@ local config = {
['roles.httpd'] = {
default = {
listen = 13000,
log_requests = 'info',
},
additional = {
listen = 13001,
}
log_requests = 'verbose',
},
additional_debug = {
listen = 13002,
log_requests = 'debug',
},
},
['test.mocks.mock_role'] = {
{
Expand All @@ -49,6 +68,10 @@ local config = {
id = 2,
name = 'additional',
},
{
id = 3,
name = 'additional_debug',
}
},
},
instances = {
Expand All @@ -75,6 +98,8 @@ g.before_each(function(cg)

local dir = treegen.prepare_directory({}, {})

os.setenv('TT_LOG_LEVEL', tostring(cg.params.log_level))

local cfg = config
if cg.params.use_tls then
cfg = tls_config
Expand Down Expand Up @@ -146,3 +171,32 @@ g.test_change_server_addr_on_the_run = function(cg)
t.assert_equals(resp.status, 200, 'response not 200')
t.assert_equals(resp.body, 'pong')
end

g.test_log_requests = function(cg)
t.skip_if(cg.params.use_tls)

local function make_request(port)
local resp = http_client:get(string.format('http://localhost:%d/ping', port))
t.assert_equals(resp.status, 200, 'response not 200')
end

local function assert_should_log(expected)
local grep_res = cg.server:grep_log('GET /ping', math.huge)
if expected then
t.assert(grep_res)
else
t.assert_not(grep_res)
end
end

local log_level = tonumber(cg.params.log_level)

make_request(13002)
assert_should_log(log_level >= LOG_LEVEL.DEBUG)

make_request(13001)
assert_should_log(log_level >= LOG_LEVEL.VERBOSE)

make_request(13000)
assert_should_log(log_level >= LOG_LEVEL.INFO)
end
32 changes: 32 additions & 0 deletions test/unit/httpd_role_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local g = t.group()
local httpd_role = require('roles.httpd')
local helpers = require('test.helpers')
local fio = require('fio')
local log = require('log')

local ssl_data_dir = fio.abspath(fio.pathjoin(helpers.get_testdir_path(), "ssl_data"))

Expand Down Expand Up @@ -209,8 +210,18 @@ local validation_cases = {
},
err = "ssl_key_file and ssl_cert_file must be set to enable TLS",
},
["invalid_log_requests"] = {
cfg = {
server = {
listen = "localhost:123",
log_requests = "yes",
},
},
err = "invalid log_requests",
}
}


for name, case in pairs(validation_cases) do
local test_name = ('test_validate_http_%s%s'):format(
(case.err ~= nil) and 'fails_on_' or 'success_for_',
Expand Down Expand Up @@ -311,3 +322,24 @@ g.test_edit_server_address = function()
t.assert(result)
t.assert_equals(result.port, cfg[httpd_role.DEFAULT_SERVER_NAME].listen)
end

g.test_log_requests = function()
local cfg = {
server1 = {
listen = 13002,
log_requests = false,
},
server2 = {
listen = 13003,
log_requests = log.verbose,
},
}

httpd_role.apply(cfg)

for name, body in pairs(cfg) do
local res = httpd_role.get_server(name)
t.assert(res)
t.assert_equals(res.options.log_requests, body.log_requests)
end
end
Loading