Copilot commented on code in PR #12448:
URL: https://github.com/apache/apisix/pull/12448#discussion_r2218521615


##########
apisix/upstream.lua:
##########
@@ -310,6 +339,14 @@ function _M.set_by_route(route, api_ctx)
             return HTTP_CODE_UPSTREAM_UNAVAILABLE, "no valid upstream node: " 
.. (err or "nil")
         end
 
+        if up_conf.metadata_match then
+            new_nodes = match_nodes_by_metadata(new_nodes, 
up_conf.metadata_match)
+            if #new_nodes == 0 then
+                return HTTP_CODE_UPSTREAM_UNAVAILABLE, "no matched upstream 
node after metadata matching"
+            end
+        end
+        core.log.info("new_nodes: ", core.json.delay_encode(new_nodes, true))

Review Comment:
   This info-level log statement will be printed on every request and could 
generate excessive logs in production. Consider using debug level or adding a 
condition to only log when metadata filtering actually occurs.
   ```suggestion
               core.log.info("new_nodes: ", core.json.delay_encode(new_nodes, 
true))
           end
   ```



##########
apisix/upstream.lua:
##########
@@ -310,6 +339,14 @@ function _M.set_by_route(route, api_ctx)
             return HTTP_CODE_UPSTREAM_UNAVAILABLE, "no valid upstream node: " 
.. (err or "nil")
         end
 
+        if up_conf.metadata_match then

Review Comment:
   The metadata_match field is used directly without validation of its 
structure. The code expects it to be an array of objects with 'key' and 
'allowed_vals' fields, but the test shows it as a simple key-value object. This 
mismatch will cause the filtering to fail.
   ```suggestion
           if up_conf.metadata_match then
               -- Validate and transform metadata_match into the expected format
               if type(up_conf.metadata_match) == "table" and not 
(#up_conf.metadata_match > 0) then
                   local transformed_metadata = {}
                   for key, allowed_vals in pairs(up_conf.metadata_match) do
                       if type(allowed_vals) ~= "table" then
                           return HTTP_CODE_UPSTREAM_UNAVAILABLE, "invalid 
metadata_match format: allowed_vals must be a table"
                       end
                       table.insert(transformed_metadata, { key = key, 
allowed_vals = allowed_vals })
                   end
                   up_conf.metadata_match = transformed_metadata
               elseif type(up_conf.metadata_match) ~= "table" or not 
(#up_conf.metadata_match > 0) then
                   return HTTP_CODE_UPSTREAM_UNAVAILABLE, "invalid 
metadata_match format: must be an array of objects with 'key' and 
'allowed_vals'"
               end
   ```



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