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


##########
docs/en/latest/plugins/request-id.md:
##########
@@ -40,7 +40,7 @@ The `request-id` Plugin adds a unique ID to each request 
proxied through APISIX,
 | ------------------- | ------- | -------- | -------------- | 
------------------------------- | 
---------------------------------------------------------------------- |
 | header_name         | string  | False    | "X-Request-Id" |                  
               | Name of the header that carries the request unique ID. Note 
that if a request carries an ID in the `header_name` header, the Plugin will 
use the header value as the unique ID and will not overwrite it with the 
generated ID.                                 |
 | include_in_response | boolean | False    | true           |                  
               | If true, include the generated request ID in the response 
header, where the name of the header is the `header_name` value. |
-| algorithm           | string  | False    | "uuid"         | 
["uuid","nanoid","range_id"] | Algorithm used for generating the unique ID. 
When set to `uuid` , the Plugin generates a universally unique identifier. When 
set to `nanoid`, the Plugin generates a compact, URL-safe ID. When set to 
`range_id`, the Plugin generates a sequential ID with specific parameters.      
           |
+| algorithm           | string  | False    | "uuid"         | 
["uuid","nanoid","range_id","ksuid"] | Algorithm used for generating the unique 
ID. When set to `uuid` , the Plugin generates a universally unique identifier. 
When set to `nanoid`, the Plugin generates a compact, URL-safe ID. When set to 
`range_id`, the Plugin generates a sequential ID with specific parameters. When 
set to `ksuid`, the Plugin generates a sequential ID with timestamp and 
randomly number.                  |

Review Comment:
   The phrase 'randomly number' should be 'random number' to correct the 
grammar.
   ```suggestion
   | algorithm           | string  | False    | "uuid"         | 
["uuid","nanoid","range_id","ksuid"] | Algorithm used for generating the unique 
ID. When set to `uuid` , the Plugin generates a universally unique identifier. 
When set to `nanoid`, the Plugin generates a compact, URL-safe ID. When set to 
`range_id`, the Plugin generates a sequential ID with specific parameters. When 
set to `ksuid`, the Plugin generates a sequential ID with timestamp and random 
number.                  |
   ```



##########
docs/en/latest/plugins/request-id.md:
##########
@@ -243,6 +243,59 @@ You should receive an `HTTP/1.1 200 OK` response and see 
the response includes t
 X-Request-Id: kepgHWCH2ycQ6JknQKrX2
 ```
 
+### Use `ksuid` Algorithm
+
+The following example demonstrates how to configure `request-id` on a Route 
and use the `ksuid` algorithm to generate the request ID.
+
+Create a Route with the `request-id` Plugin as such:
+
+```shell
+curl "http://127.0.0.1:9180/apisix/admin/routes"; -X PUT \
+  -H "X-API-KEY: ${ADMIN_API_KEY}" \
+  -d '{
+    "id": "request-id-route",
+    "uri": "/anything",
+    "plugins": {
+      "request-id": {
+        "algorithm": "ksuid"
+      }
+    },
+    "upstream": {
+      "type": "roundrobin",
+      "nodes": {
+        "httpbin.org:80": 1
+      }
+    }
+  }'
+```
+
+Send a request to the Route:
+
+```shell
+curl -i "http://127.0.0.1:9080/anything";
+```
+
+You should receive an `HTTP/1.1 200 OK` response and see the response includes 
the `X-Req-Identifier` header with an ID generated using the `ksuid` algorithm:

Review Comment:
   The header name is incorrect. It should be `X-Request-Id` not 
`X-Req-Identifier` to match the actual plugin behavior and other examples in 
the documentation.
   ```suggestion
   You should receive an `HTTP/1.1 200 OK` response and see the response 
includes the `X-Request-Id` header with an ID generated using the `ksuid` 
algorithm:
   ```



##########
docs/en/latest/plugins/request-id.md:
##########
@@ -243,6 +243,59 @@ You should receive an `HTTP/1.1 200 OK` response and see 
the response includes t
 X-Request-Id: kepgHWCH2ycQ6JknQKrX2
 ```
 
