chore: merge backend repo history into monorepo at backend/
git-subtree-dir: backend git-subtree-mainline:dc11190527git-subtree-split:acae976622
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"dating-app/internal/models"
|
||||
"dating-app/internal/monitoring"
|
||||
)
|
||||
|
||||
// Metrics هر درخواست رو (متد، مسیر، کد پاسخ، مدت زمان) توی Store ثبت میکنه.
|
||||
// باید قبل از Recovery ثبت بشه (بیرونیتر) تا حتی درخواستهایی که پنیک میکنن
|
||||
// و توسط Recovery مهار میشن هم توی آمار لحاظ بشن.
|
||||
func Metrics(store *monitoring.Store) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
defer func() {
|
||||
path := c.FullPath()
|
||||
if path == "" {
|
||||
path = c.Request.URL.Path
|
||||
}
|
||||
|
||||
rec := monitoring.RequestRecord{
|
||||
Time: start,
|
||||
Method: c.Request.Method,
|
||||
Path: path,
|
||||
StatusCode: c.Writer.Status(),
|
||||
DurationMs: float64(time.Since(start).Microseconds()) / 1000.0,
|
||||
ClientIP: c.ClientIP(),
|
||||
}
|
||||
if userVal, exists := c.Get(ContextUserKey); exists {
|
||||
if user, ok := userVal.(*models.User); ok {
|
||||
id := user.ID
|
||||
rec.UserID = &id
|
||||
}
|
||||
}
|
||||
store.Record(rec)
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user