Skip to content
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

THREESCALE-10894 concat filtered services into a single log #1493

Merged
merged 4 commits into from
Sep 30, 2024
Merged
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
12 changes: 10 additions & 2 deletions gateway/src/apicast/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local tostring = tostring
local next = next
local lower = string.lower
local insert = table.insert
local concat = table.concat
local setmetatable = setmetatable
local null = ngx.null

Expand Down Expand Up @@ -140,7 +141,8 @@ end

function _M.filter_services(services, subset)
local selected_services = {}
local service_regexp_filter = env.value("APICAST_SERVICES_FILTER_BY_URL")
local filtered_services = {}
local service_regexp_filter = env.value("APICAST_SERVICES_FILTER_BY_URL")

if service_regexp_filter then
-- Checking that the regexp sent is correct, if not an empty service list
Expand All @@ -163,9 +165,15 @@ function _M.filter_services(services, subset)
if service:match_host(service_regexp_filter) or subset[service.id] then
insert(selected_services, service)
else
ngx.log(ngx.WARN, 'filtering out service ', service.id)
insert(filtered_services, service.id)
end
end

-- Log all filtered services in a single log
if #filtered_services > 0 then
ngx.log(ngx.WARN, "filtering out services: ", concat(filtered_services, ", "))
end

return selected_services
end

Expand Down
35 changes: 35 additions & 0 deletions spec/configuration_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
local configuration = require 'apicast.configuration'
local env = require 'resty.env'
local captured_logs = {}

local function capture_log(level, ...)
local message_parts = {...} -- Capture all message parts
local full_message = table.concat(message_parts, "") -- Concatenate the parts into a full string
table.insert(captured_logs, {level = level, message = full_message})
end

describe('Configuration object', function()

Expand Down Expand Up @@ -126,13 +133,41 @@ describe('Configuration object', function()
end)

describe("with service filter", function()
local original_ngx_log

before_each(function()
-- Save original log
original_ngx_log = ngx.log
end)

after_each(function()
-- After each test, restore the log
ngx.log = original_ngx_log
end)

local mockservices = {
Service.new({id="42", hosts={"test.foo.com", "test.bar.com"}}),
Service.new({id="12", hosts={"staging.foo.com"}}),
Service.new({id="21", hosts={"prod.foo.com"}}),
Service.new({id="56", hosts={"staging.foo.com"}})
}

it("log service list once for all filtered services", function()
env.set('APICAST_SERVICES_FILTER_BY_URL', '^test.*')
env.set('APICAST_SERVICES_LIST', '42,21')

ngx.log = capture_log
local services_returned = filter_services(mockservices, {"21"})

assert.same(services_returned, {mockservices[1], mockservices[3]})

-- Inspect the captured logs
assert.is_not_nil(captured_logs)
for _, log in ipairs(captured_logs) do
assert.match("filtering out services: 12, 56", log.message)
end
end)

it("with empty env variable", function()
env.set('APICAST_SERVICES_FILTER_BY_URL', '')
assert.same(filter_services(mockservices, nil), mockservices)
Expand Down
68 changes: 68 additions & 0 deletions t/configuration-loading-from-service-list.t
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,71 @@ location /transactions/authrep.xml {
["yay, api backend\n","yay, api backend\n","yay, api backend\n",""]
--- error_code eval
[200, 200, 200, 404]

=== TEST 5: multi service configuration limited to specific service log messages
--- env eval
("APICAST_SERVICES_LIST", "42")
--- configuration
{
"services": [
{
"backend_version": 1,
"proxy": {
"hosts": [
"one"
],
"api_backend": "http://test:$TEST_NGINX_SERVER_PORT/",
"proxy_rules": [
{
"http_method": "GET",
"delta": 1,
"metric_system_name": "one",
"pattern": "/"
}
]
},
"id": 42
},
{
"proxy": {
"hosts": [
"one"
]
},
"id": 11
},
{
"proxy": {
"hosts": [
"one"
]
},
"id": 33
},
{
"proxy": {
"hosts": [
"one"
]
},
"id": 999
}
]
}
--- backend
location /transactions/authrep.xml {
content_by_lua_block { ngx.exit(200) }
}
--- upstream
location ~ / {
echo 'yay, api backend';
}
--- request
GET /?user_key=1
--- more_headers
Host: one
--- response_body
yay, api backend
--- error_code: 200
--- error_log
filtering out services: 11, 33, 999
Loading