You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Prometheus中配置GET请求实现第三方API状态监控

基于Prometheus + Grafana 监控GET接口的实现方案

整体方案采用Prometheus官方的blackbox_exporter作为探测组件,主动发起GET请求访问目标接口,采集接口可用性、响应状态、耗时等指标后存入Prometheus,最终在Grafana中可视化展示和配置告警。

1. 部署并配置blackbox_exporter

blackbox_exporter是专门用于HTTP/HTTPS、TCP、ICMP等端点探测的指标导出组件,配置方式如下:

  • 下载对应系统版本的blackbox_exporter二进制包,解压后进入目录
  • 修改配置文件blackbox.yml,参考配置如下:
modules:
  http_2xx:
    prober: http
    timeout: 5s
    http:
      method: GET
      # 按接口实际正常返回的状态码调整,默认200-299都生效
      valid_status_codes: []
      headers:
        mobile: "你的实际mobile值" # 替换为接口要求的请求头mobile参数
        authkey: "你的实际Authkey" # 替换为接口要求的请求头authkey参数
      tls_config:
        # 若接口使用自签名证书,可设置为true跳过证书校验
        insecure_skip_verify: false
  • 启动服务:./blackbox_exporter --config.file=blackbox.yml,默认监听端口为9115。

2. 配置Prometheus采集规则

修改Prometheus配置文件prometheus.yml,新增探测任务的采集配置:

scrape_configs:
  - job_name: 'otp-api-monitor'
    metrics_path: /probe
    params:
      module: [http_2xx]
    static_configs:
      - targets:
        # 替换为实际的接口地址,请求头参数已在blackbox配置,无需放在URL中
        - 'https://example.com/api/v5/otp?template_id=application/json'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        # 替换为实际部署blackbox_exporter的服务器IP和端口
        replacement: 127.0.0.1:9115

配置修改完成后,执行热加载生效:curl -X POST http://你的Prometheus地址:9090/-/reload。

3. 验证采集指标

Prometheus配置生效后,可在Prometheus UI的查询页验证指标是否正常采集:

  • probe_success{job="otp-api-monitor"}:值为1表示接口探测成功,0表示探测失败
  • probe_http_status_code{job="otp-api-monitor"}:返回接口响应的HTTP状态码
  • probe_duration_seconds{job="otp-api-monitor"}:返回接口完整请求耗时

4. Grafana可视化配置

确保Grafana已添加Prometheus作为数据源后,可按需创建监控面板:

  • 可用性面板:指标选probe_success{job="otp-api-monitor"},使用Stat组件,配置阈值规则,值为1显示绿色(正常),值为0显示红色(异常)
  • 状态码面板:指标选probe_http_status_code{job="otp-api-monitor"},使用时序图或Stat组件,异常状态码(4xx/5xx)配置高亮提示
  • 响应耗时面板:指标选probe_duration_seconds{job="otp-api-monitor"},使用时序图组件,可配置阈值,超过指定耗时触发告警

可选:配置告警规则

可在Prometheus中配置告警规则,接口异常时自动推送告警:

groups:
- name: otp-api告警规则
  rules:
  - alert: OTP接口不可用
    expr: probe_success{job="otp-api-monitor"} == 0
    for: 1m
    labels:
      severity: 紧急
    annotations:
      summary: "接口 {{ $labels.instance }} 访问失败"
      description: "接口连续1分钟探测失败,请检查接口可用性"
  - alert: OTP接口响应超时
    expr: probe_duration_seconds{job="otp-api-monitor"} > 3
    for: 2m
    labels:
      severity: 警告
    annotations:
      summary: "接口 {{ $labels.instance }} 响应耗时过高"
      description: "接口响应耗时超过3秒,已持续2分钟,请检查接口性能"

内容的提问来源于stack exchange,提问作者Andrii Sh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 02:15:02