Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support to log custom labels of route by default #6181

Closed
wants to merge 9 commits into from
Closed

feat: support to log custom labels of route by default #6181

wants to merge 9 commits into from

Conversation

jagerzhang
Copy link
Contributor

@jagerzhang jagerzhang commented Jan 21, 2022

What this PR does / why we need it:

supports to log custom labels of route when the log_format not defined(Code reference tapisix by @kyroslin): #5998

@spacewander
Copy link
Member

We already provide a mechanism to do it: #6163
Please do it in your own fork.

@moonming
Copy link
Member

This PR is to log some data without changing the file format. Is it different?

@moonming
Copy link
Member

I will reopen this PR for more discussions

@moonming moonming reopened this Jan 21, 2022
@jagerzhang
Copy link
Contributor Author

jagerzhang commented Jan 22, 2022

@moonming 感谢回复。

This PR is to log some data without changing the file format. Is it different?

日志格式有两种定义方式:通过log_format自定义日志格式直接使用APISIX的默认全日志格式,我说下两种日志方式的个人见解:

自定义日志格式

这个模式,需要用户自定义log_format。我觉得这个场景有2个难点:

比如space小哥这里提的优化:#6163 支持用户自定义日志格式,也能够记录路由里面具体的路由标签,用这个方法除了有上述难点之外,这个方法记录的路由标签也是固定不变的,如果路由标签新增了其他keyword,将不会被记录,需要重新定义日志格式,增加了运维复杂度。

总体来说,这种方式的上手门槛和运维成本较高,所以我们没有选择这个方式。

默认的全日志格式

这个模式,不需要用户自定义日志就能得到非常全面的日志格式(通过 log_util.get_full_log(conf, ctx) 生成),没有上手门槛和运维成本,前面我还提了个PR进一步支持了记录多阶段延迟字段:#6063 ,从生成函数名称(get_full_log)来看,官方也是将其称为APISIX全日志,既然是全日志,那路由里面的自定义labels是不是也应该支持记录进去?可以看到,我对kafka-logger的修改也只是将路由自定义的labels放到了get_full_log逻辑块里面,并没有污染到其他自定义日志的场景。

最后,我说下我们为什么需要这个自定义标签

前面我也提交了相关issue: #5998 我们是想通过在路由里面定义标签,实现和生产业务的关联关系,这样做的好处很多,比如后续网关用户可以在网关服务门户能快速提取具体路由对应的请求日志,而不需要到日志里面去进行复杂的索引(我们目前正在将APISIX包装成一站式的七层接入服务),再比如可以快速聚合分析各业务、应用的请求流量,更准确的将告警送达到具体业务等等,而不需要通过复杂的域名和路由组合条件来关联到具体业务。

以上就是我这边的一些理解和说明,如果讲得不对请指正。

如果最终确实觉得路由自定义标签不适合默认就添加到全日志当中,能否考虑一下以下实现方式:
方式1、kafka-logger 插件新增schema选项,支持用户打开记录路由自定义labels字段的功能;
方式2、kafka-logger 插件新增schema选项,支持用户设置指定需要记录到日志的label,而不是全部label。这个方式和 #6163 也算是一个结合使用。

如果都觉得不合理,那我们只能单独维护 kafka-logger.lua 这个文件来实现这个功能了。

@spacewander
Copy link
Member

It is very unsuitable to add every field to the default log format. And there is no need to maintain your own kafka-logger.

Your understanding of APISIX is very very incorrect.

比如space小哥这里提的优化:#6163 支持用户自定义日志格式,也能够记录路由里面具体的路由标签,用这个方法除了有上述难点之外,这个方法记录的路由标签也是固定不变的,如果路由标签新增了其他keyword,将不会被记录,需要重新定义日志格式,增加了运维复杂度。

Please read the code carefully. All labels are logged.

如何通过log_format来实现和默认全日志(full log)一样的日志内容?这个之前求助过,实际没有结论

Solved. Use a custom variable to call the get_full_log.

log_format 一旦定义就是静态的设置,后续如果APISIX的日志格式有所优化,得通过修改log_format来更新

The current way is to avoid putting any more fields into the full log. Every new field should be in the custom log.

@jagerzhang
Copy link
Contributor Author

@spacewander https://github.com/apache/apisix/pull/5941/files 求问下,我看到这个特性需要自己写代码注册自定义变量:

local core = require "apisix.core"
    core.ctx.register_var("a6_route_labels", function(ctx)
        local route = ctx.matched_route and ctx.matched_route.value
        if route and route.labels then
            return route.labels
        end
        return nil
    end)

那这个代码是写到哪里呢? 我只是一个普通APISIX用户,并不是开发..确实没看出来应该怎么将这个路由labels记录到kafka-logger里面。

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.

3 participants