聊聊dubbo-go-proxy的loggerFilter

Posted codecraft

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聊聊dubbo-go-proxy的loggerFilter相关的知识,希望对你有一定的参考价值。

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

  • dubbo-go-proxy

以上是关于聊聊dubbo-go-proxy的loggerFilter的主要内容,如果未能解决你的问题,请参考以下文章

聊聊dubbo-go-proxy的recoveryFilter

聊聊dubbo-go-proxy的ParamMapper

聊聊dubbo-go-proxy的ZookeeperRegistryLoad

聊聊dubbo-go-proxy的authorityFilter

聊聊dubbo-go-proxy的Client

聊聊dubbo-go-proxy的Route