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 ...
随机推荐
- 免费验证码接收网站&不停开小号方法
手机号注册:免费验证码接收网站 0.http://getfreesmsnumber.com/ 9个外国网站,访问后要快点点击链接,否则2秒会检测出adblocker 1.http://smsrecei ...
- 致敬学长!J20航模遥控器开源项目计划【开局篇】 | 先做一个开机界面 | MATLAB图像二值化 | Img2Lcd图片取模 | OLED显示图片
我们的开源宗旨:自由 协调 开放 合作 共享 拥抱开源,丰富国内开源生态,开展多人运动,欢迎加入我们哈~ 和一群志同道合的人,做自己所热爱的事! 项目开源地址:https://github.com/C ...
- Vscode配置C++环境
(终于申请博客了qaq) 之前用了那么久Dev-C++,总算换了一个编辑器,Visual Studio Code (Vscode). 界面可比以前的舒适多了. Vscode作为一款功能极其丰富的开发工 ...
- Linux 资源监控与性能测试
综合管理 glances 系统情况监控 vmstat 能看到上下文切换,runnable进程个数,uninterrupted进程个数 磁盘IO iostat是磁盘级别监控,iotop进程级别监控,注意 ...
- kernel 通知链
原文链接: 深入理解linux网络技术内幕读书笔记(四)--通知链 概述 [注意] 通知链只在内核子系统之间使用. 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生的事件感兴趣.为了 ...
- Go interface 操作示例
原文链接:Go interface操作示例 特点: 1. interface 是一种类型 interface 是一种具有一组方法的类型,这些方法定义了 interface 的行为.go 允许不带任何方 ...
- 问卷星导入数据到SPSS,数据变成-3是什么原因?
问卷星将数字“-3”表示为筛选或者跳转题:如果问卷中有设计筛选或者跳转,此时则会出现“-3”这个数字. 解决办法1 分析时首先进行筛选,然后再分析,便不会出现“-3”,而且一定需要这样进行.“筛选样本 ...
- springcloud高级
第一章 负载均衡 Ribbon (Spring Cloud 高级) 一. Ribbon 在微服务中的作用 1 什么是 Ribbon 1.Ribbon 是一个基于 Http 和 TCP ...
- laravel中elastisearch安装和测试运行是否成功(注意是windows下的操作)
1.去elasticsearch官网下载,如果太慢可以在我上一个随笔看下载地址 2.下载完解压缩,在cmd中找到到elasticsearch的bin目录下执行.\elasticsearch.bat - ...
- js 数组与字符串互相转换
1.数组转字符串 arr.join() 2.字符串转数组 str.split(',')