【问题标题】:Custom metric is not exposed in prometheus自定义指标未在普罗米修斯中公开
【发布时间】:2020-11-10 20:06:06
【问题描述】:

我正在编写一个 Go 应用程序,我需要使用 Prometheus 记录一些自定义指标。我有一个 Prometheus 的本地实例,这是我的 prometheus.yml 文件:

scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - localhost:2112

这是我的 Go 代码:

package main

import (
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promauto"
    "github.com/prometheus/client_golang/prometheus/promhttp"

    "net/http"
    "time"
)

func recordMetrics() {
    go func() {
        for {
            opsProcessed.Inc()
            time.Sleep(2 * time.Second)

        }
    }()
}

var (
    opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
        Name: "myapp_processed_ops_total",
        Help: "The total number of processed events",
    })
)

func main() {
    recordMetrics()

    http.Handle("/metrics", promhttp.Handler())
    http.ListenAndServe(":2112", nil)
}

如您所见,我定义了一个名为 opsProcessed 的自定义指标,其名称为 myapp_processed_ops_total。我可以在 http://localhost:2112/metrics 看到 myapp_processed_ops_total。但是,我在我的 Prometheus 实例上看不到这个指标。

有什么问题? 我认为我的服务器被刮掉了,因为我可以在 Prometheus 中看到像 scrape_duration_seconds 这样的其他指标:

也许问题出在我的 prometheus 的 docker-compose 文件中。这是 prometheus UI 中的目标页面:

这是我的 docker-compose 文件:

version: '2.1'

networks:
  monitor-net:
    driver: bridge

volumes:
    prometheus_data: {}
    grafana_data: {}

services:

  prometheus:
    image: prom/prometheus:v2.15.2
    container_name: prometheus1
    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
    ports:
      - "9090:9090"
    networks:
      - monitor-net
    labels:
      org.label-schema.group: "monitoring"

  grafana:
    image: grafana/grafana:6.5.3
    container_name: grafana1
    volumes:
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning:/etc/grafana/provisioning
    environment:
      - GF_SECURITY_ADMIN_USER=${ADMIN_USER:-admin}
      - GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD:-admin}
      - GF_USERS_ALLOW_SIGN_UP=false
    restart: unless-stopped
    ports:
      - "3000:3000"
    networks:
      - monitor-net
    labels:
      org.label-schema.group: "monitoring"

【问题讨论】:

  • 您是否检查了目标页面以确认您的服务器实际上已被抓取?
  • 我如何检查这个?
  • 我认为我的服务器已被抓取,因为我可以在 Prometheus 中看到像 scrape_duration_seconds 这样的其他指标。
  • 在 prometheus UI 中转到 /targets。
  • 然后使用本地机器的外部IP地址代替localhost。

标签: docker go docker-compose prometheus metrics


【解决方案1】:

基于@Peter 和@Henry 有用的 cmets,答案是这样的:

从 Prometheus docker 容器到我本地运行的应用程序的连接被拒绝。我是从 /targets 的 Prometheus UI 中发现的。原因是 prometheus.yml 配置文件。如果你已经使用 docker(我的案例)设置了 Prometheus 和 Grafana,prometheus.yml 应该是这样的:

1- 如果正在运行的应用程序也在容器中,则应使用 docker-compose 文件中的服务名称:

scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - <service>:2112

2- 如果应用程序在您的机器上本地运行,但不在容器中:

scrape_configs:
- job_name: myapp
  scrape_interval: 10s
  static_configs:
  - targets:
    - <External_IP>:2112

并从 ifconfig 中找到您机器的外部 IP 地址。

【讨论】:

    【解决方案2】:
    scrape_configs:
    - job_name: myapp
      scrape_interval: 10s
      static_configs:
      - targets:
      - localhost:2112
    

    如果你的应用是 prometheus 的 sidecar,你可以使用 localhost;

    如果没有,你必须在这里使用它的服务

    【讨论】:

      猜你喜欢
      • 2022-12-13
      • 1970-01-01
      • 2020-01-10
      • 2020-11-12
      • 2021-04-10
      • 2021-09-13
      • 2022-11-12
      • 1970-01-01
      • 2022-12-02
      相关资源
      最近更新 更多