Skip to content

Commit

Permalink
amend! feat(dynamic_hook): support non request-scoped hooks
Browse files Browse the repository at this point in the history
feat(dynamic_hook): support non request-scoped hooks + disable

When a disabled and non-request scoped hook was called outside of
a request scope, it was producing an error while trying to index ngx.ctx

This commit adds a check to early return from is_group_enabled when
outside of the request scope

This allows for hooks to still work when used in other contexts where
they can be enabled via the always_enable() function

This commit also adds support for disabling hooks.
  • Loading branch information
samugi committed Jun 25, 2024
1 parent 41e7b28 commit 274d185
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion kong/dynamic_hook/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dynamic_hook.hook_function("my_group", _G, "print", "varargs", {
})

-- Enable the hook group
dynamic_hook.always_enable("my_group")
dynamic_hook.enable_by_default("my_group")

-- Call the function
print("world!") -- prints "hello, world!"
Expand Down
15 changes: 13 additions & 2 deletions kong/dynamic_hook/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,24 @@ end

--- Enables a hook group for all requests
--
-- @function dynamic_hook:always_enable
-- @function dynamic_hook:enable_by_default
-- @tparam string group_name The name of the hook group to enable
function _M.always_enable(group_name)
function _M.enable_by_default(group_name)
assert(type(group_name) == "string", "group_name must be a string")

ALWAYS_ENABLED_GROUPS[group_name] = true
end


--- Disables a hook group that was enabled with `enable_by_default`
--
-- @function dynamic_hook:disable_by_default
-- @tparam string group_name The name of the hook group to disable
function _M.disable_by_default(group_name)
assert(type(group_name) == "string", "group_name must be a string")

ALWAYS_ENABLED_GROUPS[group_name] = nil
end


return _M
2 changes: 1 addition & 1 deletion kong/timing/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function _M.init_worker(is_enabled)
enabled = is_enabled and ngx.config.subsystem == "http"

if enabled then
req_dyn_hook.always_enable("timing:auth")
req_dyn_hook.enable_by_default("timing:auth")
end
end

Expand Down

0 comments on commit 274d185

Please sign in to comment.