前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊dubbo-go的PrometheusReporter

聊聊dubbo-go的PrometheusReporter

原创
作者头像
code4it
修改2020-08-03 10:00:42
4370
修改2020-08-03 10:00:42
举报
文章被收录于专栏:码匠的流水账码匠的流水账

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

PrometheusReporter

dubbo-go-v1.4.2/metrics/prometheus/reporter.go

代码语言:javascript
复制
const (
    reporterName = "prometheus"
    serviceKey   = constant.SERVICE_KEY
    groupKey     = constant.GROUP_KEY
    versionKey   = constant.VERSION_KEY
    methodKey    = constant.METHOD_KEY
    timeoutKey   = constant.TIMEOUT_KEY
​
    providerKey = "provider"
    consumerKey = "consumer"
​
    // to identify the metric's type
    histogramSuffix = "_histogram"
    // to identify the metric's type
    summarySuffix = "_summary"
)
​
var (
    labelNames       = []string{serviceKey, groupKey, versionKey, methodKey, timeoutKey}
    namespace        = config.GetApplicationConfig().Name
    reporterInstance *PrometheusReporter
    reporterInitOnce sync.Once
)
​
// should initialize after loading configuration
func init() {
​
    extension.SetMetricReporter(reporterName, newPrometheusReporter)
}
​
// PrometheusReporter
// it will collect the data for Prometheus
// if you want to use this, you should initialize your prometheus.
// https://prometheus.io/docs/guides/go-application/
type PrometheusReporter struct {
​
    // report the consumer-side's summary data
    consumerSummaryVec *prometheus.SummaryVec
    // report the provider-side's summary data
    providerSummaryVec *prometheus.SummaryVec
​
    // report the provider-side's histogram data
    providerHistogramVec *prometheus.HistogramVec
    // report the consumer-side's histogram data
    consumerHistogramVec *prometheus.HistogramVec
}
  • PrometheusReporter定义了consumerSummaryVec、providerSummaryVec、providerHistogramVec、consumerHistogramVec

newPrometheusReporter

dubbo-go-v1.4.2/metrics/prometheus/reporter.go

代码语言:javascript
复制
// newPrometheusReporter create new prometheusReporter
// it will register the metrics into prometheus
func newPrometheusReporter() metrics.Reporter {
    if reporterInstance == nil {
        reporterInitOnce.Do(func() {
            reporterInstance = &PrometheusReporter{
                consumerSummaryVec: newSummaryVec(consumerKey),
                providerSummaryVec: newSummaryVec(providerKey),
​
                consumerHistogramVec: newHistogramVec(consumerKey),
                providerHistogramVec: newHistogramVec(providerKey),
            }
            prometheus.MustRegister(reporterInstance.consumerSummaryVec, reporterInstance.providerSummaryVec,
                reporterInstance.consumerHistogramVec, reporterInstance.providerHistogramVec)
        })
    }
    return reporterInstance
}
  • newPrometheusReporter方法实例化PrometheusReporter

newSummaryVec

dubbo-go-v1.4.2/metrics/prometheus/reporter.go

代码语言:javascript
复制
// newSummaryVec create SummaryVec, the Namespace is dubbo
// the objectives is from my experience.
func newSummaryVec(side string) *prometheus.SummaryVec {
    return prometheus.NewSummaryVec(
        prometheus.SummaryOpts{
            Namespace: namespace,
            Help:      "This is the dubbo's summary metrics",
            Subsystem: side,
            Name:      serviceKey + summarySuffix,
            Objectives: map[float64]float64{
                0.5:   0.01,
                0.75:  0.01,
                0.90:  0.005,
                0.98:  0.002,
                0.99:  0.001,
                0.999: 0.0001,
            },
        },
        labelNames,
    )
}
  • newSummaryVec方法执行prometheus.NewSummaryVec,其中SummaryOpts设置了0.5、0.75、0.90、0.98、0.99、0.999的Objectives

