进阶之路:Prometheus —— 技巧篇

前言

入门篇:从零开始: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监控列表中

基本思路:

  1. 新增/删除服务并在zookeeper中添加节点,traefik可通过配置zookeeper连接实现自动注册,修改Prometheus的服务(以下简称服务A)检测到zookeeper节点变化后执行第 2 步;
  2. 服务A对比traefik和prometheus.yml中的服务列表,筛选出新增/删除的服务;
  3. 按照格式修改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)

要点:

  1. traefik是一个负载均衡工具,如果没有相关需求可以去掉,更加简单粗暴^_^;
  2. 修改prometheus.xml文件后一定要触发重新加载才能看到效果;
  3. prometheus.xml的写入格式可能会根据需求不同有所变化,具体请查看官方文档

 

文章内容是根据参考+实践理解所得,如果有错误的地方欢迎指正!

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值