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
Docs or examples for creating custom prometheus metrics #76
Comments
Hi @starkers I faced a similar issue and I was able to do it in a slightly different way. As the issue is still open. adding the approach. I initialised and registered my metrics prior to attaching the middleware and was able to add custom metrics in the platform. Borrowing from the added doc above, it works in below format too
Make sure the above code is invoked before your initializing echo server
|
closing, new middleware was added with #94 custom metrics can be added now as follows NB: this example creates custom registry but you could register these also with default prometheus registry 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()
customRegistry := prometheus.NewRegistry() // create custom registry for your custom metrics
customCounter := prometheus.NewCounter( // create new counter metric. This is replacement for `prometheus.Metric` struct
prometheus.CounterOpts{
Name: "custom_requests_total",
Help: "How many HTTP requests processed, partitioned by status code and HTTP method.",
},
)
if err := customRegistry.Register(customCounter); err != nil { // register your new counter metric with metrics registry
log.Fatal(err)
}
e.Use(echoprometheus.NewMiddlewareWithConfig(echoprometheus.MiddlewareConfig{
AfterNext: func(c echo.Context, err error) {
customCounter.Inc() // use our custom metric in middleware. after every request increment the counter
},
Registerer: customRegistry, // use our custom registry instead of default Prometheus registry
}))
// register route for getting gathered metrics data from our custom Registry
e.GET("/metrics", echoprometheus.NewHandlerWithConfig(echoprometheus.HandlerConfig{Gatherer: customRegistry}))
if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
} |
Issue Description
Hello, I'm just checking out the middleware (don't want to write my own)..
Typically I'm used to just using the vanilla prometheus
promauto
(EGpromauto.NewCounter()
to create custom metrics. However with this library and the examples it is not clear how this is done with this middleware.I can see severally public functions but honestly I can't figure out how to just do this simple thing.
Is this even possible? If so, I'm happy to PR some docs to echo with an example if someone can explain to me how to access them. (thanks)
Checklist
Expected behaviour
Actual behaviour
Steps to reproduce
Working code to debug
Version/commit
The text was updated successfully, but these errors were encountered: