leslie-tsang commented on code in PR #10086: URL: https://github.com/apache/apisix/pull/10086#discussion_r1305361606
########## example/register_custom_var.lua: ########## @@ -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") Review Comment: ```suggestion ngx.log(ngx.EMERG, "my hook works in stream") ``` please use `core.log` instead. ########## docs/en/latest/plugin-develop.md: ########## @@ -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 Review Comment: ```suggestion return route.labels ``` Please standardize indentation style. `4` space will be please ########## example/register_custom_var.lua: ########## @@ -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") + old_stream_init(...) +end Review Comment: 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. -- 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]
