李林超博客
首页
归档
留言
友链
动态
关于
归档
留言
友链
动态
关于
首页
工具
正文
Prometheus Exporter介绍
Leefs
2025-06-08 PM
1928℃
0条
[TOC] ### 一、概述 Prometheus Exporter是一个用来收集和暴露指标数据的工具,通过与Prometheus监控系统一起使用。 它的结构包括两个组件:**Collector**和**Exporter**: - **Collector**:用于从目标应用程序或系统收集指标并将其转化为Prometheus可识别的格式。收集器可以使用Prometheus客户端库来生成指标,并公开HTTP/metrics以便Prometheus Server进行定期调用和拉取指标。 - **Exporter**:它会从Collector获取指标数据,并将其转成为Prometheus可读格式。 Prometheus Server:它是一个用于存储和查询指标数据的中心化实践序列数据库服务器。Prometheus Server使用scrape_configs的YAML配置文件,其中指定了每个Exporter的配置信息,包括URL、抓取间隔等。 ### 二、**指标格式及其应用** Prometheus使用一种简单的文本格式来表示指标数据,即“度量名称{标签名="标签值", ...} 值 时间戳”这样的格式。 例如: ``` http_request_duration_seconds_bucket{le="0.2"} 150 http_request_duration_seconds_bucket{le="0.5"} 200 ``` 其中,”http_request_duration_seconds_bucket”是度量名称,”le”是标签名,”0.2”、”0.5”是标签值, 150、200是相应的值,时间戳则通常省略不写。 Prometheus指标格式的应用包括: 1.**收集指标数据**:在Prometheus Exporter中,我们通过Collector来收集指标数据,并将其转换为合适的指标格式。 2.**暴露指标数据**:Prometheus Exporter会把采集到的指标数据暴露给Prometheus,使得Prometheus能够对其进行监控和分析。 3.**查询指标数据**:在Prometheus中,我们可以使用PromQL查询语言对指标数据查询和分析,比如计算指标的平均值、最大值、最小值等等。 4.**可视化指标**:通过Grafana等可视化工具,我们可以将Prometheus收集到的指标数据进行图表展示和监控报警等操作。 总之,Prometheus指标格式是Prometheus监控系统中非常重要的一个概念,它是实现收集、查询、暴露、查询和可视化数据的基础。 ### 三、Prometheus四种指标类型 Prometheus定义了四种主要的指标类型: - **Counter(计数器)**:用于表示单调递增的指标,例如请求数等。Counter在每次观测时会增加它所代表的值(通常是一个整数),但不会减少或者重置。 - **Gauge(仪表盘)**:用于表示可变化的度量值,例如CPU利用率、内存用量等。Gauge可以增加、减少或重置,代表着当前的状态。 - **Histogram(直方图)**:用于表示数据样本的分布情况,例如请求延迟等。Histogram将数据按照桶(bucket)进行划分,并对每个桶内的样本计算出一些统计信息,如样本数量、总和、平均值等。 - **Summary(摘要)**:类似于Histogram,也用于表示数据样本的分布情况,但同时展示更多的统计信息,如样本数量、总和、平均值、上分位数、下分位数等。 这些指标类型可以组合使用,以便更好的描述系统运行状态和性能指标。例如使用Counter来记录某个API的请求数,使用Gauge来记录内存使用量,使用Histogram来记录请求延迟分布情况。使用Summary来记录响应时间分布情况。在实际应用中应根据需要选择合适的指标类型来表示监控数据。 ### 四、指标使用示例 #### 4.1 Exporter Counter 示例 ```go import ( "net/http" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { // 创建一个Counter指标 counterMetric := prometheus.NewCounter(prometheus.CounterOpts{ Name: "example_counter", // 指标名称 Help: "An example counter metric.", // 指标帮助信息 }) // 注册指标 prometheus.MustRegister(counterMetric) // 增加指标值 counterMetric.Inc() // 创建一个HTTP处理器来暴露指标 http.Handle("/metrics", promhttp.Handler()) // 启动Web服务器 http.ListenAndServe(":8080", nil) } ``` #### 4.2 Exporter Gauge示例 ```go import ( "net/http" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { // 创建一个Gauge指标 guageMetric := prometheus.NewGauge(prometheus.GaugeOpts{ Name: "example_gauge", // 指标名称 Help: "An example gauge metric.", // 指标帮助信息 }) // 注册指标 prometheus.MustRegister(guageMetric) // 设置指标值 guageMetric.Set(100) // 创建一个HTTP处理器来暴露指标 http.Handle("/metrics", promhttp.Handler()) // 启动Web服务器 http.ListenAndServe(":8080", nil) } ``` #### 4.3 Exporter Histogram示例 ```go import ( "math/rand" "net/http" "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { // 创建一个Histogram指标 histogramMetric := prometheus.NewHistogram(prometheus.HistogramOpts{ Name: "example_histogram", // 指标名称 Help: "An example histogram metric.", // 指标帮助信息 Buckets: prometheus.LinearBuckets(0, 10, 10), // 设置桶宽度 }) // 注册指标 prometheus.MustRegister(histogramMetric) // 定期更新指标值 go func() { for { time.Sleep(time.Second) histogramMetric.Observe(rand.Float64() * 100) } }() // 创建一个HTTP处理器来暴露指标 http.Handle("/metrics", promhttp.Handler()) // 启动Web服务器 http.ListenAndServe(":8080", nil) } ``` #### 4.4 Exporter Summary示例 ```go import ( "math/rand" "net/http" "time" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" ) func main() { // 创建一个Summary指标 summaryMetric := prometheus.NewSummary(prometheus.SummaryOpts{ Name: "example_summary", // 指标名称 Help: "An example summary metric.", // 指标帮助信息 Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, // 设置分位数和偏差 }) // 注册指标 prometheus.MustRegister(summaryMetric) // 定期更新指标值 go func() { for { time.Sleep(time.Second) summaryMetric.Observe(rand.Float64() * 100) } }() // 创建一个HTTP处理器来暴露指标 http.Handle("/metrics", promhttp.Handler()) // 启动Web服务器 http.ListenAndServe(":8080", nil) } ``` *附参考原文链接地址* *https://www.cnblogs.com/0x00000/p/17557743.html*
标签:
Prometheus
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:
https://www.lilinchao.com/archives/2991.html
上一篇
Prometheus简介
下一篇
没有了
取消回复
评论啦~
提交评论
栏目分类
随笔
2
Java
326
大数据
229
工具
35
其它
25
GO
48
NLP
8
标签云
Netty
FileBeat
人工智能
Golang基础
Jquery
ajax
FastDFS
Linux
GET和POST
VUE
nginx
Hive
Spark Streaming
国产数据库改造
SQL练习题
Spark SQL
Spark
前端
Prometheus
Stream流
Livy
线程池
HDFS
Java工具类
JavaSE
MyBatis
Flink
Java阻塞队列
机器学习
Jenkins
友情链接
申请
范明明
庄严博客
Mx
陶小桃Blog
虫洞