本文主要研究一下dubbo-go-proxy的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方法
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。