Prometheus开发中间件Exporter过程详解
作者:-零 发布时间:2023-04-18 16:14:13
标签:Prometheus,开发,中间件,Exporter
Prometheus 为开发这提供了客户端工具,用于为自己的中间件开发Exporter,对接Prometheus 。
目前支持的客户端
Go
Java
Python
Ruby
以go为例开发自己的Exporter
依赖包的引入
工程结构
[root@node1 data]# tree exporter/
exporter/
├── collector
│ └── node.go
├── go.mod
└── main.go
引入依赖包
require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/prometheus/client_golang v1.1.0
//借助gopsutil 采集主机指标
github.com/shirou/gopsutil v0.0.0-20190731134726-d80c43f9c984
)
main.go
package main
import (
"cloud.io/exporter/collector"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
)
func init() {
//注册自身采集器
prometheus.MustRegister(collector.NewNodeCollector())
}
func main() {
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Printf("Error occur when start server %v", err)
}
}
为了能看清结果我将默认采集器注释,位置registry.go
func init() {
//MustRegister(NewProcessCollector(ProcessCollectorOpts{}))
//MustRegister(NewGoCollector())
}
/collector/node.go
代码中涵盖了Counter、Gauge、Histogram、Summary四种情况,一起混合使用的情况,具体的说明见一下代码中。
package collector
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"runtime"
"sync"
)
var reqCount int32
var hostname string
type NodeCollector struct {
requestDesc *prometheus.Desc //Counter
nodeMetrics nodeStatsMetrics //混合方式
goroutinesDesc *prometheus.Desc //Gauge
threadsDesc *prometheus.Desc //Gauge
summaryDesc *prometheus.Desc //summary
histogramDesc *prometheus.Desc //histogram
mutex sync.Mutex
}
//混合方式数据结构
type nodeStatsMetrics []struct {
desc *prometheus.Desc
eval func(*mem.VirtualMemoryStat) float64
valType prometheus.ValueType
}
//初始化采集器
func NewNodeCollector() prometheus.Collector {
host,_:= host.Info()
hostname = host.Hostname
return &NodeCollector{
requestDesc: prometheus.NewDesc(
"total_request_count",
"请求数",
[]string{"DYNAMIC_HOST_NAME"}, //动态标签名称
prometheus.Labels{"STATIC_LABEL1":"静态值可以放在这里","HOST_NAME":hostname}),
nodeMetrics: nodeStatsMetrics{
{
desc: prometheus.NewDesc(
"total_mem",
"内存总量",
nil, nil),
valType: prometheus.GaugeValue,
eval: func(ms *mem.VirtualMemoryStat) float64 { return float64(ms.Total) / 1e9 },
},
{
desc: prometheus.NewDesc(
"free_mem",
"内存空闲",
nil, nil),
valType: prometheus.GaugeValue,
eval: func(ms *mem.VirtualMemoryStat) float64 { return float64(ms.Free) / 1e9 },
},
},
goroutinesDesc:prometheus.NewDesc(
"goroutines_num",
"协程数.",
nil, nil),
threadsDesc: prometheus.NewDesc(
"threads_num",
"线程数",
nil, nil),
summaryDesc: prometheus.NewDesc(
"summary_http_request_duration_seconds",
"summary类型",
[]string{"code", "method"},
prometheus.Labels{"owner": "example"},
),
histogramDesc: prometheus.NewDesc(
"histogram_http_request_duration_seconds",
"histogram类型",
[]string{"code", "method"},
prometheus.Labels{"owner": "example"},
),
}
}
// Describe returns all descriptions of the collector.
//实现采集器Describe接口
func (n *NodeCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- n.requestDesc
for _, metric := range n.nodeMetrics {
ch <- metric.desc
}
ch <- n.goroutinesDesc
ch <- n.threadsDesc
ch <- n.summaryDesc
ch <- n.histogramDesc
}
// Collect returns the current state of all metrics of the collector.
//实现采集器Collect接口,真正采集动作
func (n *NodeCollector) Collect(ch chan<- prometheus.Metric) {
n.mutex.Lock()
ch <- prometheus.MustNewConstMetric(n.requestDesc,prometheus.CounterValue,0,hostname)
vm, _ := mem.VirtualMemory()
for _, metric := range n.nodeMetrics {
ch <- prometheus.MustNewConstMetric(metric.desc, metric.valType, metric.eval(vm))
}
ch <- prometheus.MustNewConstMetric(n.goroutinesDesc, prometheus.GaugeValue, float64(runtime.NumGoroutine()))
num, _ := runtime.ThreadCreateProfile(nil)
ch <- prometheus.MustNewConstMetric(n.threadsDesc, prometheus.GaugeValue, float64(num))
//模拟数据
ch <- prometheus.MustNewConstSummary(
n.summaryDesc,
4711, 403.34,
map[float64]float64{0.5: 42.3, 0.9: 323.3},
"200", "get",
)
//模拟数据
ch <- prometheus.MustNewConstHistogram(
n.histogramDesc,
4711, 403.34,
map[float64]uint64{25: 121, 50: 2403, 100: 3221, 200: 4233},
"200", "get",
)
n.mutex.Unlock()
}
执行的结果http://127.0.0.1:8080/metrics
来源:https://www.cnblogs.com/-wenli/p/13424790.html


