31 lines
771 B
Go
31 lines
771 B
Go
package middleware
|
|
|
|
import (
|
|
"cybertron/models/instrumentation"
|
|
"cybertron/pkg/metrics"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func MetricMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
startTime := time.Now()
|
|
c.Next()
|
|
endTime := time.Now()
|
|
duration := endTime.Sub(startTime)
|
|
|
|
metricsPublisher := metrics.NewMetricPublisher()
|
|
apiMetrics := instrumentation.ApiMetric{
|
|
Url: c.FullPath(),
|
|
ResponseCode: c.Writer.Status(),
|
|
StartTime: startTime.Unix(),
|
|
EndTime: endTime.Unix(),
|
|
DurationInMs: duration.Milliseconds(),
|
|
Method: c.Request.Method,
|
|
BytesSent: c.Writer.Size(),
|
|
}
|
|
metricsPublisher.PublishMetrics(instrumentation.MetricAttributes{ApiMetric: apiMetrics}, instrumentation.API_METRICS)
|
|
}
|
|
}
|