聊聊dubbo-go的AccessLogFilter

本文主要研究一下dubbo-go的AccessLogFilter

AccessLogFilter

dubbo-go-v1.4.2/filter/filter_impl/access_log_filter.go

type AccessLogFilter struct {
    logChan chan AccessLogData
}
  • AccessLogFilter定义了AccessLogData类型的chan

Invoke

dubbo-go-v1.4.2/filter/filter_impl/access_log_filter.go

func (ef *AccessLogFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
    accessLog := invoker.GetUrl().GetParam(constant.ACCESS_LOG_KEY, "")
    if len(accessLog) > 0 {
        accessLogData := AccessLogData{data: ef.buildAccessLogData(invoker, invocation), accessLog: accessLog}
        ef.logIntoChannel(accessLogData)
    }
    return invoker.Invoke(ctx, invocation)
}
  • Invoke方法首先通过invoker.GetUrl().GetParam获取constant.ACCESS_LOG_KEY配置,若accessLog有值,则通过ef.buildAccessLogData构建accessLogData,然后执行ef.logIntoChannel(accessLogData),最后执行invoker.Invoke(ctx, invocation)

logIntoChannel

dubbo-go-v1.4.2/filter/filter_impl/access_log_filter.go

func (ef *AccessLogFilter) logIntoChannel(accessLogData AccessLogData) {
    select {
    case ef.logChan <- accessLogData:
        return
    default:
        logger.Warn("The channel is full and the access logIntoChannel data will be dropped")
        return
    }
}
  • logIntoChannel方法通过select方法将accessLogData写入ef.logChan

buildAccessLogData

dubbo-go-v1.4.2/filter/filter_impl/access_log_filter.go

func (ef *AccessLogFilter) buildAccessLogData(_ protocol.Invoker, invocation protocol.Invocation) map[string]string {
    dataMap := make(map[string]string, 16)
    attachments := invocation.Attachments()
    dataMap[constant.INTERFACE_KEY] = attachments[constant.INTERFACE_KEY]
    dataMap[constant.METHOD_KEY] = invocation.MethodName()
    dataMap[constant.VERSION_KEY] = attachments[constant.VERSION_KEY]
    dataMap[constant.GROUP_KEY] = attachments[constant.GROUP_KEY]
    dataMap[constant.TIMESTAMP_KEY] = time.Now().Format(MessageDateLayout)
    dataMap[constant.LOCAL_ADDR], _ = attachments[constant.LOCAL_ADDR]
    dataMap[constant.REMOTE_ADDR], _ = attachments[constant.REMOTE_ADDR]

    if len(invocation.Arguments()) > 0 {
        builder := strings.Builder{}
        // todo(after the paramTypes were set to the invocation. we should change this implementation)
        typeBuilder := strings.Builder{}

        builder.WriteString(reflect.ValueOf(invocation.Arguments()[0]).String())
        typeBuilder.WriteString(reflect.TypeOf(invocation.Arguments()[0]).Name())
        for idx := 1; idx < len(invocation.Arguments()); idx++ {
            arg := invocation.Arguments()[idx]
            builder.WriteString(",")
            builder.WriteString(reflect.ValueOf(arg).String())

            typeBuilder.WriteString(",")
            typeBuilder.WriteString(reflect.TypeOf(arg).Name())
        }
        dataMap[Arguments] = builder.String()
        dataMap[Types] = typeBuilder.String()
    }

    return dataMap
}
  • buildAccessLogData方法提取invocation.Attachments()的数据来构建dataMap

writeLogToFile

dubbo-go-v1.4.2/filter/filter_impl/access_log_filter.go

func (ef *AccessLogFilter) writeLogToFile(data AccessLogData) {
    accessLog := data.accessLog
    if isDefault(accessLog) {
        logger.Info(data.toLogMessage())
        return
    }

    logFile, err := ef.openLogFile(accessLog)
    if err != nil {
        logger.Warnf("Can not open the access log file: %s, %v", accessLog, err)
        return
    }
    logger.Debugf("Append log to %s", accessLog)
    message := data.toLogMessage()
    message = message + "/n"
    _, err = logFile.WriteString(message)
    if err != nil {
        logger.Warnf("Can not write the log into access log file: %s, %v", accessLog, err)
    }
}
  • writeLogToFile方法通过logFile.WriteString(message)将log写到文件

GetAccessLogFilter

dubbo-go-v1.4.2/filter/filter_impl/access_log_filter.go

func GetAccessLogFilter() filter.Filter {
    accessLogFilter := &AccessLogFilter{logChan: make(chan AccessLogData, LogMaxBuffer)}
    go func() {
        for accessLogData := range accessLogFilter.logChan {
            accessLogFilter.writeLogToFile(accessLogData)
        }
    }()
    return accessLogFilter
}
  • GetAccessLogFilter方法会创建AccessLogFilter,同时异步遍历accessLogFilter.logChan执行accessLogFilter.writeLogToFile(accessLogData)

小结

AccessLogFilter定义了AccessLogData类型的chan;Invoke方法首先通过invoker.GetUrl().GetParam获取constant.ACCESS_LOG_KEY配置,若accessLog有值,则通过ef.buildAccessLogData构建accessLogData,然后执行ef.logIntoChannel(accessLogData),最后执行invoker.Invoke(ctx, invocation)

doc

  • access_log_filter

原文 

https://segmentfault.com/a/1190000023230710

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » 聊聊dubbo-go的AccessLogFilter

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址