自定义指标未在普罗米修斯中公开
Custom metric is not exposed in prometheus
我正在编写一个 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:
也许问题出在我的 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"
scrape_configs:
- job_name: myapp
scrape_interval: 10s
static_configs:
- targets:
- localhost:2112
如果您的应用是 prometheus 的 sidecar,您可以使用 localhost;
如果没有,你必须在这里使用它的服务
根据@Peter 和@Henry 的有用评论,答案是这样的:
从 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 地址。
我正在编写一个 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:
也许问题出在我的 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"
scrape_configs:
- job_name: myapp
scrape_interval: 10s
static_configs:
- targets:
- localhost:2112
如果您的应用是 prometheus 的 sidecar,您可以使用 localhost;
如果没有,你必须在这里使用它的服务
根据@Peter 和@Henry 的有用评论,答案是这样的:
从 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 地址。