mirror of
https://github.com/nprasad2077/NBA_Go.git
synced 2026-09-22 14:05:13 +00:00
tests running
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
// Define metrics
|
||||
var (
|
||||
// HTTPRequestsTotal counts the number of HTTP requests processed
|
||||
HTTPRequestsTotal = promauto.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "nba_http_requests_total",
|
||||
Help: "Total number of HTTP requests",
|
||||
},
|
||||
[]string{"method", "endpoint", "status"},
|
||||
)
|
||||
|
||||
// HTTPRequestDuration measures the duration of HTTP requests
|
||||
HTTPRequestDuration = promauto.NewHistogramVec(
|
||||
prometheus.HistogramOpts{
|
||||
Name: "nba_http_request_duration_seconds",
|
||||
Help: "HTTP request duration in seconds",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
},
|
||||
[]string{"method", "endpoint"},
|
||||
)
|
||||
|
||||
// DBOperationsTotal counts database operations
|
||||
DBOperationsTotal = promauto.NewCounterVec(
|
||||
prometheus.CounterOpts{
|
||||
Name: "nba_db_operations_total",
|
||||
Help: "Total number of database operations",
|
||||
},
|
||||
[]string{"operation", "entity"},
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/nprasad2077/NBA_Go/utils/metrics"
|
||||
)
|
||||
|
||||
// MetricsMiddleware tracks HTTP metrics for Prometheus
|
||||
func MetricsMiddleware() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
start := time.Now()
|
||||
|
||||
// Process request
|
||||
err := c.Next()
|
||||
|
||||
// Record metrics after request is processed
|
||||
duration := time.Since(start).Seconds()
|
||||
status := strconv.Itoa(c.Response().StatusCode())
|
||||
method := c.Method()
|
||||
path := c.Route().Path
|
||||
|
||||
// Increment request counter
|
||||
metrics.HTTPRequestsTotal.WithLabelValues(method, path, status).Inc()
|
||||
|
||||
// Record request duration
|
||||
metrics.HTTPRequestDuration.WithLabelValues(method, path).Observe(duration)
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user