共计 3788 个字符,预计需要花费 10 分钟才能阅读完成。
序
本文次要钻研一下 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
正文完