+### Use `ksuid` Algorithm
+
+The following example demonstrates how to configure `request-id` on a Route 
and use the `ksuid` algorithm to generate the request ID.
+
+Create a Route with the `request-id` Plugin as such:
+
+```shell
+curl "http://127.0.0.1:9180/apisix/admin/routes"; -X PUT \
+  -H "X-API-KEY: ${ADMIN_API_KEY}" \
+  -d '{
+    "id": "request-id-route",
+    "uri": "/anything",
+    "plugins": {
+      "request-id": {
+        "algorithm": "ksuid"
+      }
+    },
+    "upstream": {
+      "type": "roundrobin",
+      "nodes": {
+        "httpbin.org:80": 1
+      }
+    }
+  }'
+```
+
+Send a request to the Route:
+
+```shell
+curl -i "http://127.0.0.1:9080/anything";
+```
+
+You should receive an `HTTP/1.1 200 OK` response and see the response includes 
the `X-Req-Identifier` header with an ID generated using the `ksuid` algorithm:
+
+```text
+X-Request-Id: 325ghCANEKjw6Jsfejg5p6QrLYB
+```
+
+If installed 
[ksuid](https://github.com/segmentio/ksuid?tab=readme-ov-file#command-line-tool)
 command tool,This ID can be viewed through `ksuid -f inspect 
325ghCANEKjw6Jsfejg5p6QrLYB`:

Review Comment:
   Missing space after comma. Should be 'If installed [ksuid](...) command 
tool, This ID can be viewed through...' or better yet, rephrase to 'If the 
[ksuid](...) command tool is installed, this ID can be viewed through...'.
   ```suggestion
   If the 
[ksuid](https://github.com/segmentio/ksuid?tab=readme-ov-file#command-line-tool)
 command tool is installed, this ID can be viewed through `ksuid -f inspect 
325ghCANEKjw6Jsfejg5p6QrLYB`:
   ```



##########
t/plugin/request-id3.t:
##########
@@ -0,0 +1,154 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+worker_connections(1024);
+repeat_each(1);
+no_long_string();
+no_root_location();
+
+add_block_preprocessor(sub {
+    my ($block) = @_;
+
+    if (!$block->request) {
+        $block->set_value("request", "GET /t");
+    }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: check config with algorithm ksuid
+--- 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,
+                 [[{
+                        "plugins": {
+                            "request-id": {
+                                "algorithm": "ksuid"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+                )
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- response_body
+passed
+
+
+
+=== TEST 2: hit
+--- request
+GET /opentracing
+--- error_log
+X-Request-Id
+--- no_error_log
+[error]
+
+
+=== TEST 3: add plugin with algorithm ksuid
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local http = require "resty.http"
+            local v = {}
+            local ids = {}
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                       "plugins": {
+                            "request-id": {
+                                "algorithm": "ksuid"
+                            }
+                        },
+                        "upstream": {
+                            "nodes": {
+                                "127.0.0.1:1982": 1
+                            },
+                            "type": "roundrobin"
+                        },
+                        "uri": "/opentracing"
+                }]]
+                )
+            if code >= 300 then
+                ngx.say("algorithm ksuid is error")
+            end
+            for i = 1, 180 do
+                local th = assert(ngx.thread.spawn(function()
+                    local httpc = http.new()
+                    local uri = "http://127.0.0.1:"; .. ngx.var.server_port .. 
"/opentracing"
+                    local res, err = httpc:request_uri(uri,
+                        {
+                            method = "GET",
+                            headers = {
+                                ["Content-Type"] = "application/json",
+                            }
+                        }
+                    )
+                    if not res then
+                        ngx.log(ngx.ERR, err)
+                        return
+                    end
+                    local id = res.headers["X-Request-Id"]
+                    if not id then
+                        return -- ignore if the data is not synced yet.
+                    end
+                    if #id ~= 27 then
+                        ngx.say(id)
+                        ngx.say("incorrect length for id")
+                        return
+                    end
+                    local start, en = string.find(id, '[a-zA-Z0-9]*')
+                    if start ~= 1 or en ~= 27 then
+                        ngx.say("incorrect char set for id")
+                        ngx.say(id)
+                        return
+                    end
+                    

Review Comment:
   Remove trailing whitespace on this line to maintain code cleanliness.
   ```suggestion
   
   ```



-- 
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: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to