Need for SetListenAddress in prometheus.
Need the SetListenAddress in prometheus to include the middleware in my project where the go project is being used as an API gateway and if the default address is set to give the metrics it will make the metrics open for the public to hit.
I am looking for a way to hide the metrics-endpoint from the public as well, because the contain quite sensistive information! The only workaround I found is to change the "/metrics"-URL to secret one:
p := prometheus.NewPrometheus("echo", nil)
p.MetricsPath = "/metrics-secret"
I saw, that the methods SetListenAddress and runServer are already implemented, but commented out, so why is this not available?
no idea but you do not need them. Handler uses prometheus global objects and necessary handler can be created and served from/by ordinary http.Server
This is how handler is created for Echo https://github.com/labstack/echo-contrib/blob/4d116eef4725384df66e4c4d079254bc53ae2d54/prometheus/prometheus.go#L418-L424
This should be enough for you to serve prom form custom server.
metricsServer := http.Server{
Addr: "localhost:8080",
Handler: promhttp.Handler(),
}
if err := metricsServer.ListenAndServe(); err != http.ErrServerClosed {
// log.fatal
}
closing, new middleware was added with #94
Newer middleware allows user to separate handler function which can be served from different http server.
extremely simplistic example:
import (
"errors"
"github.com/labstack/echo-contrib/echoprometheus"
"github.com/labstack/echo/v4"
"log"
"net/http"
)
func main() {
e := echo.New()
e.Use(echoprometheus.NewMiddleware("myapp")) // Add metrics middleware
go func() {
private := echo.New()
private.GET("/metrics", echoprometheus.NewHandler()) // Add handler for metrics scrapers
if err := private.Start("localhost:8081"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
if err := e.Start("0.0.0.0:8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}