This is an automated email from the ASF dual-hosted git repository.

baoyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 283963c5a feat: add rate-limiting-info variable (#13155)
283963c5a is described below

commit 283963c5a9c91211b6be20f5859837c55a630b5d
Author: Shreemaan Abhishek <[email protected]>
AuthorDate: Thu Apr 9 11:44:28 2026 +0545

    feat: add rate-limiting-info variable (#13155)
---
 apisix/cli/ngx_tpl.lua              |  2 ++
 apisix/core/ctx.lua                 |  2 ++
 apisix/core/utils.lua               | 16 +++++++++++
 apisix/plugins/limit-count/init.lua |  2 ++
 t/APISIX.pm                         |  3 ++-
 t/plugin/limit-count5.t             | 53 +++++++++++++++++++++++++++++++++++++
 6 files changed, 77 insertions(+), 1 deletion(-)

diff --git a/apisix/cli/ngx_tpl.lua b/apisix/cli/ngx_tpl.lua
index 049282ad0..6bd33368b 100644
--- a/apisix/cli/ngx_tpl.lua
+++ b/apisix/cli/ngx_tpl.lua
@@ -752,6 +752,8 @@ http {
         {% end %}
         # zipkin_set_ngx_var ends
 
+        set $rate_limiting_info '';
+
         # http server configuration snippet starts
         {% if http_server_configuration_snippet then %}
         {* http_server_configuration_snippet *}
diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua
index a5d1d4751..08833d4b5 100644
--- a/apisix/core/ctx.lua
+++ b/apisix/core/ctx.lua
@@ -253,6 +253,8 @@ do
         upstream_cache_key         = true,
         upstream_cache_bypass      = true,
 
+        rate_limiting_info         = true,
+
         var_x_forwarded_proto      = true,
         var_x_forwarded_port       = true,
         var_x_forwarded_host       = true,
diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua
index a22de4111..0765465d0 100644
--- a/apisix/core/utils.lua
+++ b/apisix/core/utils.lua
@@ -478,6 +478,22 @@ function _M.check_tls_bool(fields, conf, plugin_name)
 end
 
 
+function _M.set_var_rate_limiting_info(ctx, key, limit, remaining, reset)
+    if not ctx then
+        return
+    end
+    key = key or ""
+    limit = limit or 0
+    remaining = tonumber(remaining) or 0
+    reset = reset or 0
+
+    ctx.var.rate_limiting_info = str_format(
+        '{"rate_limiting_key":"%s","rate_limiting_limit":%d,'
+        .. '"rate_limiting_remaining":%d,"rate_limiting_reset":%d}',
+            key, limit, remaining, reset)
+end
+
+
 -- worker-local monotonic state for UUID v7 (no shared dict needed)
 -- Each nginx worker has its own copy; no locking required.
 local _v7_last_ms = 0
diff --git a/apisix/plugins/limit-count/init.lua 
b/apisix/plugins/limit-count/init.lua
index ec8a02f8b..964876e90 100644
--- a/apisix/plugins/limit-count/init.lua
+++ b/apisix/plugins/limit-count/init.lua
@@ -405,6 +405,8 @@ local function run_rate_limit(conf, rule, ctx, name, cost, 
dry_run)
         delay, remaining, reset = lim:incoming(key, cost)
     end
 
+    core.utils.set_var_rate_limiting_info(ctx, key, lim.limit, remaining, 
reset)
+
     local metadata = apisix_plugin.plugin_metadata("limit-count")
     if metadata then
         metadata = metadata.value
diff --git a/t/APISIX.pm b/t/APISIX.pm
index 78e676c9d..3ea057123 100644
--- a/t/APISIX.pm
+++ b/t/APISIX.pm
@@ -238,6 +238,7 @@ if ($version =~ m/\/apisix-nginx-module/) {
     $a6_ngx_vars = <<_EOC_;
     set \$wasm_process_req_body       '';
     set \$wasm_process_resp_body      '';
+    set \$rate_limiting_info          '';
 _EOC_
 }
 
@@ -675,7 +676,7 @@ _EOC_
         require("apisix").http_exit_worker()
     }
 
-    log_format main escape=default '\$remote_addr - \$remote_user 
[\$time_local] \$http_host "\$request" \$status \$body_bytes_sent 
\$request_time "\$http_referer" "\$http_user_agent" \$upstream_addr 
\$upstream_status \$apisix_upstream_response_time 
"\$upstream_scheme://\$upstream_host\$upstream_uri" \$request_llm_model 
\$llm_model \$llm_time_to_first_token \$llm_prompt_tokens 
\$llm_completion_tokens';
+    log_format main escape=default '\$remote_addr - \$remote_user 
[\$time_local] \$http_host "\$request" \$status \$body_bytes_sent 
\$request_time "\$http_referer" "\$http_user_agent" \$upstream_addr 
\$upstream_status \$apisix_upstream_response_time 
"\$upstream_scheme://\$upstream_host\$upstream_uri" \$request_llm_model 
\$llm_model \$llm_time_to_first_token \$llm_prompt_tokens 
\$llm_completion_tokens "\$rate_limiting_info"';
 
     # fake server, only for test
     server {
diff --git a/t/plugin/limit-count5.t b/t/plugin/limit-count5.t
index 4227b4f10..eeb2b10e5 100644
--- a/t/plugin/limit-count5.t
+++ b/t/plugin/limit-count5.t
@@ -200,3 +200,56 @@ GET /hello
 APISIX-RATELIMIT-QUOTA: 10
 APISIX-RATELIMIT-REMAINING: 9
 APISIX-RATELIMIT-RESET: \d+
+
+
+
+=== TEST 6: set route(id: 1) using key_type var
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                        "methods": ["GET"],
+                        "plugins": {
+                            "limit-count": {
+                                "count": 2,
+                                "time_window": 10,
+                                "key_type": "var",
+                                "key": "http_host"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1980": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/hello"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 7: check access log contains rate_limiting_info
+--- request
+GET /hello
+--- more_headers
+host: test.com
+--- extra_yaml_config
+nginx_config:
+    http:
+        access_log_format: main '$rate_limiting_info';
+--- error_code: 200
+--- access_log eval
+qr/\{\\x22rate_limiting_key\\x22:\\x22\/apisix\/routes\/1:\d+:test\.com\\x22,\\x22rate_limiting_limit\\x22:2,\\x22rate_limiting_remaining\\x22:1,\\x22rate_limiting_reset\\x22:10}/

Reply via email to