newHistogramVec

dubbo-go-v1.4.2/metrics/prometheus/reporter.go

代码语言:javascript
复制
func newHistogramVec(side string) *prometheus.HistogramVec {
    mc := config.GetMetricConfig()
    return prometheus.NewHistogramVec(
        prometheus.HistogramOpts{
            Namespace: namespace,
            Subsystem: side,
            Name:      serviceKey + histogramSuffix,
            Help:      "This is the dubbo's histogram metrics",
            Buckets:   mc.GetHistogramBucket(),
        },
        labelNames)
}
  • newHistogramVec方法执行prometheus.NewHistogramVec

Report

dubbo-go-v1.4.2/metrics/prometheus/reporter.go

代码语言:javascript
复制
// Report report the duration to Prometheus
// the role in url must be consumer or provider
// or it will be ignored
func (reporter *PrometheusReporter) Report(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation, cost time.Duration, res protocol.Result) {
    url := invoker.GetUrl()
    var sumVec *prometheus.SummaryVec
    var hisVec *prometheus.HistogramVec
    if isProvider(url) {
        sumVec = reporter.providerSummaryVec
        hisVec = reporter.providerHistogramVec
    } else if isConsumer(url) {
        sumVec = reporter.consumerSummaryVec
        hisVec = reporter.consumerHistogramVec
    } else {
        logger.Warnf("The url is not the consumer's or provider's, "+
            "so the invocation will be ignored. url: %s", url.String())
        return
    }
​
    labels := prometheus.Labels{
        serviceKey: url.Service(),
        groupKey:   url.GetParam(groupKey, ""),
        versionKey: url.GetParam(versionKey, ""),
        methodKey:  invocation.MethodName(),
        timeoutKey: url.GetParam(timeoutKey, ""),
    }
​
    costMs := float64(cost.Nanoseconds() / constant.MsToNanoRate)
    sumVec.With(labels).Observe(costMs)
    hisVec.With(labels).Observe(costMs)
}
​
// whether this url represents the application received the request as server
func isProvider(url common.URL) bool {
    role := url.GetParam(constant.ROLE_KEY, "")
    return strings.EqualFold(role, strconv.Itoa(common.PROVIDER))
}
​
// whether this url represents the application sent then request as client
func isConsumer(url common.URL) bool {
    role := url.GetParam(constant.ROLE_KEY, "")
    return strings.EqualFold(role, strconv.Itoa(common.CONSUMER))
}
  • Report方法根据url来判断是provider还是consumer,若为provider则获取reporter.providerSummaryVec、reporter.providerHistogramVec;若为consumer则获取reporter.consumerSummaryVec、reporter.consumerHistogramVec;之后设置labels,然后执行sumVec.With(labels).Observe(costMs)及hisVec.With(labels).Observe(costMs)

小结

PrometheusReporter定义了consumerSummaryVec、providerSummaryVec、providerHistogramVec、consumerHistogramVec;Report方法根据url来判断是provider还是consumer,若为provider则获取reporter.providerSummaryVec、reporter.providerHistogramVec;若为consumer则获取reporter.consumerSummaryVec、reporter.consumerHistogramVec;之后设置labels,然后执行sumVec.With(labels).Observe(costMs)及hisVec.With(labels).Observe(costMs)

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • PrometheusReporter
  • newPrometheusReporter
    • newSummaryVec
      • newHistogramVec
      • Report
      • 小结
      • doc
      相关产品与服务
      Prometheus 监控服务
      Prometheus 监控服务(TencentCloud Managed Service for Prometheus,TMP)是基于开源 Prometheus 构建的高可用、全托管的服务,与腾讯云容器服务(TKE)高度集成,兼容开源生态丰富多样的应用组件,结合腾讯云可观测平台-告警管理和 Prometheus Alertmanager 能力,为您提供免搭建的高效运维能力,减少开发及运维成本。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档