mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
create a gin middleware to collect request metrics and export them to the Prometheus route
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||||
"github.com/hermeznetwork/hermez-node/db/l2db"
|
"github.com/hermeznetwork/hermez-node/db/l2db"
|
||||||
|
"github.com/hermeznetwork/hermez-node/metric"
|
||||||
"github.com/hermeznetwork/tracerr"
|
"github.com/hermeznetwork/tracerr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,6 +51,12 @@ func NewAPI(
|
|||||||
hermezAddress: consts.HermezAddress,
|
hermezAddress: consts.HermezAddress,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
middleware, err := metric.PrometheusMiddleware()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
server.Use(middleware)
|
||||||
|
|
||||||
v1 := server.Group("/v1")
|
v1 := server.Group("/v1")
|
||||||
|
|
||||||
// Add coordinator endpoints
|
// Add coordinator endpoints
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||||
"github.com/hermeznetwork/hermez-node/log"
|
"github.com/hermeznetwork/hermez-node/log"
|
||||||
|
"github.com/hermeznetwork/hermez-node/metric"
|
||||||
"github.com/hermeznetwork/tracerr"
|
"github.com/hermeznetwork/tracerr"
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
"github.com/russross/meddler"
|
"github.com/russross/meddler"
|
||||||
@@ -46,7 +47,9 @@ var (
|
|||||||
|
|
||||||
func retSQLErr(err error, c *gin.Context) {
|
func retSQLErr(err error, c *gin.Context) {
|
||||||
log.Warnw("HTTP API SQL request error", "err", err)
|
log.Warnw("HTTP API SQL request error", "err", err)
|
||||||
errMsg := tracerr.Unwrap(err).Error()
|
unwrapErr := tracerr.Unwrap(err)
|
||||||
|
metric.CollectError(unwrapErr)
|
||||||
|
errMsg := unwrapErr.Error()
|
||||||
retDupKey := func(errCode pq.ErrorCode) {
|
retDupKey := func(errCode pq.ErrorCode) {
|
||||||
// https://www.postgresql.org/docs/current/errcodes-appendix.html
|
// https://www.postgresql.org/docs/current/errcodes-appendix.html
|
||||||
if errCode == "23505" {
|
if errCode == "23505" {
|
||||||
@@ -80,6 +83,7 @@ func retSQLErr(err error, c *gin.Context) {
|
|||||||
|
|
||||||
func retBadReq(err error, c *gin.Context) {
|
func retBadReq(err error, c *gin.Context) {
|
||||||
log.Warnw("HTTP API Bad request error", "err", err)
|
log.Warnw("HTTP API Bad request error", "err", err)
|
||||||
|
metric.CollectError(err)
|
||||||
c.JSON(http.StatusBadRequest, errorMsg{
|
c.JSON(http.StatusBadRequest, errorMsg{
|
||||||
Message: err.Error(),
|
Message: err.Error(),
|
||||||
})
|
})
|
||||||
|
|||||||
78
metric/request.go
Normal file
78
metric/request.go
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
package metric
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
favicon = "/favicon.ico"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Prometheus contains the metrics gathered by the instance and its path
|
||||||
|
type Prometheus struct {
|
||||||
|
reqCnt *prometheus.CounterVec
|
||||||
|
reqDur *prometheus.HistogramVec
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPrometheus generates a new set of metrics with a certain subsystem name
|
||||||
|
func NewPrometheus() (*Prometheus, error) {
|
||||||
|
reqCnt := prometheus.NewCounterVec(
|
||||||
|
prometheus.CounterOpts{
|
||||||
|
Namespace: namespaceAPI,
|
||||||
|
Name: "requests_total",
|
||||||
|
Help: "How many HTTP requests processed, partitioned by status code and HTTP method",
|
||||||
|
},
|
||||||
|
[]string{"code", "method", "path"},
|
||||||
|
)
|
||||||
|
if err := registerCollector(reqCnt); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
reqDur := prometheus.NewHistogramVec(
|
||||||
|
prometheus.HistogramOpts{
|
||||||
|
Namespace: namespaceAPI,
|
||||||
|
Name: "request_duration_seconds",
|
||||||
|
Help: "The HTTP request latencies in seconds",
|
||||||
|
},
|
||||||
|
[]string{"code", "method", "path"},
|
||||||
|
)
|
||||||
|
if err := registerCollector(reqDur); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Prometheus{
|
||||||
|
reqCnt: reqCnt,
|
||||||
|
reqDur: reqDur,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrometheusMiddleware creates the prometheus collector and
|
||||||
|
// defines status handler function for the middleware
|
||||||
|
func PrometheusMiddleware() (gin.HandlerFunc, error) {
|
||||||
|
p, err := NewPrometheus()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p.Middleware(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Middleware defines status handler function for middleware
|
||||||
|
func (p *Prometheus) Middleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if c.Request.URL.Path == favicon {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
start := time.Now()
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
status := strconv.Itoa(c.Writer.Status())
|
||||||
|
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||||
|
fullPath := c.FullPath()
|
||||||
|
|
||||||
|
p.reqDur.WithLabelValues(status, c.Request.Method, fullPath).Observe(elapsed)
|
||||||
|
p.reqCnt.WithLabelValues(status, c.Request.Method, fullPath).Inc()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user