欢迎加入go语言学习交流群 636728449git
Prometheus笔记(二)监控go项目实时给grafana展现
Prometheus笔记(一)metric typegithub
Prometheus客户端库提供四种核心度量标准类型。 这些目前仅在客户端库中区分(以启用针对特定类型的使用而定制的API)和有线协议。 Prometheus服务器还没有使用类型信息,并将全部数据展平为无类型时间序列。(本文全部示例代码都是使用go来举例的)golang
计数器是表示单个单调递增计数器的累积量,其值只能增长或在重启时重置为零。 例如,您可使用计数器来表示服务的总请求数,已完成的任务或错误总数。 不要使用计数器来监控可能减小的值。 例如,不要使用计数器来处理当前正在运行的进程数,而应该用Gauge。服务器
counter主要有两个方法:app
//将counter值加1. Inc() // 将指定值加到counter值上,若是指定值< 0会panic. Add(float64)
通常 metric 容器使用的步骤都是:ide
一、初始化一个metric容器学习
二、Register注册容器this
三、向容器中添加值spa
使用举例:.net
//step1:初始一个counter pushCounter := prometheus.NewCounter(prometheus.CounterOpts{ Name: "repository_pushes", // 注意: 没有help字符串 }) err := prometheus.Register(pushCounter) // 会返回一个错误. if err != nil { fmt.Println("Push counter couldn't be registered, no counting will happen:", err) return } // Try it once more, this time with a help string. pushCounter = prometheus.NewCounter(prometheus.CounterOpts{ Name: "repository_pushes", Help: "Number of pushes to external repository.", }) //setp2: 注册容器 err = prometheus.Register(pushCounter) if err != nil { fmt.Println("Push counter couldn't be registered AGAIN, no counting will happen:", err) return } pushComplete := make(chan struct{}) // TODO: Start a goroutine that performs repository pushes and reports // each completion via the channel. for range pushComplete { //step3:向容器中写入值 pushCounter.Inc() }
输出:
Push counter couldn't be registered, no counting will happen: descriptor Desc{fqName: "repository_pushes", help: "", constLabels: {}, variableLabels: []} is invalid: empty help string
CounterVec是一组counter,这些计数器具备相同的描述,但它们的变量标签具备不一样的值。 若是要计算按各类维度划分的相同内容(例如,响应代码和方法分区的HTTP请求数),则使用此方法。使用NewCounterVec建立实例。
//step1:初始化一个容器 httpReqs := prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "http_requests_total", Help: "How many HTTP requests processed, partitioned by status code and HTTP method.", }, []string{"code", "method"}, ) //step2:注册容器 prometheus.MustRegister(httpReqs) httpReqs.WithLabelValues("404", "POST").Add(42) // If you have to access the same set of labels very frequently, it // might be good to retrieve the metric only once and keep a handle to // it. But beware of deletion of that metric, see below! //step3:向容器中写入值,主要调用容器的方法如Inc()或者Add()方法 m := httpReqs.WithLabelValues("200", "GET") for i := 0; i < 1000000; i++ { m.Inc() } // Delete a metric from the vector. If you have previously kept a handle // to that metric (as above), future updates via that handle will go // unseen (even if you re-create a metric with the same label set // later). httpReqs.DeleteLabelValues("200", "GET") // Same thing with the more verbose Labels syntax. httpReqs.Delete(prometheus.Labels{"method": "GET", "code": "200"})
Gauge能够用来存放一个能够任意变大变小的数值,一般用于测量值,例如温度或当前内存使用状况,或者运行的goroutine数量
主要有如下四个方法
// 将Gauge中的值设为指定值. Set(float64) // 将Gauge中的值加1. Inc() // 将Gauge中的值减1. Dec() // 将指定值加到Gauge中的值上。(指定值能够为负数) Add(float64) // 将指定值从Gauge中的值减掉。(指定值能够为负数) Sub(float64)
示例代码(实时统计CPU的温度):
//step1:初始化容器 cpuTemprature := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "CPU_Temperature", Help: "the temperature of CPU", }) //step2:注册容器 prometheus.MustRegister(cpuTemprature) //定时获取cpu温度而且写入到容器 func(){ tem = getCpuTemprature() //step3:向容器中写入值。调用容器的方法 cpuTemprature.Set(tem) }
假设你要一次性统计四个cpu的温度,这个时候就适合使用GaugeVec了。
cpusTemprature := prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "CPUs_Temperature", Help: "the temperature of CPUs.", }, []string{ // Which cpu temperature? "cpuName", }, ) prometheus.MustRegister(cpusTemprature) cpusTemprature.WithLabelValues("cpu1").Set(temperature1) cpusTemprature.WithLabelValues("cpu2").Set(temperature2) cpusTemprature.WithLabelValues("cpu3").Set(temperature3)
Summary从事件或样本流中捕获单个观察,并以相似于传统汇总统计的方式对其进行汇总:1。观察总和,2。观察计数,3。排名估计。典型的用例是观察请求延迟。 默认状况下,Summary提供延迟的中位数。
temps := prometheus.NewSummary(prometheus.SummaryOpts{ Name: "pond_temperature_celsius", Help: "The temperature of the frog pond.", Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, }) // Simulate some observations. for i := 0; i < 1000; i++ { temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10) } // Just for demonstration, let's check the state of the summary by // (ab)using its Write method (which is usually only used by Prometheus // internally). metric := &dto.Metric{} temps.Write(metric) fmt.Println(proto.MarshalTextString(metric))
主要用于表示一段时间范围内对数据进行采样,(一般是请求持续时间或响应大小),并可以对其指定区间以及总数进行统计,一般咱们用它计算分位数的直方图。
temps := prometheus.NewHistogram(prometheus.HistogramOpts{ Name: "pond_temperature_celsius", Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells. Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide. }) // Simulate some observations. for i := 0; i < 1000; i++ { temps.Observe(30 + math.Floor(120*math.Sin(float64(i)*0.1))/10) } // Just for demonstration, let's check the state of the histogram by // (ab)using its Write method (which is usually only used by Prometheus // internally). metric := &dto.Metric{} temps.Write(metric) fmt.Println(proto.MarshalTextString(metric))
欢迎加入go语言学习交流群 636728449
[1] https://godoc.org/github.com/prometheus/client_golang/prometheus
[2] https://prometheus.io/docs/introduction/overview/