gpt4 book ai didi

docker - 普罗米修斯未公开自定义指标

转载 作者:行者123 更新时间:2023-12-01 20:22:07 26 4
gpt4 key购买 nike

我正在编写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实例上看不到该指标。
enter image description here
问题是什么?
我认为我的服务器已被删除,因为我可以在Prometheus中看到其他指标,例如scrape_duration_seconds:
enter image description here
也许问题出在我的docker-compose文件中,用于prometheus。这是prometheus UI中的目标页面:
enter image description here
这是我的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
如果您的应用是普罗米修斯的辅助工具,则可以使用localhost;
如果没有,您必须在这里使用其服务

关于docker - 普罗米修斯未公开自定义指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63014580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com