我正在编写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。这是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"
答案 0 :(得分:1)
scrape_configs:
- job_name: myapp
scrape_interval: 10s
static_configs:
- targets:
- localhost:2112
如果您的应用是普罗米修斯的辅助工具,则可以使用localhost;
如果不是,则必须在此处使用其服务
答案 1 :(得分:0)
基于@Peter和@Henry有用的评论,答案是这样的:
从Prometheus泊坞窗容器到我本地运行的应用程序的连接被拒绝。我从Prometheus UI的/ targets找到了有关它的信息。原因是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地址。