Skip to content
Closed
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
27 changes: 17 additions & 10 deletions docs/en/latest/plugin-develop.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Plugin Develop
title: Develop Plugin
---

<!--
Expand Down Expand Up @@ -71,8 +71,7 @@ apisix:
lua_module_hook: "my_hook"
```

The `example/my_hook.lua` will be loaded when APISIX starts, and you can use this hook to replace a method in APISIX.
The example of [my_hook.lua](https://github.com/apache/apisix/blob/master/example/my_hook.lua) can be found under the `example` directory of this project.
The `example/my_hook.lua` will be loaded when APISIX starts, and you can use this hook to replace a method in APISIX. The example of [my_hook.lua](https://github.com/apache/apisix/blob/master/example/my_hook.lua) can be found under the `example` directory of this project.

## check dependencies

Expand Down Expand Up @@ -488,25 +487,33 @@ curl -i -X GET "http://127.0.0.1:9090/v1/plugin/example-plugin/hello"

## register custom variable

We can use variables in many places of APISIX. For example, customizing log format in http-logger, using it as the key of `limit-*` plugins. In some situations, the builtin variables are not enough. Therefore, APISIX allows developers to register their variables globally, and use them as normal builtin variables.
You can also register your own variables and use them as built-in variables, for use cases such as customizing log format in logging plugins, or using built-in variables as keys in rate limiting plugins.

For instance, let's register a variable called `a6_labels_zone` to fetch the value of the `zone` label in a route:
For example, you can register a variable called `a6_route_labels` to obtain `labels` from a route:

```
```lua
local core = require "apisix.core"

core.ctx.register_var("a6_labels_zone", function(ctx)
core.ctx.register_var("a6_route_labels", function(ctx)
local route = ctx.matched_route and ctx.matched_route.value
if route and route.labels then
return route.labels.zone
return route.labels
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return route.labels
return route.labels

Please standardize indentation style. 4 space will be please

end
return nil
end)
```

After that, any get operation to `$a6_labels_zone` will call the registered getter to fetch the value.
If you are developing a new plugin, the snippet could be added directly to your plugin source file.

::: info

If you are not doing development and would like to register a custom variable at runtime, you can run the snippet in serverless functions with the [`serverless`](plugins/serverless.md) plugins.

:::

Upon a successful registration, you can use the variable `$a6_route_labels` in configurations.

Note that the custom variables can't be used in features that depend on the Nginx directive, like `access_log_format`.
Note that the custom variables cannot be used in static configuration files, for example, to configure `access_log_format`.

## write test case

Expand Down
42 changes: 42 additions & 0 deletions example/register_custom_var.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local apisix = require "apisix"
local core = require "apisix.core"

local register_custom_var = function()
core.ctx.register_var("a6_route_labels", function(ctx)
local route = ctx.matched_route and ctx.matched_route.value
if route and route.labels then
return route.labels
end
return nil
end)
end

local old_http_init = apisix.http_init
apisix.http_init = function(...)
register_custom_var()
ngx.log(ngx.EMERG, "my hook works in http")
old_http_init(...)
end

local old_stream_init = apisix.stream_init
apisix.stream_init = function(...)
register_custom_var()
ngx.log(ngx.EMERG, "my hook works in stream")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ngx.log(ngx.EMERG, "my hook works in stream")
ngx.log(ngx.EMERG, "my hook works in stream")

please use core.log instead.

old_stream_init(...)
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To show that the variable was really registered, should we try to access and log this in another phase and then grep and assert in the tests?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean something like this?

local old_http_access_phase = apisix.http_access_phase
apisix.http_access_phase = function(...)
    ngx.log(ngx.EMERG, ctx.a6_route_labels)
    old_http_access_phase(...)
end

My understanding is that the custom variable a6_route_labels would only has value if a route is configured with the prop labels.

The register_var("a6_route_labels") example is kept consistent with this test case:

=== TEST 15: use custom variable in the logger
--- extra_init_by_lua
local core = require "apisix.core"
core.ctx.register_var("a6_route_labels", function(ctx)
local route = ctx.matched_route and ctx.matched_route.value
if route and route.labels then
return route.labels
end
return nil
end)

Please advise.

Copy link
Copy Markdown
Contributor

@Revolyssup Revolyssup Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes part of the suggestion was to PUT that route (just like in the test you mentioned) and then send request to that route, after which to make assertion. But I don't have a strong opinion here. If others think that showing how it's registered is good enough then I think its fine. cc: @monkeyDluffy6017 @leslie-tsang What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've already add test cases for this feature, and it doesn't seem necessary to do it again in the demo.
BTW, "my hook works in http" doesn't mean it actually works, it's just a log.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I understand this doesn't cover the entire use case. The initial motivation of this was users having difficulties understanding how to register a variable with a custom Lua file that gets sourced (i.e. lua_module_hook). Once it's sourced and APISIX starts successfully (aka here it doesn't error out), the rest is easy. I agree with you that the feature of sourcing external files is covered in test, however.

33 changes: 32 additions & 1 deletion t/cli/test_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ fi

echo "passed: detect invalid extra_lua_path"

# support hooking into APISIX methods
# validate lua_module_hook
echo '
apisix:
lua_module_hook: "example/my_hook"
Expand All @@ -668,6 +668,7 @@ fi

echo "passed: bad lua_module_hook should be rejected"

# support hooking into APISIX methods
echo '
apisix:
proxy_mode: http&stream
Expand Down Expand Up @@ -697,6 +698,36 @@ fi

echo "passed: hook can take effect"

# register custom variable with the hook
echo '
apisix:
proxy_mode: http&stream
extra_lua_path: "\$prefix/example/?.lua"
lua_module_hook: "register_custom_var"
stream_proxy:
tcp:
- addr: 9100
' > conf/config.yaml

rm logs/error.log
make init
make run

sleep 0.5
make stop

if ! grep "my hook works in http" logs/error.log > /dev/null; then
echo "failed: hook can take effect"
exit 1
fi

if ! grep "my hook works in stream" logs/error.log > /dev/null; then
echo "failed: hook can take effect"
exit 1
fi

echo "passed: register custom variable with hook"

# check the keepalive related parameter settings in the upstream
git checkout conf/config.yaml

Expand Down