共计 2055 个字符,预计需要花费 6 分钟才能阅读完成。
序
本文次要钻研一下 dubbo-go 的 metricsFilter
metricsFilter
dubbo-go-v1.4.2/filter/filter_impl/metrics_filter.go
const (metricFilterName = "metrics")
var (metricFilterInstance filter.Filter)
// must initialized before using the filter and after loading configuration
func init() {extension.SetFilter(metricFilterName, newMetricsFilter)
}
// metricFilter will calculate the invocation's duration and the report to the reporters
// If you want to use this filter to collect the metrics,
// Adding this into your configuration file, like:
// filter: "metrics"
// metrics:
// reporter:
// - "your reporter" # here you should specify the reporter, for example 'prometheus'
// more info please take a look at dubbo-samples projects
type metricsFilter struct {reporters []metrics.Reporter
}
- metricsFilter 定义了 reporters 属性
newMetricsFilter
dubbo-go-v1.4.2/filter/filter_impl/metrics_filter.go
func newMetricsFilter() filter.Filter {
if metricFilterInstance == nil {reporterNames := config.GetMetricConfig().Reporters
reporters := make([]metrics.Reporter, 0, len(reporterNames))
for _, name := range reporterNames {reporters = append(reporters, extension.GetMetricReporter(name))
}
metricFilterInstance = &metricsFilter{reporters: reporters,}
}
return metricFilterInstance
}
- newMetricsFilter 办法在 metricFilterInstance 为 null 的时候会获取 config.GetMetricConfig().Reporters,而后创立 reporters 及 metricsFilter
Invoke
dubbo-go-v1.4.2/filter/filter_impl/metrics_filter.go
func (p *metricsFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {start := time.Now()
res := invoker.Invoke(ctx, invocation)
end := time.Now()
duration := end.Sub(start)
go func() {
for _, reporter := range p.reporters {reporter.Report(ctx, invoker, invocation, duration, res)
}
}()
return res
}
- Invoke 办法在 invoker.Invoke(ctx, invocation) 前后记录时间,最初算出 duration,然异步遍历 p.reporters,执行 reporter.Report
OnResponse
dubbo-go-v1.4.2/filter/filter_impl/metrics_filter.go
func (p *metricsFilter) OnResponse(ctx context.Context, res protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {return res}
- OnResponse 办法目前间接返回 result
小结
metricsFilter 的 Invoke 办法在 invoker.Invoke(ctx, invocation) 前后记录时间,最初算出 duration,然异步遍历 p.reporters,执行 reporter.Report
doc
- metrics_filter
正文完