-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests(opentelemetry): OTel formatted logs
This commit adds unit and integration tests for the OTel-formatted logs feature.
- Loading branch information
Showing
10 changed files
with
528 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require "kong.tools.utils" | ||
|
||
|
||
describe("Observability/Logs unit tests", function() | ||
describe("maybe_push()", function() | ||
local o11y_logs, maybe_push, get_request_logs, get_worker_logs | ||
local old_ngx, old_kong | ||
|
||
lazy_setup(function() | ||
old_ngx = _G.ngx | ||
old_kong = _G.kong | ||
|
||
_G.ngx = { | ||
config = { subsystem = "http" }, | ||
ctx = {}, | ||
DEBUG = ngx.DEBUG, | ||
INFO = ngx.INFO, | ||
WARN = ngx.WARN, | ||
} | ||
|
||
_G.kong = { | ||
configuration = { | ||
log_level = "info", | ||
}, | ||
} | ||
|
||
o11y_logs = require "kong.observability.logs" | ||
maybe_push = o11y_logs.maybe_push | ||
get_request_logs = o11y_logs.get_request_logs | ||
get_worker_logs = o11y_logs.get_worker_logs | ||
end) | ||
|
||
before_each(function() | ||
_G.ngx.ctx = {} | ||
end) | ||
|
||
lazy_teardown(function() | ||
_G.ngx = old_ngx | ||
_G.kong = old_kong | ||
end) | ||
|
||
it("has no effect when no log line is provided", function() | ||
maybe_push(1, ngx.INFO) | ||
local worker_logs = get_worker_logs() | ||
assert.same({}, worker_logs) | ||
local request_logs = get_request_logs() | ||
assert.same({}, request_logs) | ||
end) | ||
|
||
it("has no effect when log line is empty", function() | ||
maybe_push(1, ngx.INFO, "") | ||
local worker_logs = get_worker_logs() | ||
assert.same({}, worker_logs) | ||
local request_logs = get_request_logs() | ||
assert.same({}, request_logs) | ||
end) | ||
|
||
it("has no effect when log level is lower than the configured value", function() | ||
maybe_push(1, ngx.DEBUG, "Don't mind me, I'm just a debug log") | ||
local worker_logs = get_worker_logs() | ||
assert.same({}, worker_logs) | ||
local request_logs = get_request_logs() | ||
assert.same({}, request_logs) | ||
end) | ||
|
||
it("generates worker-scoped log entries", function() | ||
local log_level = ngx.WARN | ||
local body = "Careful! I'm a warning!" | ||
|
||
maybe_push(1, log_level, body, true, 123, ngx.null, nil, function()end, { foo = "bar" }) | ||
local worker_logs = get_worker_logs() | ||
assert.equals(1, #worker_logs) | ||
|
||
local logged_entry = worker_logs[1] | ||
assert.same(log_level, logged_entry.log_level) | ||
assert.matches(body .. "true123nilnilfunction:%s0x%x+table:%s0x%x+", logged_entry.body) | ||
assert.is_table(logged_entry.attributes) | ||
assert.is_number(logged_entry.attributes["introspection.current.line"]) | ||
assert.is_string(logged_entry.attributes["introspection.name"]) | ||
assert.is_string(logged_entry.attributes["introspection.namewhat"]) | ||
assert.is_string(logged_entry.attributes["introspection.source"]) | ||
assert.is_string(logged_entry.attributes["introspection.what"]) | ||
assert.is_number(logged_entry.observed_time_unix_nano) | ||
assert.is_number(logged_entry.time_unix_nano) | ||
end) | ||
end) | ||
end) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
local helpers = require "spec.helpers" | ||
|
||
for _, strategy in helpers.each_strategy() do | ||
describe("Observability Logs", function () | ||
describe("ngx.log patch", function() | ||
local proxy_client | ||
local post_function_access = [[ | ||
local threads = {} | ||
local n_threads = 100 | ||
for i = 1, n_threads do | ||
threads[i] = ngx.thread.spawn(function() | ||
ngx.log(ngx.INFO, "thread_" .. i .. " logged") | ||
end) | ||
end | ||
for i = 1, n_threads do | ||
ngx.thread.wait(threads[i]) | ||
end | ||
]] | ||
|
||
lazy_setup(function() | ||
local bp = helpers.get_db_utils(strategy, { | ||
"routes", | ||
"services", | ||
"plugins", | ||
}) | ||
|
||
local http_srv = assert(bp.services:insert { | ||
name = "mock-service", | ||
host = helpers.mock_upstream_host, | ||
port = helpers.mock_upstream_port, | ||
}) | ||
|
||
local logs_route = assert(bp.routes:insert({ service = http_srv, | ||
protocols = { "http" }, | ||
paths = { "/logs" }})) | ||
|
||
assert(bp.plugins:insert({ | ||
name = "post-function", | ||
route = logs_route, | ||
config = { | ||
access = { post_function_access }, | ||
}, | ||
})) | ||
|
||
-- only needed to enable the log collection hook | ||
assert(bp.plugins:insert({ | ||
name = "opentelemetry", | ||
route = logs_route, | ||
config = { | ||
logs_endpoint = "http://" .. helpers.mock_upstream_host .. ":" .. helpers.mock_upstream_port, | ||
} | ||
})) | ||
|
||
helpers.start_kong({ | ||
database = strategy, | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
plugins = "opentelemetry,post-function", | ||
}) | ||
proxy_client = helpers.proxy_client() | ||
end) | ||
|
||
lazy_teardown(function() | ||
if proxy_client then | ||
proxy_client:close() | ||
end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("does not produce yielding and concurrent executions", function () | ||
local res = assert(proxy_client:send { | ||
method = "GET", | ||
path = "/logs", | ||
}) | ||
assert.res_status(200, res) | ||
|
||
-- plugin produced logs: | ||
assert.logfile().has.line("thread_1 logged", true, 10) | ||
assert.logfile().has.line("thread_100 logged", true, 10) | ||
-- plugin did not produce concurrent accesses to ngx.log: | ||
assert.logfile().has.no.line("[error]", true) | ||
end) | ||
end) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.