0
点赞
收藏
分享

微信扫一扫

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

Prometheus blackbox 监控接口返回值

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

如下图所示:

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

当接口不可用时,返回的字符串为 ["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'

可以看到返回结果是 1

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



举报

相关推荐

0 条评论