You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "shreemaan-abhishek (via GitHub)" <gi...@apache.org> on 2024/04/09 07:00:59 UTC
[PR] feat(kfk-logger): add max req/resp body size attributes [apisix]
shreemaan-abhishek opened a new pull request, #11133:
URL: https://github.com/apache/apisix/pull/11133
### Description
In cases where the req/response body is very large and when include_req_body/resp_body is enabled this leads to very high CPU usage and can even cause unavailability. To fix such event, two new attributes are introduced:
- max_resp_body_bytes
- max_req_body_bytes
### Checklist
- [x] I have explained the need for this PR and the problem it solves
- [x] I have explained the changes or the new features added to this PR
- [x] I have added tests corresponding to this change
- [ ] I have updated the documentation to reflect this change
- [ ] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
<!--
Note
1. Mark the PR as draft until it's ready to be reviewed.
2. Always add/update tests for any changes unless you have a good reason.
3. Always update the documentation to reflect the changes made in the PR.
4. Make a new commit to resolve conversations instead of `push -f`.
5. To resolve merge conflicts, merge master instead of rebasing.
6. Use "request review" to notify the reviewer after making changes.
7. Only a reviewer can mark a conversation as resolved.
-->
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kafka-logger): add max req/resp body size attributes [apisix]
Posted by "moonming (via GitHub)" <gi...@apache.org>.
moonming merged PR #11133:
URL: https://github.com/apache/apisix/pull/11133
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kafka-logger): add max req/resp body size attributes [apisix]
Posted by "starsz (via GitHub)" <gi...@apache.org>.
starsz commented on code in PR #11133:
URL: https://github.com/apache/apisix/pull/11133#discussion_r1560568373
##########
apisix/core/response.lua:
##########
@@ -192,22 +192,35 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
n = 1
}
ctx._body_buffer[ctx._plugin_name] = body_buffer
+ ctx._resp_body_bytes = #chunk
else
local n = body_buffer.n + 1
body_buffer.n = n
body_buffer[n] = chunk
+ ctx._resp_body_bytes = ctx._resp_body_bytes + #chunk
+ end
+ if max_resp_body_bytes and ctx._resp_body_bytes >= max_resp_body_bytes then
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
+ return body_data
end
end
if eof then
body_buffer = ctx._body_buffer[ctx._plugin_name]
if not body_buffer then
+ if max_resp_body_bytes and #chunk >= max_resp_body_bytes then
+ chunk = str_sub(chunk, 1, max_resp_body_bytes)
+ end
return chunk
end
- body_buffer = concat_tab(body_buffer, "", 1, body_buffer.n)
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
ctx._body_buffer[ctx._plugin_name] = nil
- return body_buffer
+ if max_resp_body_bytes and #body_data >= max_resp_body_bytes then
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
Review Comment:
I think if it's the first chunk, it will be dealed in line ~202
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kafka-logger): add max req/resp body size attributes [apisix]
Posted by "kayx23 (via GitHub)" <gi...@apache.org>.
kayx23 commented on code in PR #11133:
URL: https://github.com/apache/apisix/pull/11133#discussion_r1719578897
##########
docs/en/latest/plugins/kafka-logger.md:
##########
@@ -55,8 +55,10 @@ It might take some time to receive the log data. It will be automatically sent a
| log_format | object | False | | | Log format declared as key value pairs in JSON format. Values only support strings. [APISIX](../apisix-variable.md) or [Nginx](http://nginx.org/en/docs/varindex.html) variables can be used by prefixing the string with `$`. |
| include_req_body | boolean | False | false | [false, true] | When set to `true` includes the request body in the log. If the request body is too big to be kept in the memory, it can't be logged due to Nginx's limitations. |
| include_req_body_expr | array | False | | | Filter for when the `include_req_body` attribute is set to `true`. Request body is only logged when the expression set here evaluates to `true`. See [lua-resty-expr](https://github.com/api7/lua-resty-expr) for more. |
+| max_req_body_bytes | integer | False | 524288 | >=1 | Request bodies within this size will be pushed to kafka, if the size exceeds the configured value it will be truncated before pushing to Kafka. |
| include_resp_body | boolean | False | false | [false, true] | When set to `true` includes the response body in the log. |
| include_resp_body_expr | array | False | | | Filter for when the `include_resp_body` attribute is set to `true`. Response body is only logged when the expression set here evaluates to `true`. See [lua-resty-expr](https://github.com/api7/lua-resty-expr) for more. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Request bodies within this size will be pushed to kafka, if the size exceeds the configured value it will be truncated before pushing to Kafka. |
Review Comment:
@shreemaan-abhishek this should be response bodies eh?
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kafka-logger): add max req/resp body size attributes [apisix]
Posted by "shreemaan-abhishek (via GitHub)" <gi...@apache.org>.
shreemaan-abhishek commented on code in PR #11133:
URL: https://github.com/apache/apisix/pull/11133#discussion_r1562017809
##########
apisix/core/response.lua:
##########
@@ -192,22 +192,35 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
n = 1
}
ctx._body_buffer[ctx._plugin_name] = body_buffer
+ ctx._resp_body_bytes = #chunk
else
local n = body_buffer.n + 1
body_buffer.n = n
body_buffer[n] = chunk
+ ctx._resp_body_bytes = ctx._resp_body_bytes + #chunk
+ end
+ if max_resp_body_bytes and ctx._resp_body_bytes >= max_resp_body_bytes then
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
+ return body_data
end
end
if eof then
body_buffer = ctx._body_buffer[ctx._plugin_name]
if not body_buffer then
+ if max_resp_body_bytes and #chunk >= max_resp_body_bytes then
+ chunk = str_sub(chunk, 1, max_resp_body_bytes)
+ end
return chunk
end
- body_buffer = concat_tab(body_buffer, "", 1, body_buffer.n)
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
ctx._body_buffer[ctx._plugin_name] = nil
- return body_buffer
+ if max_resp_body_bytes and #body_data >= max_resp_body_bytes then
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
Review Comment:
hmm now it makes sense but I think we are calling sub on 213 on `chunk` because `body_buffer` is nil. And on 221 we are calling it on body data formed after `concat_tab` the `body_buffer` table.
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kafka-logger): add max req/resp body size attributes [apisix]
Posted by "kayx23 (via GitHub)" <gi...@apache.org>.
kayx23 commented on code in PR #11133:
URL: https://github.com/apache/apisix/pull/11133#discussion_r1719578897
##########
docs/en/latest/plugins/kafka-logger.md:
##########
@@ -55,8 +55,10 @@ It might take some time to receive the log data. It will be automatically sent a
| log_format | object | False | | | Log format declared as key value pairs in JSON format. Values only support strings. [APISIX](../apisix-variable.md) or [Nginx](http://nginx.org/en/docs/varindex.html) variables can be used by prefixing the string with `$`. |
| include_req_body | boolean | False | false | [false, true] | When set to `true` includes the request body in the log. If the request body is too big to be kept in the memory, it can't be logged due to Nginx's limitations. |
| include_req_body_expr | array | False | | | Filter for when the `include_req_body` attribute is set to `true`. Request body is only logged when the expression set here evaluates to `true`. See [lua-resty-expr](https://github.com/api7/lua-resty-expr) for more. |
+| max_req_body_bytes | integer | False | 524288 | >=1 | Request bodies within this size will be pushed to kafka, if the size exceeds the configured value it will be truncated before pushing to Kafka. |
| include_resp_body | boolean | False | false | [false, true] | When set to `true` includes the response body in the log. |
| include_resp_body_expr | array | False | | | Filter for when the `include_resp_body` attribute is set to `true`. Response body is only logged when the expression set here evaluates to `true`. See [lua-resty-expr](https://github.com/api7/lua-resty-expr) for more. |
+| max_resp_body_bytes | integer | False | 524288 | >=1 | Request bodies within this size will be pushed to kafka, if the size exceeds the configured value it will be truncated before pushing to Kafka. |
Review Comment:
@shreemaan-abhishek this should be response bodies eh?
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kafka-logger): add max req/resp body size attributes [apisix]
Posted by "starsz (via GitHub)" <gi...@apache.org>.
starsz commented on code in PR #11133:
URL: https://github.com/apache/apisix/pull/11133#discussion_r1558782874
##########
apisix/plugins/kafka-logger.lua:
##########
@@ -22,6 +23,7 @@ local bp_manager_mod = require("apisix.utils.batch-processor-manager")
local math = math
local pairs = pairs
local type = type
+local req_body = ngx.req.read_body
Review Comment:
```suggestion
local req_read_body = ngx.req.read_body
```
##########
apisix/core/response.lua:
##########
@@ -192,22 +192,35 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
n = 1
}
ctx._body_buffer[ctx._plugin_name] = body_buffer
+ ctx._resp_body_bytes = #chunk
else
local n = body_buffer.n + 1
body_buffer.n = n
body_buffer[n] = chunk
+ ctx._resp_body_bytes = ctx._resp_body_bytes + #chunk
+ end
+ if max_resp_body_bytes and ctx._resp_body_bytes >= max_resp_body_bytes then
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
+ return body_data
end
end
if eof then
body_buffer = ctx._body_buffer[ctx._plugin_name]
if not body_buffer then
+ if max_resp_body_bytes and #chunk >= max_resp_body_bytes then
+ chunk = str_sub(chunk, 1, max_resp_body_bytes)
+ end
return chunk
end
- body_buffer = concat_tab(body_buffer, "", 1, body_buffer.n)
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
ctx._body_buffer[ctx._plugin_name] = nil
- return body_buffer
+ if max_resp_body_bytes and #body_data >= max_resp_body_bytes then
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
Review Comment:
Why do we need to sub the body_data again here?
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] feat(kafka-logger): add max req/resp body size attributes [apisix]
Posted by "shreemaan-abhishek (via GitHub)" <gi...@apache.org>.
shreemaan-abhishek commented on code in PR #11133:
URL: https://github.com/apache/apisix/pull/11133#discussion_r1559207461
##########
apisix/core/response.lua:
##########
@@ -192,22 +192,35 @@ function _M.hold_body_chunk(ctx, hold_the_copy)
n = 1
}
ctx._body_buffer[ctx._plugin_name] = body_buffer
+ ctx._resp_body_bytes = #chunk
else
local n = body_buffer.n + 1
body_buffer.n = n
body_buffer[n] = chunk
+ ctx._resp_body_bytes = ctx._resp_body_bytes + #chunk
+ end
+ if max_resp_body_bytes and ctx._resp_body_bytes >= max_resp_body_bytes then
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
+ return body_data
end
end
if eof then
body_buffer = ctx._body_buffer[ctx._plugin_name]
if not body_buffer then
+ if max_resp_body_bytes and #chunk >= max_resp_body_bytes then
+ chunk = str_sub(chunk, 1, max_resp_body_bytes)
+ end
return chunk
end
- body_buffer = concat_tab(body_buffer, "", 1, body_buffer.n)
+ local body_data = concat_tab(body_buffer, "", 1, body_buffer.n)
ctx._body_buffer[ctx._plugin_name] = nil
- return body_buffer
+ if max_resp_body_bytes and #body_data >= max_resp_body_bytes then
+ body_data = str_sub(body_data, 1, max_resp_body_bytes)
Review Comment:
in 213, we just sub the chunk this happens in the case when this is the first and last chunk of body_filter phase. (since eof == true and body_buffer is nil).
in 221 we need to sub because it is the last chunk but not the very first one.
--
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-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org