Prometheus Implementation #1892
Answered
by
aldas
nicholasanthonys
asked this question in
Q&A
-
How to implement prometheus counter on echo ? should i use this package https://github.com/prometheus/client_golang ? or is it enough to use echo package : github.com/labstack/echo-contrib/prometheus ? |
Beta Was this translation helpful? Give feedback.
Answered by
aldas
Jun 10, 2021
Replies: 1 comment 1 reply
-
https://github.com/labstack/echo-contrib/blob/master/prometheus/prometheus.go registers middleware for http server and exposes prometheus metrics through You can use it with your own custom metrics. For example: import (
promMW "github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
"github.com/prometheus/client_golang/prometheus"
"net/http"
)
func main() {
e := echo.New()
p := promMW.NewPrometheus("echo", nil)
myCounter := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "my_handler_executions",
Help: "Counts executions of my handler function.",
ConstLabels: prometheus.Labels{"version": "1234"},
})
if err := prometheus.Register(myCounter); err != nil {
log.Fatal(err)
}
e.GET("/", func(c echo.Context) error {
myCounter.Inc()
return c.JSON(http.StatusOK, "OK")
})
p.Use(e) // Enable metrics middleware and register route to expose metrics
if err := e.Start("localhost:8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
} And output: x@x:~/code$ curl http://localhost:8080
"OK"
x@x:~/code$ curl http://localhost:8080
"OK"
x@x:~/code$ curl http://localhost:8080/metrics
# HELP echo_request_duration_seconds The HTTP request latencies in seconds.
# TYPE echo_request_duration_seconds histogram
echo_request_duration_seconds_bucket{code="200",method="GET",url="/",le="0.005"} 2
echo_request_duration_seconds_bucket{code="200",method="GET",url="/",le="0.01"} 2
...
...
# HELP my_handler_executions Counts executions of my handler functions.
# TYPE my_handler_executions gauge
my_handler_executions{version="1234"} 2
...
...
... |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nicholasanthonys
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/labstack/echo-contrib/blob/master/prometheus/prometheus.go registers middleware for http server and exposes prometheus metrics through
/metrics
route.You can use it with your own custom metrics.
For example: