52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
)
|
|
|
|
var (
|
|
// httpRequestsTotal compte le total de requêtes HTTP par méthode, path et status
|
|
httpRequestsTotal = promauto.NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "veza_gin_http_requests_total",
|
|
Help: "Total number of HTTP requests (Gin middleware)",
|
|
},
|
|
[]string{"method", "path", "status"},
|
|
)
|
|
|
|
// httpRequestDuration mesure la durée des requêtes HTTP
|
|
httpRequestDuration = promauto.NewHistogramVec(
|
|
prometheus.HistogramOpts{
|
|
Name: "veza_gin_http_request_duration_seconds",
|
|
Help: "HTTP request duration in seconds (Gin middleware)",
|
|
Buckets: prometheus.DefBuckets,
|
|
},
|
|
[]string{"method", "path", "status"},
|
|
)
|
|
)
|
|
|
|
// Metrics middleware pour collecter métriques HTTP
|
|
// Mesure la durée et compte les requêtes HTTP avec labels (method, path, status)
|
|
func Metrics() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
start := time.Now()
|
|
path := c.FullPath()
|
|
if path == "" {
|
|
path = c.Request.URL.Path
|
|
}
|
|
|
|
c.Next()
|
|
|
|
duration := time.Since(start).Seconds()
|
|
status := strconv.Itoa(c.Writer.Status())
|
|
method := c.Request.Method
|
|
|
|
httpRequestsTotal.WithLabelValues(method, path, status).Inc()
|
|
httpRequestDuration.WithLabelValues(method, path, status).Observe(duration)
|
|
}
|
|
}
|