序
本文主要研究一下dubbo-go-proxy的loggerFilter
loggerFilter
dubbo-go-proxy/pkg/filter/logger/logger.go
// nolint
func Init() {
extension.SetFilterFunc(constant.LoggerFilter, loggerFilterFunc())
}
func loggerFilterFunc() context.FilterFunc {
return New().Do()
}
// loggerFilter is a filter for simple logger.
type loggerFilter struct {
}
// New create logger filter.
func New() filter.Filter {
return &loggerFilter{}
}
loggerFilter的Init方法往extension设置了loggerFilterFunc,该context.FilterFunc执行loggerFilter的Do方法
Do
dubbo-go-proxy/pkg/filter/logger/logger.go
// Logger logger filter, print url and latency
func (f loggerFilter) Do() context.FilterFunc {
return func(c context.Context) {
start := time.Now()
c.Next()
latency := time.Now().Sub(start)
logger.Infof("[dubboproxy go] [UPSTREAM] receive request | %d | %s | %s | %s | ", c.StatusCode(), latency, c.GetMethod(), c.GetUrl())
}
}
Do方法先记录下start,之后执行c.Next(),最后计算latency,最后打印下日志,输出status code,latency、method、url
小结
dubbo-go-proxy的loggerFilter的Init方法往extension设置了loggerFilterFunc,该context.FilterFunc执行loggerFilter的Do方法;Do方法先记录下start,之后执行c.Next(),最后计算latency,最后打印下日志,输出status code,latency、method、url。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。