提问者:小点点

Prometheus没有在/metricsendpoint上显示来自localhost服务的自定义指标


我正在localhost上运行一个服务,该服务将指标导出到Docker上运行的Prometheus容器。我能够看到我的指标通过Prometheus的 /graphendpoint正确注册、抓取和显示。但是,我无法在Prometheus的 /metricsendpoint上找到我的指标报告。

我的服务使用PrometheusJava客户端公开localhost:8081/metrics的指标。我的服务在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);
    }
  }
}

我正在使用Prometheus Docker文件并使用以下配置运行它:

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”endpoint上收集并报告了几个与“my_job”关联的Prometheus原生指标。但是,我通过PrometheusJava客户端注册的指标只能通过“/graph”endpoint访问。以下是在“/metrics”endpoint上显示的prometheus原生指标示例:

# 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文本格式导出度量。正在通过Prometheus配置为抓取的正确endpoint和端口导出度量。我仍然不确定为什么我的度量没有显示在“/metrics”endpoint上。我将继续调查和解决问题。


共1个答案

匿名用户

哦,你搞混了。http://localhost:9090/metrics是prometheus实例上的一个url。它将显示关于prometheus本身的指标,例如抓取了多少数据,存储了多少数据等。它不应该显示prometheus收集的数据。所以你找到的指标只是prometheus花了多少时间抓取你的数据的状态。

您实际上想要的是访问PrometheusAPIhttp://localhost:9090/api/v1并查询有关度量的数据。但是一旦您了解了如何运行查询(这也可以在 /graphendpoint上完成),您可能需要像Grafana这样的可视化工具。

回到你的问题:它按设计工作。

  • https://prometheus.io/docs/prometheus/latest/querying/api/
  • https://grafana.com/