Files
Developer 349d783fd1 refactor: clean up project structure
- Remove old review reports (keep latest only)
- Move docs/ to deploy/docs-backup/
- Move performance-testing/ to deploy/
- Clean up test output files
- Organize root directory
2026-04-06 23:36:03 +08:00

48 lines
1.1 KiB
Go

// Package middleware provides HTTP middleware for metrics collection.
package middleware
import (
"strconv"
"time"
"github.com/Wei-Shaw/sub2api/internal/pkg/metrics"
"github.com/gin-gonic/gin"
)
// PrometheusMetrics returns a Gin middleware that records Prometheus metrics
func PrometheusMetrics() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.FullPath()
if path == "" {
path = "unknown"
}
c.Next()
duration := time.Since(start).Seconds()
status := strconv.Itoa(c.Writer.Status())
// Record HTTP metrics
metrics.RecordHTTPRequest(c.Request.Method, path, status, duration)
}
}
// GatewayMetrics returns a Gin middleware that records gateway-specific metrics
func GatewayMetrics() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
platform := c.GetString("platform")
model := c.GetString("model")
c.Next()
duration := time.Since(start).Seconds()
status := strconv.Itoa(c.Writer.Status())
if platform != "" {
metrics.RecordGatewayRequest(platform, model, status, duration)
}
}
}