猜你喜欢
- 本文实例为大家分享了python3音乐播放器的关键代码,供大家参考,具体内容如下from tkinter import *from trac
- 一、 了解postman1. 什么是postman?------ 软件测试用来做接口测试的工具。2. 如何下载postman--
- 下表列出 SQL Server 查询分析器提供的所有键盘快捷方式。活动 快捷方式 书签:清除所有书签。 CTRL-SHIFT-F2
- 1.游戏背景介绍(写在前面的废话): 五月初的某天,看到某网推荐了这款游戏,Pongo,看着还不错的样子
- 目录一、Python GUI 编程简介二、流行GUI框架总结三、代码演示四、界面一、Python GUI 编程简介Tkinter 模块(Tk
- 记录mysql5.7.14安装与配置过程,梳理成文,希望对大家有所帮助。1.配置文档: ####################配
- 关于端口复用一个套接字不能同时绑定多个端口,如果客户端想绑定端口号,一定要调用发送信息函数之前绑定( bind )端口,因为在发送信息函数(
- 一、软件下载MySQL下载安装:官网下载地址:https://www.mysql.com/或者本地下载二、安装须知如果是安装过该软件的卸载重
- 简介zhdate模块统计从1900年到2100年的农历月份数据代码,支持农历和公历之间的转化,并且支持日期差额运算。安装pip instal
- 最近使用Python 3.5写了一个GUI小程序,于是想将该写好的程序发布成一个exe文件,供自己单独使用。至于通过安装的方式使用该程序,我
- 实现的思路:将准备好的图片通过opencv读取出来,并将其设置好帧数等参数后合成为无声视频。最后通过moviepy编辑视频将背景音乐加入到视
- 做数据分析、科学计算等离不开工具、语言的使用,目前最流行的数据语言,无非是MATLAB,R语言,Python这三种语言,但今天小编简单总结了
- 常用功能 mean(data)mean(data)用于求给定序列或者迭代器的算术平均数。import statisticsexample_l
- match()方法用于从字符串中查找指定的值本方法类似于indexOf()和lastindexOf(),不同的是它返回的是指定的值,而不是指
- 一、BN(Batch Normalization)算法1. 对数据进行归一化处理的重要性神经网络学习过程的本质就是学习数据分布,在训练数据与
- eval()函数可以将字符串型的list、tuple、dict等等转换为原有的数据类型即使用eval可以实现从元组,列表,字典型的字符串到元
- 我就废话不多说了,大家还是直接看代码吧~func main() { var a chan string a =mak
- 项目结构:运行效果:=========================================================代码部
- 最近在做微信支付,调用微信的统一下单支付接口http://mch.weixin.qq.com/wiki/doc/api/jsapi.php?
- 1.参考Beautiful Soup and Unicode Problems详细解释unicodedata.normalize('