Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to register and use custom variable? #6702

Closed
loseself opened this issue Mar 24, 2022 · 10 comments
Closed

How to register and use custom variable? #6702

loseself opened this issue Mar 24, 2022 · 10 comments
Labels

Comments

@loseself
Copy link

Description

Referenced document: https://apisix.apache.org/zh/docs/apisix/plugin-develop/#%E6%B3%A8%E5%86%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F

image

How do I use it?, "register custom variable"
Can you provide demo?

how to register
how to get
how to destroy
how to modify
and announcements


After I registered, and reload plugin, there is an error

local core = require "apisix.core"

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

22826 [lua] events.lua:194: do_handlerlist(): worker-events: event callback failed; source=/apisix/admin/plugins/reload, event=PUT, pid=47 error='/usr/local/apisix/apisix/plugin.lua:113: attempt to index local 'plugin' (a number value)
stack traceback:
/usr/local/apisix/apisix/plugin.lua:113: in function 'load_plugin'
/usr/local/apisix/apisix/plugin.lua:191: in function 'load'
/usr/local/apisix/apisix/plugin.lua:300: in function 'load'
/usr/local/apisix/apisix/admin/init.lua:309: in function </usr/local/apisix/apisix/admin/init.lua:307>
[C]: in function 'xpcall'
...local/apisix//deps/share/lua/5.1/resty/worker/events.lua:185: in function 'do_handlerlist'
...local/apisix//deps/share/lua/5.1/resty/worker/events.lua:219: in function 'do_event_json'
...local/apisix//deps/share/lua/5.1/resty/worker/events.lua:361: in function 'poll'
...local/apisix//deps/share/lua/5.1/resty/worker/events.lua:380: in function <...local/apisix//deps/share/lua/5.1/resty/worker/events.lua:375>', data=1648092700, context: ngx.timer

Environment

  • APISIX version (run apisix version): 2.12.1
  • Operating system (run uname -a): Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64 x86_64
  • OpenResty / Nginx version (run openresty -V or nginx -V): nginx version: openresty/1.19.9.1
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info): version: 3.4.15, API version: 3.4
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version): 3.8.0
@liangliang4ward
Copy link
Contributor

like this:

core.ctx.register_var("mqtt_client_id", function(ctx)
return ctx.mqtt_client_id
end)

@juzhiyuan juzhiyuan changed the title the use of "register custom variable" How to register and use custom variable? Mar 24, 2022
@loseself
Copy link
Author

thanks

@loseself
Copy link
Author

How to destroy

@tzssangglass
Copy link
Member

see: #6656 (comment)

@shuaijinchao
Copy link
Member

How to destroy

you can use core.ctx.register_var("xxxx", function(ctx) return nil end) destroy.

@amrellafy
Copy link

Loading the lua file is enough to register the custom variable or it must be a part of a custom plugin?

@tokers
Copy link
Contributor

tokers commented May 24, 2022

Loading the lua file is enough to register the custom variable or it must be a part of a custom plugin?

It doesn't have to be a plugin, you can even modify the core codebase of APISIX and register some custom variables. But please make sure that you know what you're doing, or something might be broken.

@amrellafy
Copy link

@tokers so loading the lua file is enough?
Then It might be there is no need to modify the codebase?

@github-actions
Copy link

github-actions bot commented May 9, 2023

This issue has been marked as stale due to 350 days of inactivity. It will be closed in 2 weeks if no further activity occurs. If this issue is still relevant, please simply write any comment. Even if closed, you can still revive the issue at any time or discuss it on the dev@apisix.apache.org list. Thank you for your contributions.

@github-actions github-actions bot added the stale label May 9, 2023
@github-actions
Copy link

This issue has been closed due to lack of activity. If you think that is incorrect, or the issue requires additional review, you can revive the issue at any time.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants