聊聊dubbo-go的metricsFilter

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

原文 

https://segmentfault.com/a/1190000023326628

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

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

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

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

评论 0

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