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

Prometheus gauge for in-flight requests #70

Closed
yabberyabber opened this issue Jan 19, 2022 · 1 comment
Closed

Prometheus gauge for in-flight requests #70

yabberyabber opened this issue Jan 19, 2022 · 1 comment

Comments

@yabberyabber
Copy link

I would really like to be able to track the number of requests that are currently in flight for my application.

To do this, I could of course create my own middleware that wraps each request, but with this approach it would be difficult for my custom metric to match the naming convention and label-set as those metrics provided by the prometheus package in this repo.

Would the maintainers here be open to a PR from me where I add an requests_in_flight gauge to track the number of currently running packages?

@aldas
Copy link
Contributor

aldas commented May 23, 2023

closing, new middleware was added with #94

I think you can create custom gauge metric and use BeforeNext and AfterNext functions to inc/dec that gauge value

package main

import (
	"errors"
	"github.com/labstack/echo-contrib/echoprometheus"
	"github.com/labstack/echo/v4"
	"github.com/prometheus/client_golang/prometheus"
	"log"
	"net/http"
)

func main() {
	e := echo.New()

	customCounter := prometheus.NewGauge( // create new gauge metric.
		prometheus.GaugeOpts{
			Name: "requests_in_flight",
			Help: "How many HTTP requests are currently being served",
		},
	)
	if err := prometheus.Register(customCounter); err != nil { // register your new counter metric with default prometheus registry
		log.Fatal(err)
	}

	e.Use(echoprometheus.NewMiddlewareWithConfig(echoprometheus.MiddlewareConfig{
		BeforeNext: func(c echo.Context) {
			customCounter.Inc() // before every request increment the counter

		},
		AfterNext: func(c echo.Context, err error) {
			customCounter.Dec() // after every request decrements the counter
		},
	}))
	// register route for getting gathered metrics data
	e.GET("/metrics", echoprometheus.NewHandler())

	if err := e.Start(":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

3 participants