membphis commented on a change in pull request #2026: URL: https://github.com/apache/apisix/pull/2026#discussion_r467442848
########## File path: apisix/plugins/request-id.lua ########## @@ -0,0 +1,67 @@ +-- +-- 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 core = require("apisix.core") +local plugin_name = "request-id" +local ngx = ngx +local uuid = require("resty.jit-uuid") + +local schema = { + type = "object", + properties = { + header_name = {type = "string", default = "X-Request-Id"}, + include_in_response = {type = "boolean", default = false} + } +} + + +local _M = { + version = 0.1, + priority = 11010, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + + +function _M.rewrite(conf) + local headers = ngx.req.get_headers() + uuid.seed() Review comment: we have set the right `seed`, so we do not need to set it again: https://github.com/apache/apisix/blob/master/apisix/init.lua#L71 let we remove it. ########## File path: apisix/plugins/request-id.lua ########## @@ -0,0 +1,67 @@ +-- +-- 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 core = require("apisix.core") +local plugin_name = "request-id" +local ngx = ngx +local uuid = require("resty.jit-uuid") + +local schema = { + type = "object", + properties = { + header_name = {type = "string", default = "X-Request-Id"}, + include_in_response = {type = "boolean", default = false} + } +} + + +local _M = { + version = 0.1, + priority = 11010, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + + +function _M.rewrite(conf) + local headers = ngx.req.get_headers() + uuid.seed() + local uuid_val = uuid() + if not headers[conf.header_name] then + core.request.set_header(conf.header_name, uuid_val) + end + + if conf.include_in_response then + ngx.ctx.api_ctx.x_request_id = uuid_val Review comment: `ctx.x_request_id = uuid_val`, simpler way ########## File path: apisix/plugins/request-id.lua ########## @@ -0,0 +1,67 @@ +-- +-- 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 core = require("apisix.core") +local plugin_name = "request-id" +local ngx = ngx +local uuid = require("resty.jit-uuid") + +local schema = { + type = "object", + properties = { + header_name = {type = "string", default = "X-Request-Id"}, + include_in_response = {type = "boolean", default = false} + } +} + + +local _M = { + version = 0.1, + priority = 11010, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + + +function _M.rewrite(conf) Review comment: `_M.rewrite(conf)` -> `_M.rewrite(conf, ctx)` ########## File path: apisix/plugins/request-id.lua ########## @@ -0,0 +1,67 @@ +-- +-- 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 core = require("apisix.core") +local plugin_name = "request-id" +local ngx = ngx +local uuid = require("resty.jit-uuid") + +local schema = { + type = "object", + properties = { + header_name = {type = "string", default = "X-Request-Id"}, + include_in_response = {type = "boolean", default = false} + } +} + + +local _M = { + version = 0.1, + priority = 11010, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf) + return core.schema.check(schema, conf) +end + + +function _M.rewrite(conf) + local headers = ngx.req.get_headers() + uuid.seed() + local uuid_val = uuid() + if not headers[conf.header_name] then + core.request.set_header(conf.header_name, uuid_val) + end + + if conf.include_in_response then + ngx.ctx.api_ctx.x_request_id = uuid_val + end +end + + +function _M.header_filter(conf, ctx) + if conf.include_in_response then Review comment: another style: ```lua if not conf.include_in_response then return end local headers = ngx.resp.get_headers() if not headers[conf.header_name] then core.response.set_header(conf.header_name, ctx.x_request_id) 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. For queries about this service, please contact Infrastructure at: [email protected]
