当前位置:网站首页>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();
}
}
}
参考文献:
边栏推荐
- P3052 [USACO12MAR]Cows in a Skyscraper G
- Number array de duplication in JS
- STEP7 master station and remote i/o networking_ Old bear passing by_ Sina blog
- 用frp搭建云电脑
- Studio5k V28 installation and cracking_ Old bear passing by_ Sina blog
- Stop eating vitamin C tablets. These six fruits have the highest vitamin C content
- Apache doris1.0 cluster setup, load balancing and parameter tuning
- Transformation of communication protocol between Siemens S7-200PLC and Danfoss inverter_ Old bear passing by_ Sina blog
- Reading notes on how to connect the network - hubs, routers and routers (III)
- Use Baidu map API to set an overlay (infowindow) in the map to customize the window content
猜你喜欢

Redis之内存淘汰机制
![寻找翻转数组的最小值[抽象二分]](/img/b9/1e0c6196e6dc51ae2c48f6c5e83289.png)
寻找翻转数组的最小值[抽象二分]

ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided解决

final和static

Recommended system design

Establishment of multiple background blocks in botu software_ Old bear passing by_ Sina blog

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

Line height for small use

MySQL version upgrade + data migration

How postman tests interfaces that require login
随机推荐
Common problems encountered when creating and publishing packages using NPM
懒人教你用猕猴桃一月饱减16斤_过路老熊_新浪博客
树莓派开机发送热点进行远程登录
在step7中实现模拟量数值与工程量数值之间的转换_过路老熊_新浪博客
Establishment of multiple background blocks in botu software_ Old bear passing by_ Sina blog
About Simple Data Visualization
Talk about how to hot restart a spoole or PHP cli process
huibian
oracle写一个先插入一条数据,在将该数据中一个字段更新的触发器的坑
Object类常用方法
Rocket之消息存储
The InputStream stream has been closed, but the file or folder cannot be deleted, indicating that it is occupied by the JVM
How to configure SQL Server 2008 Manager_ Old bear passing by_ Sina blog
Understanding of pseudo classes
(转载)进程和线程的形象解释
对象数组去重
Two ways to center block level elements
西门子S7-200PLC和丹佛斯变频器的通讯协议改造_过路老熊_新浪博客
Redis之跳跃表
Joint simulation of STEP7 and WinCC_ Old bear passing by_ Sina blog