前言
入门篇:从零开始:Prometheus
理解篇:进阶之路:Prometheus —— 理解篇
本文记录了一些我在使用Prometheus的过程中的技巧,以后还会随着使用的深入不定期更新,需要的小伙伴可以订阅收藏,希望可以让正在摸索的你们少走一些弯路。
对于刚开始使用Prometheus的用户可以从入门篇获取到一些基本的概念。
技巧
1.自定义监控指标
//自定义counter
static final Counter PARAM1 = Counter.build()
.name("param_1") //定义名称,名称可用于搜索
.help("this is a counter.") //定义描述
.register();
//自定义gauge
static final Gauge PARAM2 = Gauge.build()
.name("param_2")
.help("this is a gauge.")
.register();
param1.inc(); //当前属性加1
param2.dec(); //当前属性减1
param2.set(10); //设置当前属性
- Counter和Gauge表示类型;
- 必须加上static final关键字,否则编译不能通过;
2.带label的监控指标
private static final Gauge PARAM3 = Gauge.build()
.name("param_3")
.labelNames("label1", "label2", "label3") //定义标签名称,可定义多个
.help("this is a gauge with labels")
.register();
PARAM3.labels("value1","value2","value3").set(1100); //设置标签值,标签可用于条件筛选
3.动态添加服务到Prometheus监控列表中
基本思路:
- 新增/删除服务并在zookeeper中添加节点,traefik可通过配置zookeeper连接实现自动注册,修改Prometheus的服务(以下简称服务A)检测到zookeeper节点变化后执行第 2 步;
- 服务A对比traefik和prometheus.yml中的服务列表,筛选出新增/删除的服务;
- 按照格式修改prometheus.yml文件并重新加载Prometheus。
pom.xml
//有关于zookeeper和traefik的配置在此不做展开,此处仅列出需要的依赖
<!-- zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<!-- prometheus -->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.3.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>0.3.0</version>
</dependency>
关键代码
//加载zookeeper的CuratorFramework框架
@Autowired
private CuratorFramework client;
//触发traefik注册服务
client.transactionOp().create().forPath("/traefik/frontends/path");
client.transactionOp().create().forPath("/traefik/backends/path");
//服务A处理prometheus.xml
PathChildrenCache cache = new PathChildrenCache(client, "/traefik/backends", true);
cache.getListenable().addListener((client, event) -> {
switch (event.getType()) {
case CHILD_ADDED:
//TODO 找到traefik中有,Prometheus中没有的项目(新增项目)
//TODO 向prometheus.xml文件中写入项目信息
case CHILD_REMOVED:
//TODO 找到traefik中没有,Prometheus中有的项目(删除项目)
//TODO 删除prometheus.xml文件中对应项目信息
}
//触发Prometheus重新加载
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForLocation("http://127.0.0.1:9090/-/reload", null);
}
向prometheus.xml写入项目的格式
##### START #####
- job_name: 'service'
metrics_path: /metrics
dns_sd_configs:
- names:
- 'service'
refresh_interval: 15s
type: A
port: 9000
##### END #####
快速获取prometheus.xml中所有job_name的正则表达式(Java)
(?<=job_name:\\s')\w+(?='\\s)
要点:
- traefik是一个负载均衡工具,如果没有相关需求可以去掉,更加简单粗暴^_^;
- 修改prometheus.xml文件后一定要触发重新加载才能看到效果;
- prometheus.xml的写入格式可能会根据需求不同有所变化,具体请查看官方文档。
文章内容是根据参考+实践理解所得,如果有错误的地方欢迎指正!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)