当前位置:网站首页>14.1.1、Promethues监控,四种数据类型metrics,Pushgateway
14.1.1、Promethues监控,四种数据类型metrics,Pushgateway
2022-06-25 22:12:00 【Loves_dccBigData】
1、4种常用Metrics
1)Counter
连续增加不会减少的计数器,可以用于记录只增不减的类型,例如:网站访问人数,系统运行时间等。
对于Counter类型的指标,只包含一个inc()的方法,就是用于计数器+1.
一般而言,Counter类型的metric指标在冥冥中我们使用_total结束,如http_requests_total.
2)Gauge
可增可减的仪表盘,曲线图
对于这类可增可减的指标,用于反应应用的当前状态。
例如在监控主机时,主机当前空闲的内存大小,可用内存大小等等。
对于Gauge指标的对象则包含两个主要的方法inc()和dec(),用于增加和减少计数。
3)Histogram
主要用来统计数据的分布情况,这是一种特殊的metrics数据类型,代表的是一种近似的百分比估算数值,统计所有离散的指标数据在各个取值区段内的次数。例如:我们想统计一段时间内http请求响应小于0.005秒、小于0.01秒、小于0.025秒的数据分布情况。那么使用Histogram采集每一次http请求的时间,同时设置bucket。
4)Summary
Summary和Histogram非常相似,都可以统计事件发生的次数或者大小,以及其分布情况,他们都提供了对时间的计数_count以及值的汇总_sum,也都提供了可以计算统计样本分布情况的功能,不同之处在于Histogram可以通过histogram_quantile函数在服务器计算分位数。而Sumamry的分位数则是直接在客户端进行定义的。因此对于分位数的计算,Summary在通过PromQL进行查询的时候有更好的性能表现,而Histogram则会消耗更多的资源,但是相对于客户端而言Histogram消耗的资源就更少。用哪个都行,根据实际场景自由调整即可。
2、基于SpringBoot写一个简单的exporter
1)pom.xml配置如下
4个prometheus的依赖包和两个pushgateway的依赖包
<!--普罗米修斯依赖-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.4.0</version>
</dependency>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2)Application类注解
添加@EnableScheduling注解主要是添加了定时任务,用于动态模拟数据的变化,后面会看到应用的地方
@SpringBootApplication
@EnableScheduling
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
public class ExporterDemoApplication{
public static void main(String[] args) {
SpringApplication.run(ExporterDemoApplication.class, args);
}
}
3)CounterTest
定义一个Counter类型的metrics,一般而言,Counter类型的metrics指标在命名中我们使用_total结束。
@RestController
public class CounterTest {
/* * 使用Counter.build()创建Counter类型的监控指标,并且通过name()方法定义监控指标的名称network_traffic_input * ,通过labelNames()定义该指标包含的标签。最后通过register()将该指标注册到Collector的defaultRegistry中 */
static final Counter counterDemo = Counter.build()
.name("counterChanger_job").labelNames("ddd","ccc","unit")
.help("Counter 实例").register();
//指标埋点,定时器会造成普罗米修斯与本地的数据时间戳不同步,尽量不要使用这种方式,实例中的定时器是为了数据演示
@Scheduled(cron="0/5 * * * * ?")
@RequestMapping("/changeCounter")
public void changeCounter(){
counterDemo.labels("标签1","标签2","seconds").inc();//指标值增加
}
}
这里每5秒会自动执行changeCounter用于模拟数据的变化
4)GaugeTest
@RestController
public class GaugeTest {
/**指标注册 * name设置指标名 * labelNames设置各项指标名称 * help设置指标描述 */
static final Gauge gaugeDemo = Gauge.build()
.name("gaugeDemo")
.labelNames("label1","label2","label3","label4","label5")
.help("gauge 实例").register();
//指标埋点
@Scheduled(cron="0/5 * * * * ?")
@RequestMapping("/changeGauge")
public void changeGauge() {
gaugeDemo.labels("1","2","3","4","5").inc(); //指标值加1
gaugeDemo.labels("1","2","3","4","5").dec(); //指标值减一
gaugeDemo.labels("1","2","3","4","5").set(19.00); //指标值直接赋值
}
}
Gauge类型的metrics可以对数据进行增加、减小和直接赋值。这种类型在实际应用中比较多
5)HistogramTest
@RestController
public class HistogramTest
{
/** * 注册 * 注册时buckets()设置区间值,如下设置了100、200、300三个区间值 */
static final Histogram histogramDemo = Histogram.build()
.labelNames("label1", "label2", "label3", "label4", "label5")
.name("histogramDemo")
.buckets(100, 200, 300)
.help("Histogram 实例")
.register();
//指标埋点
@Scheduled(cron = "0/5 * * * * ?")
public void changeHistogram()
{
/** * 本次执行的指标值 * 如下设置为150,则每次执行,小于200区间以及小于300区间加1,小于100区间不变 */
histogramDemo.labels("1", "2", "3", "4", "5").observe(150);
}
}
6)SummaryTest
@RestController
public class SummaryTest {
//注册
static final Summary summaryDemo = Summary.build()
.quantile(0.5, 0.01) // 添加50%分位数,允许有5%的误差,相当于求中位数
.quantile(0.9, 0.01) // 添加90%分位数,允许有1%的误差
.name("summaryDemo").labelNames("label1","label2","label3","label4","label5")
.help("Summary 实例").register();
//指标埋点
@Scheduled(cron="0/5 * * * * ?")
public void changeSummary(){
summaryDemo.labels("1","2","3","4","5").observe(1);
}
}
在prometheus的Graph中通过查看summaryDemo、summaryDemo_count、summaryDemo_sum查看对应结果
7)pushgateway
@Component
public class PrometheusConfig
{
public static final Counter counterDemo = Counter.build()
.name("push_way_counter")
.labelNames("wy","zxjr","ocs","xxjf","unit","instance")
.help("Counter 实例")
.register();
//测试发送
public static void main(String[] args)
{
PushGateway prometheusPush = new PushGateway("localhost:9091");
try
{
for(int i=0;i<50;i++){
counterDemo.labels("网元","在线接入","OCS","消息计费","byte","localhsot:9091").inc();
prometheusPush.push(counterDemo,"sp-getway");
Thread.sleep(5000);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
参考文献:
边栏推荐
猜你喜欢

Reading notes on how to connect the network - hubs, routers and routers (III)

Simulation connection between WinCC and STEP7_ Old bear passing by_ Sina blog

Building cloud computers with FRP

文獻調研(三):數據驅動的建築能耗預測模型綜述
![Bit Compressor [蓝桥杯题目训练]](/img/d5/231d20bf4104cc2619b2a4f19b605c.png)
Bit Compressor [蓝桥杯题目训练]

(转载)进程和线程的形象解释

懒人教你用猕猴桃一月饱减16斤_过路老熊_新浪博客

文献调研(二):基于短期能源预测的建筑节能性能定量评估

今天说说String相关知识点

手工制作 pl-2303hx 的USB轉TTL電平串口的電路_過路老熊_新浪博客
随机推荐
常用的几款富文本编辑器
(转载)进程和线程的形象解释
Given the parameter n, there will be n integers 1, 2, 3,... From 1 to n, n. These n arrays have n! An arrangement that lists all columns in ascending order of size and marks them one by one. Given n a
使用npm创建并发布包时遇到的常见问题
oracle写一个先插入一条数据,在将该数据中一个字段更新的触发器的坑
树莓派开机发送热点进行远程登录
Unsigned and signed vernacular
static关键字详解
记录一些cf的题
我的博客今天2岁167天了,我领取了先锋博主徽章_过路老熊_新浪博客
Can I upload pictures without deploying the server?
Topic36——53. 最大子数组和
Jenkins releases PHP project code
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
关于运行scrapy项目时提示 ModuleNotFoundError: No module named 'pymongo‘的解决方案
ssh的复习
Using swiper to realize the rotation chart
西门子S7-200PLC和丹佛斯变频器的通讯协议改造_过路老熊_新浪博客
Rocket之消息存储
Building cloud computers with FRP