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 guidance on using echoprometheus.NewMiddleware for metrics server setup #285

Closed
agrib-01 opened this issue May 27, 2023 · 3 comments
Closed

Comments

@agrib-01
Copy link

agrib-01 commented May 27, 2023

Description:
I'm currently working on a Go project where I'm building an API using the Echo framework. Previously, I was using the prometheus.NewPrometheus method from github.com/labstack/echo-contrib/prometheus to set up my metrics server and seperate the metric endpoint. However, the method is deprecated, and the documentation suggests using the echoprometheus package instead.

I have been trying to find examples or documentation on how to use the echoprometheus.NewMiddleware method, but I couldn't locate any. I would greatly appreciate some guidance on how to correctly set up my metrics server using echoprometheus.NewMiddleware, and it would be helpful if the documentation could be updated accordingly. Currently, the documentation still refers to prometheus.NewPrometheus as the recommended approach.
https://echo.labstack.com/middleware/prometheus/

Here is the code snippet that I have been using with prometheus.NewPrometheus, and now I am looking for a solution using echoprometheus.NewMiddleware:

func NewMetricServer(subsystem string, appServer *echo.Echo) *echo.Echo {
	metricServer := echo.New()
	metricServer.HideBanner = true
	p := prometheus.NewPrometheus(subsystem, nil)
	p.SetMetricsPath(metricServer)
	p.Use(appServer)
	return metricServer
}

Any help or code examples illustrating the use of echoprometheus.NewMiddleware would be greatly appreciated. It would also be helpful if the documentation for echoprometheus could be updated to reflect the recommended approach.

@aldas
Copy link
Contributor

aldas commented May 27, 2023

This example adds prometheus middleware to gather metrics (on port 8080) and exposes gathered metrics on port 8081

func main() {
	app := echo.New()
	app.Use(echoprometheus.NewMiddleware("myapp"))

	go func() {
		metrics := echo.New()
		metrics.GET("/metrics", echoprometheus.NewHandler())
		if err := metrics.Start(":8081"); err != nil && !errors.Is(err, http.ErrServerClosed) {
			log.Fatal(err)
		}
	}()

	app.GET("/hello", func(c echo.Context) error {
		return c.String(http.StatusOK, "hello")
	})

	if err := app.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}

Migration guide is here https://github.com/labstack/echo-contrib/blob/master/echoprometheus/README.md

@aldas
Copy link
Contributor

aldas commented May 27, 2023

I'll update website soon (this weekend)

@aldas
Copy link
Contributor

aldas commented May 27, 2023

I updated website https://echo.labstack.com/middleware/prometheus/

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

No branches or pull requests

2 participants