Skip to content

feat(http/kafka-logger): support to log response body#5550

Merged
spacewander merged 11 commits into
apache:masterfrom
dmsolr:resp_body
Nov 26, 2021
Merged

feat(http/kafka-logger): support to log response body#5550
spacewander merged 11 commits into
apache:masterfrom
dmsolr:resp_body

Conversation

@dmsolr

@dmsolr dmsolr commented Nov 19, 2021

Copy link
Copy Markdown
Member

What this PR does / why we need it:

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

Closes #5344

Comment thread apisix/plugins/http-logger.lua
Comment thread apisix/plugins/http-logger.lua Outdated


function _M.body_filter(conf, ctx)
if conf.include_resp_body then

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor this part into a method in log-util.

@juzhiyuan

Copy link
Copy Markdown
Member

Hi @dmsolr, please update your PR to resolve those conflicts 😄 Thanks!

Comment thread apisix/plugins/http-logger.lua Outdated


function _M.check_schema(conf, schema_type)
local ok, err = log_util.check_log_scheme(conf)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not check log schema with the TYPE_METADATA schema_type. And it is schema, not scheme.

Comment thread apisix/utils/log-util.lua Outdated
Comment thread apisix/utils/log-util.lua Outdated
Daming and others added 2 commits November 23, 2021 10:30
| buffer_duration | integer | 可选 | 60 | [1,...] | 必须先处理批次中最旧条目的最长期限(以秒为单位)。 |
| max_retry_count | integer | 可选 | 0 | [0,...] | 从处理管道中移除之前的最大重试次数。 |
| retry_delay | integer | 可选 | 1 | [0,...] | 如果执行失败,则应延迟执行流程的秒数。 |
| include_req_body | boolean | 可选 | false | [false, true] | 是否包括请求 body。false: 表示不包含请求的 body ;true: 表示包含请求的 body。注意:如果请求 body 没办法完全放在内存中,由于 Nginx 的限制,我们没有办法把它记录下来。|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old change is overrided.

| max_retry_count | integer | optional | 0 | [0,...] | Maximum number of retries before removing from the processing pipe line. |
| retry_delay | integer | optional | 1 | [0,...] | Number of seconds the process execution should be delayed if the execution fails. |
| include_req_body | boolean | optional | false | [false, true] | Whether to include the request body. false: indicates that the requested body is not included; true: indicates that the requested body is included. |
| include_resp_body| boolean | optional | false | [false, true] | Whether to include the response body. The response body is included if and only if it is `true`. |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing doc of include_resp_body_expr

Comment thread apisix/utils/log-util.lua Outdated
conf.response_expr = response_expr
end

local result = conf.response_expr:eval(ctx.var)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to cache eval result to the ctx

Comment thread apisix/plugins/kafka-logger.lua Outdated
return core.schema.check(metadata_schema, conf)
end

local ok, err = log_util.check_log_schema(conf)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should run core.schema.check first before check_log_schema

Comment thread apisix/plugins/kafka-logger.lua Outdated

local ok, err = log_util.check_log_schema(conf)
if not ok then
return err

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return err
return nil, err

Comment thread apisix/utils/log-util.lua Outdated
local ok, err = expr.new(conf.include_req_body_expr)
if not ok then
return nil,
{error_msg = "failed to validate the 'include_req_body_expr' expression: " .. err}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should return err.
And please add a test to cover this.

daming added 2 commits November 24, 2021 21:17
Comment thread docs/zh/latest/plugins/kafka-logger.md Outdated
| retry_delay | integer | 可选 | 1 | [0,...] | 如果执行失败,则应延迟执行流程的秒数。 |
| include_req_body | boolean | 可选 | false | [false, true] | 是否包括请求 body。false: 表示不包含请求的 body ;true: 表示包含请求的 body。注意:如果请求 body 没办法完全放在内存中,由于 Nginx 的限制,我们没有办法把它记录下来。|
| include_req_body_expr | array | 可选 | | | 当 `include_req_body` 开启时, 基于 [lua-resty-expr](https://github.com/api7/lua-resty-expr) 表达式的结果进行记录。如果该选项存在,只有在表达式为真的时候才会记录请求 body。 |
| include_req_body | boolean | 可选 | false | [false, true] | 是否包括请求 body。false: 表示不包含请求的 body ; true: 表示包含请求的 body 。|

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注意:如果请求 body 没办法完全放在内存中,由于 Nginx 的限制,我们没有办法把它记录下来。 This part is overridden.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry.
I think there is the same limitation in the http-logger plugin. Right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Comment thread t/plugin/http-logger.t Outdated


=== TEST 4: add plugin
=== TEST 4: check log schema(include_resp_body_expr)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add the test at the end, so the other test numbers won't be changed? We can use reindex to fix the number.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@spacewander spacewander changed the title feat: support to log response body feat(http/kafka-logger): support to log response body Nov 26, 2021
@spacewander
spacewander merged commit 49a539b into apache:master Nov 26, 2021
bd1dtn pushed a commit to bd1dtn/apisix that referenced this pull request Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

request help: kafka logger supports logging response body

4 participants