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

Docker部署的Prometheus未在/metrics端点展示本地自定义指标

问题:自定义指标未出现在Prometheus的/metrics端点,但/graph可正常查询

本地运行Java服务,通过Prometheus Java客户端将指标暴露在localhost:8081/metrics,对接Docker中运行的Prometheus容器。目前Prometheus的/graph端点能看到自定义指标已注册、采集并正常展示,但/metrics端点找不到该指标,仅能看到与my_job相关的Prometheus原生指标。

Java服务代码(暴露localhost:8081/metrics)

public class Main {
  private HTTPServer server;
  public static final int SERVER_PORT = 8081;

  /**
   * Initiates the Prometheus Exposer HTTP Server.
   */
  public void main(String args[]) {

    try {
      server = new HTTPServer(SERVER_PORT);
    } catch (IOException e) {
      throw new IllegalStateException("could not start metrics server:\t" + e);
    }

    io.prometheus.client.Gauge gauge = io.prometheus.client.Gauge
        .build()
        .namespace("some_namespace")
        .subsystem("some_subsystem")
        .name("some_name")
        .help("helpMessage")
        .register();

    int i = 0;
    while (true) {
      try {
        Thread.sleep(1000);
        gauge.set(i++);
      } catch (InterruptedException ex) {
        System.err.println("Thread sleep issue, breaking the loop");
        break;
      }
    }
  }

  /**
   * Terminates the Prometheus Exposer HTTP Server.
   */
  public void terminate() {
    try {
      server.close();
    } catch (Exception e) {
      throw new IllegalStateException("could not stop metrics server:\t" + e);
    }
  }
}

Docker Compose配置

version: '2.1'
networks:
  monitor-net:
    driver: bridge
volumes:
    prometheus_data: {}
services:
  prometheus:
    image: prom/prometheus:v2.22.1
    container_name: prometheus
    volumes:
      - ./prometheus:/etc/prometheus
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=200h'
      - '--web.enable-lifecycle'
    restart: unless-stopped
    expose:
      - 9090
    networks:
      - monitor-net
    labels:
      org.label-schema.group: "monitoring"

prometheus.yml配置

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: 'my_job'
    honor_timestamps: true
    scrape_interval: 15s
    scrape_timeout: 10s
    metrics_path: /metrics
    scheme: http
    static_configs:
      - targets: [ 'host.docker.internal:8081' ]

已确认的现象与检查项

  • 在Prometheus的/metrics端点仅能看到与my_job相关的原生指标,示例:
# TYPE prometheus_target_sync_length_seconds summary
prometheus_target_sync_length_seconds{scrape_job="my_job",quantile="0.01"} 0.000311125
prometheus_target_sync_length_seconds{scrape_job="my_job",quantile="0.05"} 0.000311125
prometheus_target_sync_length_seconds{scrape_job="my_job",quantile="0.5"} 0.000311125
prometheus_target_sync_length_seconds{scrape_job="my_job",quantile="0.9"} 0.000311125
prometheus_target_sync_length_seconds{scrape_job="my_job",quantile="0.99"} 0.000311125
  • 已完成的检查:
    • Prometheus配置文件的目标和采集配置正确
    • Prometheus和应用日志无指标采集相关错误或警告
    • 指标以Prometheus文本格式导出
    • 指标在配置的正确端点和端口暴露

解决思路

  1. 明确Prometheus /metrics端点的作用:Prometheus自身的/metrics端点仅暴露Prometheus进程自身的监控指标(如采集任务状态、存储状态等),不会包含从目标服务采集到的自定义指标。自定义指标存储在Prometheus的TSDB(时序数据库)中,只能通过/graph或/api/v1/query等查询端点检索。
  2. 验证采集链路完整性:既然/graph能查到自定义指标,说明采集流程完全正常,无需怀疑采集配置或指标暴露的问题。
  3. 确认自定义指标的存储:可通过Prometheus的api/v1/series接口查询指标是否存在,比如执行:
    curl 'http://<prometheus-ip>:9090/api/v1/series?match[]=some_namespace_some_subsystem_some_name'
    
    若返回结果包含该指标,说明已正常存储。
  4. 避免概念混淆:不要将Prometheus自身的/metrics端点(展示自身指标)和目标服务的/metrics端点(展示业务自定义指标)混淆,两者是完全独立的端点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 21:41:12