本文主要研究一下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。

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...