kayx23 commented on code in PR #10086:
URL: https://github.com/apache/apisix/pull/10086#discussion_r1305318164


##########
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:
   Do you mean something like this?
   
   ```lua
   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: 
https://github.com/apache/apisix/blob/89c1858662e34c76de01c84b665c79fcb2edaeb8/t/plugin/http-logger-log-format.t#L431-L441
   
   Please advise.



-- 
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]

Reply via email to