我已经从localhost:57475/metrics
启动了Prometheus服务器:
使用下面的Go代码:
package main
import (
"net/http"
"contrib.go.opencensus.io/exporter/prometheus"
log "github.com/sirupsen/logrus"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/stats/view"
)
func main() {
stop := make(chan struct{})
server := &http.Server{Addr: ":8080"}
statsMux := http.NewServeMux()
statsServer := &http.Server{Handler: statsMux, Addr: ":57475"}
if err := view.Register(ochttp.DefaultServerViews...); err != nil {
log.WithError(err).Fatal("register HTTP metrics view")
}
exporter, err := prometheus.NewExporter(prometheus.Options{
Namespace: "default",
})
if err != nil {
log.WithError(err).Fatal("create Prometheus exporter")
}
view.RegisterExporter(exporter)
statsMux.Handle("/metrics", exporter)
originalHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
})
och := &ochttp.Handler{
Handler: originalHandler,
}
server.Handler = och
go func() {
log.Info("Starting stats server...")
if err := statsServer.ListenAndServe(); err != nil {
log.WithError(err).Fatal("listen and serve stats")
}
}()
go func() {
log.Info("Starting server...")
if err := server.ListenAndServe(); err != nil {
log.WithError(err).Fatal("listen and serve service endpoints")
}
}()
<-stop
}
我还使用以下命令在容器中启动了Grafana(https://grafana.com/docs/installation/docker/之后:]
> docker run --detach --publish 3000:3000 --env "GF_SECURITY_ADMIN_PASSWORD=secret" grafana/grafana
2a17ad2bcc05e2955190129b981b3329cd2c5e89098e28b443337ac79ed607f2
[在Grafana中,我想连接到在Mac localhost
上运行的Prometheus导出器。在https://docs.docker.com/docker-for-mac/networking/之后,我尝试使用特殊的DNS名称host.docker.internal
,因此将URL指定为http://host.docker.internal:57475/metrics
。但是,当我单击“保存并测试”时,这会导致显示HTTP Error Not Found
:
任何想法为什么这不起作用?
将--network host
添加到docker run
命令:
docker run --network host...
然后在Grafana设置中使用localhost:[prometheus_port]
相关docs