Skip to content

Commit

Permalink
tests(opentelemetry): OTel formatted logs
Browse files Browse the repository at this point in the history
This commit adds unit and integration tests for the OTel-formatted logs
feature.
  • Loading branch information
samugi committed Jun 24, 2024
1 parent 696c898 commit a775404
Show file tree
Hide file tree
Showing 5 changed files with 388 additions and 26 deletions.
97 changes: 97 additions & 0 deletions spec/01-unit/26-observability/05-logs_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
-- This software is copyright Kong Inc. and its licensors.
-- Use of the software is subject to the agreement between your organization
-- and Kong Inc. If there is no such agreement, use is governed by and
-- subject to the terms of the Kong Master Software License Agreement found
-- at https://konghq.com/enterprisesoftwarelicense/.
-- [ END OF LICENSE 0867164ffc95e54f04670b5169c09574bdbd9bba ]

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(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(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(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 request-scoped log entries", function()
local log_level = ngx.WARN
local body = "Careful! I'm a warning!"

maybe_push(log_level, body)
local worker_logs = get_worker_logs()
assert.same({}, worker_logs)

local request_logs = get_request_logs()
assert.same(1, #request_logs)

local logged_entry = request_logs[1]
assert.same(log_level, logged_entry.log_level)
assert.same(body, 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)
45 changes: 44 additions & 1 deletion spec/03-plugins/37-opentelemetry/01-otlp_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ local pb_decode_span = function(data)
return pb.decode("opentelemetry.proto.trace.v1.Span", data)
end

local pb_encode_log = function(data)
return pb.encode("opentelemetry.proto.logs.v1.LogRecord", data)
end

local pb_decode_log = function(data)
return pb.decode("opentelemetry.proto.logs.v1.LogRecord", data)
end

describe("Plugin: opentelemetry (otlp)", function()
local old_ngx_get_phase

Expand All @@ -66,7 +74,7 @@ describe("Plugin: opentelemetry (otlp)", function()
ngx.ctx.KONG_SPANS = nil
end)

it("encode/decode pb", function ()
it("encode/decode pb (traces)", function ()
local N = 10000

local test_spans = {
Expand Down Expand Up @@ -125,6 +133,41 @@ describe("Plugin: opentelemetry (otlp)", function()
end
end)

it("encode/decode pb (logs)", function ()
local N = 10000

local test_logs = {}

for _ = 1, N do
local now_ns = time_ns()

local log = {
time_unix_nano = now_ns,
observed_time_unix_nano = now_ns,
log_level = ngx.INFO,
span_id = rand_bytes(8),
body = "log line",
attributes = {
foo = "bar",
test = true,
version = 0.1,
},
}
insert(test_logs, log)
end

local trace_id = rand_bytes(16)
local flags = tonumber(rand_bytes(1))
local prepared_logs = otlp.prepare_logs(test_logs, trace_id, flags)

for _, prepared_log in ipairs(prepared_logs) do
local decoded_log = pb_decode_log(pb_encode_log(prepared_log))

local ok, err = table_compare(prepared_log, decoded_log)
assert.is_true(ok, err)
end
end)

it("check lengths of trace_id and span_id ", function ()
local TRACE_ID_LEN, PARENT_SPAN_ID_LEN = 16, 8
local default_span = {
Expand Down
Loading

0 comments on commit a775404

Please sign in to comment.