-
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.
This commit: * introduces the `kong.telemetry` pdk module * adds the `kong.telemetry.log` function to allow generating log entries meant to be reported via the OpenTelemetry plugin
- Loading branch information
Showing
11 changed files
with
433 additions
and
10 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,5 @@ | ||
message: | | ||
Added a new PDK module `kong.telemetry` and function: `kong.telemetry.log` | ||
to generate log entries to be reported via the OpenTelemetry plugin. | ||
type: feature | ||
scope: PDK |
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
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
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
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
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,91 @@ | ||
--- | ||
-- The telemetry module provides capabilities for telemetry operations. | ||
-- | ||
-- @module kong.telemetry.log | ||
|
||
|
||
local dynamic_hook = require("kong.dynamic_hook") | ||
|
||
local dyn_hook_run_hook = dynamic_hook.run_hook | ||
local dyn_hook_is_group_enabled = dynamic_hook.is_group_enabled | ||
|
||
local function new() | ||
local telemetry = {} | ||
|
||
|
||
--- | ||
-- Records a structured log entry, to be reported via the OpenTelemetry plugin. | ||
-- | ||
-- This function has a dependency on the OpenTelemetry plugin, which must be | ||
-- configured to report OpenTelemetry logs. | ||
-- | ||
-- @function kong.telemetry.log | ||
-- @phases `rewrite`, `access`, `balancer`, `timer`, `header_filter`, | ||
-- `response`, `body_filter`, `log` | ||
-- @tparam string plugin_name the name of the plugin | ||
-- @tparam table plugin_config the plugin configuration | ||
-- @tparam string message_type the type of the log message, useful to categorize | ||
-- the log entry | ||
-- @tparam string message the log message | ||
-- @tparam table attributes structured information to be included in the | ||
-- `attributes` field of the log entry | ||
-- @usage | ||
-- local attributes = { | ||
-- http_method = kong.request.get_method() | ||
-- ["node.id"] = kong.node.get_id(), | ||
-- hostname = kong.node.get_hostname(), | ||
-- } | ||
-- | ||
-- local ok, err = kong.telemetry.log("my_plugin", conf, "result", "successful operation", attributes) | ||
telemetry.log = function(plugin_name, plugin_config, message_type, message, attributes) | ||
if type(plugin_name) ~= "string" then | ||
return nil, "plugin_name must be a string" | ||
end | ||
|
||
if type(plugin_config) ~= "table" then | ||
return nil, "plugin_config must be a table" | ||
end | ||
|
||
if type(message_type) ~= "string" then | ||
return nil, "message_type must be a string" | ||
end | ||
|
||
if message and type(message) ~= "string" then | ||
return nil, "message must be a string" | ||
end | ||
|
||
if attributes and type(attributes) ~= "table" then | ||
return nil, "attributes must be a table" | ||
end | ||
|
||
local hook_group = "observability_logs" | ||
if not dyn_hook_is_group_enabled(hook_group) then | ||
return nil, "Telemetry logging is disabled: log entry will not be recorded. " .. | ||
"Ensure the OpenTelemetry plugin is correctly configured to " .. | ||
"report logs in order to use this feature." | ||
end | ||
|
||
attributes = attributes or {} | ||
attributes["message.type"] = message_type | ||
attributes["plugin.name"] = plugin_name | ||
attributes["plugin.id"] = plugin_config.__plugin_id | ||
attributes["plugin.instance.name"] = plugin_config.plugin_instance_name | ||
|
||
-- stack level = 5: | ||
-- 1: maybe_push | ||
-- 2: dynamic_hook.pcall | ||
-- 3: dynamic_hook.run_hook | ||
-- 4: kong.telemetry.log | ||
-- 5: caller | ||
dyn_hook_run_hook(hook_group, "push", 5, attributes, nil, message) | ||
return true | ||
end | ||
|
||
|
||
return telemetry | ||
end | ||
|
||
|
||
return { | ||
new = new, | ||
} |
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
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,38 @@ | ||
require "kong.tools.utils" | ||
|
||
|
||
describe("Telemetry PDK unit tests", function() | ||
describe("log()", function() | ||
local old_kong = _G.kong | ||
|
||
lazy_setup(function() | ||
local kong_global = require "kong.global" | ||
_G.kong = kong_global.new() | ||
kong_global.init_pdk(kong) | ||
end) | ||
|
||
lazy_teardown(function() | ||
_G.kong = old_kong | ||
end) | ||
|
||
it("fails as expected with invalid input", function() | ||
local ok, err = kong.telemetry.log() | ||
assert.is_nil(ok) | ||
assert.equals("plugin_name must be a string", err) | ||
|
||
ok, err = kong.telemetry.log("plugin_name") | ||
assert.is_nil(ok) | ||
assert.equals("plugin_config must be a table", err) | ||
|
||
ok, err = kong.telemetry.log("plugin_name", {}) | ||
assert.is_nil(ok) | ||
assert.equals("message_type must be a string", err) | ||
end) | ||
|
||
it ("considers attributes and message as optional", function() | ||
local ok, err = kong.telemetry.log("plugin_name", {}, "message_type") | ||
assert.is_nil(ok) | ||
assert.matches("Telemetry logging is disabled", err) | ||
end) | ||
end) | ||
end) |
Oops, something went wrong.