Prometheus之Exporter开发
Prometheus开发Exporter简介
Exporter 本身是一个http 服务,其指标结果只要符合 Prometheus 规范就可以被 Prometheus 使用。
Prometheus中metric的类型
Prometheus的Client Library提供度量的四种基本类型包括
// Counter 计数器
// Gauge 仪表盘
// Histogram 直方图
// Summary 概要
// Prometheus中metric的格式
// 格式:<metric name>{<label name>=<label value>, ...}
// 例如:api_http_requests_total{method="POST", handler="/messages"}
// metric name: 唯一标识,命名遵循[a-zA-Z_:][a-zA-Z0-9_:]*.
Counter
Counter类型好比计数器,用于统计类似于: cpu时间,api访问总次数,异常发生次数等等场景,这些指标的特点就是增加不减少.
因此当我们需要统计cpu的使用率时,我们需要使用rate{}函数计算该counter在过去一段时间每个时间序列上的每秒的平均增长率.
一个Counter标识一个累计度量,只增不减,重启后恢复为0,适用于访问次数统计,异常次数统计等场景.
Gauge
Gauge类型,英文直译的话叫"计量器",但是和Counter的翻译太类似了,因此我个人更喜欢使用"仪表盘"这个称呼,仪表盘的特点就是可以增加或者减少的,因此Gauge适合用于如: 当前内存使用率,当前cpu使用率,当前温度,当前速度等等一系列的监控指标
Gauge表示可变化的度量值,适用于CPU,内存使用率等
Histogram
Histogram柱状图这个比较直接,更多的是用于统计一些数据分布的情况,用于计算在一定范围的分布情况,同时还提供了度量指标值的总和.
Histogram对指标的范围性(区间)统计,比如内存在0%-30%, 30%-70%之间的采样次数
Histogram包含三个指标
<basename>: 度量值名称
<basename>_count: 样本反正总次数
<basename>_sum: 样本发生次数中值的总和
<basename>_bucket{le="+Inf"}: 每个区间的样本数
Summary
Summary摘要和Histogram柱状图比较类似,主要用于计算在一定时间窗口范围内度量指标对象的总数以及所有对量指标的总和.
和histogram类似,提供次数和总和,同时提供每个滑动窗口中的分位数.
Histogram和Summary的对比
序号 | histogram | Summary |
---|---|---|
配置 | 区间配置 | 分位数和滑动窗口 |
客户端性能 | 只需增加counters代价小 | 需要流式计算代价高 |
服务端性能 | 计算分位数消耗大,可能会耗时 | 无需计算,代价小 |
时序数量 | _sum、_count、bucket | _sum、_count、quantile |
分位数误差 | bucket的大小有关 | φ的配置有关 |
φ和滑动窗口 | Prometheus 表达式设置 | 客户端设置 |
聚合 | 根据表达式聚合 | 一般不可聚合 |
Prometheus中的Jobs和INSTANCES
Instances
// 仅采集的API endpoint
Jobs
// 相同目的的Instances
// 例如: 四个节点上的api-server
job: api-server
instance 1: 1.2.3.4:5670
instance 2: 1.2.3.4:5671
instance 3: 5.6.7.8:5670
instance 4: 5.6.7.8:5671
Prometheus拉去目标数据时, 会自动给目标的时序上增加一些标签,用于唯一标识,如果时序中本省已经包含,那么取决于honor_labels
// Job: 会增加Job名称
// instance: 增加host: port
开发一个简单Exporter
Prometheus为开发提供了客户端工具,用于为自己的中间件开发Exporter,对接Prometheus, 目前支持go,java,python,ruby
监听HTTP请求返回一行字符串
lexporter_request_count{user="admin"} 1000
package main
import (
"fmt"
"net/http"
)
func HelloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "lexporter_request_count{user=\"admin\"} 1000" )
}
func main () {
http.HandleFunc("/metrics", HelloHandler)
http.ListenAndServe(":8000", nil)
}
配置Prometheus,将exporter到Prometheus中
计数器
Counter 类型代表一种样本数据单调递增的指标,即只增不减,除非监控系统发生了重置。例如,你可以使用 counter 类型的指标来表示服务的请求数、已完成的任务数、错误发生的次数等。
我们对程序进行改造,累计计算count的数量
example1
package main
import (
"fmt"
"net/http"
)
type Counter struct {
count int64
}
func (c *Counter) Add(count int64) int64 {
c.count += count
return c.count
}
var counter = new(Counter)
func HelloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "lexporter_request_count{user=\"admin\"} %d",counter.Add(10) )
}
func main () {
http.HandleFunc("/metrics", HelloHandler)
http.ListenAndServe(":8000", nil)
}
example2
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
func main() {
// counter
requestTotal := prometheus.NewCounter(prometheus.CounterOpts{
Name: "request_total",
Help: "request total",
})
codeStatus := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "status_code_total",
Help: "status_code total",
},[]string{"code"})
requestTotal.Add(10)
codeStatus.WithLabelValues("200").Add(10)
codeStatus.WithLabelValues("500").Add(20)
codeStatus.WithLabelValues("404").Add(30)
prometheus.MustRegister(requestTotal)
prometheus.MustRegister(codeStatus)
// 暴露
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8888", nil)
}
Gauge(固定label和非固定label)
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
func main() {
// counter
// guage
// historgram
// summary
// metrics name(label=label_value) metrics_valu
// 有lable
// label/label_value 固定
// 无label(固定lable)
// lable/label_value 变化的
cpu := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu",
Help: "cpu total",
// 没有lable和固定lable一样的
ConstLabels: prometheus.Labels{"a":"xxx"},
})
// 非固定label
disk := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "disk",
Help: "disk total",
},[]string{"mount"})
cpu.Set(2222)
disk.WithLabelValues("/mnt/sda1:").Set(100)
disk.WithLabelValues("/mnt/sda2:").Set(200)
disk.WithLabelValues("/mnt/sda3:").Set(200)
disk.WithLabelValues("/mnt/sda4:").Set(200)
// 注册指标信息
prometheus.MustRegister(cpu)
prometheus.MustRegister(disk)
// 暴露
http.Handle("/metrics",promhttp.Handler())
http.ListenAndServe(":8888",nil)
}
historgram
example1
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
func main() {
// historgram
request_time := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "request_time",
Help: "request time",
},[]string{"url"})
prometheus.MustRegister(request_time)
request_time.WithLabelValues("/aaa").Observe(6)
// 暴露
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8888", nil)
}
summary
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
func main() {
// summary
requestsummary := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Name: "request_time_summary",
Help: "request time summary",
Objectives: map[float64]float64{0.5: 0.05,0.9:0.09},
},[]string{"url"})
prometheus.MustRegister(requestsummary)
requestsummary.WithLabelValues("/aaa").Observe(6)
requestsummary.WithLabelValues("/aaa").Observe(2)
// 暴露
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":8888", nil)
}
值的修改(事件触发或者时间触发)
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"math/rand"
"net/http"
"strconv"
"time"
)
func main() {
// 无label(固定lable)
// lable/label_value 变化的
cpu := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu",
Help: "cpu total",
// 没有lable和固定lable一样的
ConstLabels: prometheus.Labels{"a":"xxx"},
})
// 非固定label
disk := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "disk",
Help: "disk total",
},[]string{"mount"})
cpu.Set(2222)
disk.WithLabelValues("/mnt/sda1:").Set(100)
disk.WithLabelValues("/mnt/sda2:").Set(200)
disk.WithLabelValues("/mnt/sda3:").Set(200)
disk.WithLabelValues("/mnt/sda4:").Set(200)
codeStatus := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "status_code_total",
Help: "status_code total",
},[]string{"code"})
codeStatus.WithLabelValues("200").Add(10)
codeStatus.WithLabelValues("500").Add(20)
codeStatus.WithLabelValues("404").Add(30)
// counter
requestTotal := prometheus.NewCounter(prometheus.CounterOpts{
Name: "request_total",
Help: "request total",
})
requestTotal.Add(10)
// 值的修改
// 修改的时间 => 触发
// 时间触发
// 磁盘使用, cpu使用,内存使用
go func() {
for range time.Tick(time.Second) {
disk.WithLabelValues("/mnt/sda1").Set(float64(rand.Int()))
}
}()
// 事件触发,业务请求
http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){
requestTotal.Inc()
codeStatus.WithLabelValues(strconv.Itoa(rand.Intn(5) * 100)).Add(1)
fmt.Fprintf(w,"hi")
})
// 注册指标信息
prometheus.MustRegister(cpu)
prometheus.MustRegister(disk)
prometheus.MustRegister(requestTotal)
// 暴露
http.Handle("/metrics",promhttp.Handler())
http.ListenAndServe(":8888",nil)
}
Prometheus之Exporter开发的更多相关文章
- [转帖]prometheus数据采集exporter全家桶
prometheus数据采集exporter全家桶 Rainbowhhy1人评论2731人阅读2019-04-06 15:38:32 https://blog.51cto.com/13053917/2 ...
- 【开源监控】Prometheus+Node Exporter+Grafana监控linux服务器
Prometheus Prometheus介绍 Prometheus新一代开源监控解决方案.github地址 Prometheus主要功能 多维 数据模型(时序由 metric 名字和 k/v 的 l ...
- Prometheus + Node Exporter + Grafana 监控主机运行信息
上一篇文章中讲了如何利用Prometheus和Grafana监控SpringBoot应用的JVM信息,这次就来看看如何监控 服务器运行状态,先列出用到的工具: Prometheus node_ex ...
- Prometheus 自定义exporter 监控key
当Prometheus的node_exporter中没有我们需要的一些监控项时,就可以如zabbix一样定制一些key,让其支持我们所需要的监控项. 例如,我要根据 逻辑cpu核数 来确定load的告 ...
- 使用golang编写prometheus metrics exporter
metrcis输出 collector.go package main import ( "github.com/prometheus/client_golang/prometheus&qu ...
- 编写一个简单的基于jmespath 的prometheus exporter
目的很简单,因为系统好多监控指标是通过json 暴露的,并不是标准的prometheus metrics 格式,处理方法 实际上很简单,我们可以基于jsonpath 解析json数据,转换为prome ...
- 使用grok exporter 做为log 与prometheus 的桥
grok 是一个工具,可以用来解析非结构化的日志文件,可以使其结构化,同时方便查询,grok 被logstash 大量依赖 同时社区也提供了一个prometheus 的exporter 可以方便的进行 ...
- prometheus的agent 二次开发代码参考
import com.codahale.metrics.MetricRegistry;import io.prometheus.client.CollectorRegistry;import io.p ...
- Prometheus Operator 架构 - 每天5分钟玩转 Docker 容器技术(178)
本节讨论 Prometheus Operator 的架构.因为 Prometheus Operator 是基于 Prometheus 的,我们需要先了解一下 Prometheus. Prometheu ...
随机推荐
- python爬虫以及后端开发--实用加密模板整理
都是作者累积的,且看其珍惜,大家可以尽量可以保存一下,如果转载请写好出处https://www.cnblogs.com/pythonywy 一.md5加密 1.简介 这是一种使用非常广泛的加密方式,不 ...
- C++ Templates (2.1 类模板Stack的实现 Implementation of Class Template Stack)
返回完整目录 目录 2.1 类模板Stack的实现 Implementation of Class Template Stack 2.1.1 声明类模板 Declaration of Class Te ...
- 仿京东BOE官网 JavaScript代码
let items = document.getElementsByClassName('item'); let points = document.getElementsByClassName('p ...
- Docker-Docker与IPV6
公司计划在2020年前完成IPV6化改造,于是我先行查阅了一些资料了解Docker进行IPv6化的可能性. 预计明年正式开始测试. 方法一.使容器中的服务支持IPv6地址 不为容器中的服务特别分配IP ...
- muduo源码解析6-condtion类
condition class condition:noncopyable { }; 作用: 实现了最简单condtion操作,包括init,destroy,wait,notify,notifyAll ...
- 01 . etcd简介原理,应用场景及部署,简单使用
etcd简介 Etcd是CoreOS团队于2013年6月发起的开源项目,他的目标是构建一个高可用的分布式键值(key-value)数据库,etcd内部采用raft协议作为一致性算法,etcd基于Go语 ...
- 如何解决 iframe 无法触发 clickOutside
注:(1)非原创,来自https://blog.csdn.net/weixin_33985679/article/details/89699215.https://zhuanlan.zhihu.com ...
- win环境下安装配置openCV-4.3.0
win环境下安装openCV-4.3.0 首先下载 推荐国内镜像 官网太太太慢了 附上 下载地址 下载之后打开exe解压到目录都是常规操作 环境变量的配置 依次打开到系统变量的path 新建一个路径为 ...
- dlopen代码详解——从ELF格式到mmap
最近一个月的时间大部分在研究glibc中dlopen的代码,基本上对整个流程建立了一个基本的了解.由于网上相关资料比较少,走了不少弯路,故在此记录一二,希望后人能够站在我这个矮子的肩上做出精彩的成果. ...
- Android开发之viewpager导报错误解决方法:错误代码 Caused by: java.lang.ClassNotFoundException: Didn't find class
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 Caused by: java.lang.ClassNotFoundException: Didn't ...