Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need for SetListenAddress in prometheus. #31

Closed
ankur-cashfree opened this issue Feb 16, 2020 · 3 comments
Closed

Need for SetListenAddress in prometheus. #31

ankur-cashfree opened this issue Feb 16, 2020 · 3 comments

Comments

@ankur-cashfree
Copy link

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.

@aschiffmann
Copy link

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?

@aldas
Copy link
Contributor

aldas commented Sep 18, 2021

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

func prometheusHandler() echo.HandlerFunc {
h := promhttp.Handler()
return func(c echo.Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
}

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
	}

@aldas
Copy link
Contributor

aldas commented May 23, 2023

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)
	}
}

@aldas aldas closed this as completed May 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants