Prometheus blackbox 监控接口返回的字符串

​ 在做接口监控时发现,即使是接口已经不可用了,但是由于 curl 这个接口返回的状态码还是 200,所以未发出告警。现在改为去监控接口返回的字符串。

如下图所示:

image

当接口不可用时,返回的字符串为 ["message":"出现异常”,code:500) ,而状态码还是 200,这个时候监控状态码已经失去意义了。


这里使用 blackbox 里的 fail_if_body_not_matches_regexp 去实现该步骤。
官方链接:https://github.com/prometheus/blackbox_exporter/blob/master/example.yml

  • fail_if_body_not_matches_regexp 如果不匹配该字符串则探测失败

  • fail_if_body_matches_regexp 如果匹配该字符串则探测失败

浏览器打开接口地址,可以看到如下内容:

{"code":200,"message":"调用成功","data":null}

首先在 blackbox.yml 定义该模块:

modules:
  http_2xx_localhost:
    prober: http
    http:
      fail_if_body_not_matches_regexp:
      - "200,"

prometheus.yml 添加如下内容:

- job_name: "本地接口"
    metrics_path: /probe
    params:
      module: [http_2xx_localhost]
    file_sd_configs:
    - files:
      - xxx.io/xxxxx
      refresh_interval: 15s

    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: localhost:9115

测试:

curl 'http://localhost:39115/probe?module=http_2xx_localhost&target=https%3A%2F%2Fxxx.io%2Fxxx'

可以看到返回结果是 1image

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海口-熟练工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值