labstack / echox

Echo cookbook and website
https://echo.labstack.com
MIT License
406 stars 286 forks source link

Need guidance on using echoprometheus.NewMiddleware for metrics server setup #285

Closed agrib-01 closed 1 year ago

agrib-01 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

I'll update website soon (this weekend)

aldas commented 1 year ago

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