kys1230 commented on issue #7572:
URL: https://github.com/apache/apisix/issues/7572#issuecomment-1200426529
plugin-a.lua
```lua
local core = require("apisix.core")
local schema = {
type = "object",
}
local plugin_name = "plugin-a"
local _M = {
version = 0.1,
priority = 90,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
return core.schema.check(schema, conf)
end
local keyList = {"H1", "H2"}
local function get_value(headers)
-- The logic is simplified here, and the actual generated variables are
more complex
-- 这里简化了逻辑,实际生成变量更复杂
for k, v in pairs(keyList) do
if headers[v] ~= nil then
return headers[v]
end
end
return nil
end
core.ctx.register_var("var_name", function(ctx)
local var_name = get_value(ngx.req.get_headers())
return var_name or "-"
end)
return _M
```
plugin-b.lua
```lua
local core = require("apisix.core")
local schema = {
type = "object",
}
local plugin_name = "plugin-b"
local _M = {
version = 0.1,
priority = 80,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
return core.schema.check(schema, conf)
end
function _M.rewrite(conf, ctx)
-- Here is just calling custom variables, which will actually have
richer functions
-- 这里只是调用自定义变量,实际会有更丰富的功能
core.log.warn("plugin-a value: ", ctx.var_name)
end
return _M
```
apisix config.yaml
```yaml
plugins:
# The file-logger test is used here. In fact, I use kafka-logger
# 这里采用 file-logger 测试,实际上我使用的是 kafka-logger
- file-logger
- plugin-a
- plugin-b
```
set global rule
```bash
admin_url="http://127.0.0.1:9080"
admin_key="edd1c9f034335f136f87ad84b625c8f1"
curl -X PUT -H "X-API-KEY: $admin_key"
$admin_url/apisix/admin/global_rules/1 -d '{
"plugins": {
"file-logger": {
"disable": false,
"path": "/usr/local/apisix/logs/file.log"
},
"plugin-a": {
disable": false
},
"plugin-b": {
"disable": false
}
}
}'
```
set file-logger logformat
```bash
admin_url="http://127.0.0.1:9080"
admin_key="edd1c9f034335f136f87ad84b625c8f1"
curl -H "X-API-KEY: $admin_key"
$admin_url/apisix/admin/plugin_metadata/file-logger -X PUT -d '
{
"log_format": {
"host": "$host",
"@timestamp": "$time_iso8601",
"client_ip": "$remote_addr",
"app_key": "$app_key",
"status": "$status",
"request_time": "$request_time",
"upstream_addr": "$upstream_addr",
"upstream_response_time": "$upstream_response_time",
"var_name": "$var_name"
}
}'
```
view custom variables in the access log(file-logger)
```bash
$ curl -H 'H1: A' 127.0.0.1:9080
$ curl -H 'H2: B' 127.0.0.1:9080
$ curl -H 'H3: C' 127.0.0.1:9080
tail -3 /usr/local/apisix/logs/file.log | jq .var_name
"A"
"B"
"-"
```
view plugin-b log in error.log
```bash
$ grep /usr/local/apisix/logs/error.log | grep 'plugin-b.lua'
2022/07/31 13:24:47 [warn] 49#49: *27516 [lua] plugin-b.lua:21:
phase_func(): plugin-a value: nil, client: 192.168.192.1, server: _, request:
"GET / HTTP/1.1", host: "127.0.0.1:9080"
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]