当前位置:网站首页>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();
}
}
}
参考文献:
边栏推荐
- STEP7主站与远程I/O组网_过路老熊_新浪博客
- mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动
- Using Google protobuf protocol environment configuration in PHP
- Recommended system design
- 文献调研(二):基于短期能源预测的建筑节能性能定量评估
- Transformation of communication protocol between Siemens S7-200PLC and Danfoss inverter_ Old bear passing by_ Sina blog
- Common problems encountered when creating and publishing packages using NPM
- Circuit de fabrication manuelle d'un port série de niveau USB à TTL pour PL - 2303hx Old bear passing Sina blog
- Problems encountered in Doris operation and maintenance
- Some common operation methods of array
猜你喜欢

Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or

How postman tests interfaces that require login

社交网络可视化第三方库igraph的安装

兆欧表电压档位选择_过路老熊_新浪博客

推荐系统设计

STEP7 master station and remote i/o networking_ Old bear passing by_ Sina blog

dhcp复习

Thrift入门学习

使用npm创建并发布包时遇到的常见问题

懒人教你用猕猴桃一月饱减16斤_过路老熊_新浪博客
随机推荐
西门子S7-200PLC和丹佛斯变频器的通讯协议改造_过路老熊_新浪博客
Lazy people teach you to use kiwi fruit to lose 16 kg in a month_ Old bear passing by_ Sina blog
Let's talk about string today
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
Understanding of pseudo classes
Transformation of communication protocol between Siemens S7-200PLC and Danfoss inverter_ Old bear passing by_ Sina blog
[wechat official account H5] generates a QR code with parameters to enter the official account attention page to listen to user-defined menu bar for official account events (server)
Object类常用方法
Sword finger offer 48 Longest substring without duplicate characters
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
Efficacy of kiwi fruit enzyme_ Old bear passing by_ Sina blog
Unable to start debugging. Unexpected GDB output from command “-environment -cd xxx“ No such file or
One article explains R & D efficiency! Your concerns are
Stream in PHP socket communication_ Understanding of select method
用ES5的方式实现const
[reprint]rslogix 5000 instance tutorial
在step7中实现模拟量数值与工程量数值之间的转换_过路老熊_新浪博客
Final and static
说说单例模式!
Connecting MySQL database with VBScript_ Old bear passing by_ Sina blog