使用Prometheus SDK输出Prometheus格式的Metrics

目录

说明

client_golang是prometheus的go client,主要有两部分:用于吐出metric的sdk,和调用prometheus的api的sdk。

通过client_golang可以在程序内集成prometheus,将metric以prometheus支持的格式吐出。

起步

client_golang中已经提供了两个例子

go get github.com/prometheus/client_golang
cd $GOPATH/src/github.com/prometheus/client_golang
``

simple是一个最简单的例子:

```go
package main

import (
    "flag"
    "log"
    "net/http"
    
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

var addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")

func main() {
    flag.Parse()
    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(*addr, nil))
}

其中promhttp.Handler()中注册了prometheus默认的metrics生成器:

func Handler() http.Handler {
    return InstrumentMetricHandler(
        prometheus.DefaultRegisterer, HandlerFor(prometheus.DefaultGatherer, HandlerOpts{}),
    )
}

编译运行:

cd simple
go build
./simple

访问”127.0.0.1:8080/metric”就可以读取到metrics。

添加指标

random复杂一点点,注册了两个采集器,注意第一个采集器后面的”service”,它是label名称:

var (
    rpcDurations = prometheus.NewSummaryVec(
        prometheus.SummaryOpts{
            Name:       "rpc_durations_seconds",
            Help:       "RPC latency distributions.",
            Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
        },
        []string{"service"},
    )
    rpcDurationsHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
        Name:    "rpc_durations_histogram_seconds",
        Help:    "RPC latency distributions.",
        Buckets: prometheus.LinearBuckets(*normMean-5**normDomain, .5**normDomain, 20),
    })
)

func init() {
    // Register the summary and the histogram with Prometheus's default registry.
    prometheus.MustRegister(rpcDurations)
    prometheus.MustRegister(rpcDurationsHistogram)
}

采集器在与采集器类型同名的.go文件中实现:

prometheus/counter.go
prometheus/gauge.go
prometheus/histogram.go
prometheus/summary.go

在主程序中可以向这两个采集器中写入指标,并可以设置label的数值:

func main() {
    ...
       v := rand.Float64() * *uniformDomain
       rpcDurations.WithLabelValues("uniform").Observe(v)
       ...
       v := (rand.NormFloat64() * *normDomain) + *normMean
       rpcDurations.WithLabelValues("normal").Observe(v)
       rpcDurationsHistogram.Observe(v)
       ...
       v := rand.ExpFloat64() / 1e6
       rpcDurations.WithLabelValues("exponential").Observe(v)
       ...
    ...
    // Expose the registered metrics via HTTP.
    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(*addr, nil))
}

注意用WithLabelValues()设置label的value的时候,参数是多个label的数值,它们的顺序必须和声明采集器时定义的相同。 否则label的值会错配,为了防止这种情况,可以用GetMetricWith(labels Labels)方法设置。

启动后可以查询到下面到指标:

rpc_durations_seconds{service="exponential",quantile="0.5"} 7.380919552318622e-07
rpc_durations_seconds{service="exponential",quantile="0.9"} 2.291519677915514e-06
rpc_durations_seconds{service="exponential",quantile="0.99"} 4.539723552933882e-06
rpc_durations_seconds_sum{service="exponential"} 0.0005097984764772547
rpc_durations_seconds_count{service="exponential"} 532

参考

  1. Prometheus SDK: client go

推荐阅读

赞助商广告

Copyright @2011-2019 All rights reserved. 转载请添加原文连接,合作请加微信lijiaocn或者发送邮件: [email protected],备注网站合作

友情链接:  李佶澳的博客  小鸟笔记  软件手册  编程手册  运营手册  爱马影视  网络课程  奇技淫巧  课程文档  精选文章  发现知识星球  百度搜索 谷歌搜索