[LOGGING] Fix #9: Détection requêtes lentes avec seuil configurable (SLOW_REQUEST_THRESHOLD_MS)
This commit is contained in:
parent
8db9e93435
commit
0a3cee7109
1 changed files with 31 additions and 2 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
|
@ -70,16 +72,43 @@ func RequestLogger(logger *zap.Logger) gin.HandlerFunc {
|
|||
fields = append(fields, zap.Strings("errors", c.Errors.Errors()))
|
||||
}
|
||||
|
||||
// Logger selon le status code
|
||||
// FIX #9: Détecter les requêtes lentes avec seuil configurable
|
||||
// Seuil par défaut: 1 seconde (configurable via SLOW_REQUEST_THRESHOLD_MS)
|
||||
slowThresholdMs := getEnvInt("SLOW_REQUEST_THRESHOLD_MS", 1000) // 1000ms = 1s par défaut
|
||||
slowThreshold := time.Duration(slowThresholdMs) * time.Millisecond
|
||||
isSlowRequest := latency > slowThreshold
|
||||
|
||||
// Logger selon le status code et la latence
|
||||
if c.Writer.Status() >= 500 {
|
||||
// Erreurs serveur
|
||||
logger.Error("Request completed", fields...)
|
||||
} else if c.Writer.Status() >= 400 {
|
||||
// Erreurs client
|
||||
logger.Warn("Request completed with error", fields...)
|
||||
} else if isSlowRequest {
|
||||
// FIX #9: Requêtes lentes (succès mais > seuil)
|
||||
logger.Warn("Slow request detected",
|
||||
append(fields,
|
||||
zap.Duration("slow_threshold", slowThreshold),
|
||||
zap.Bool("is_slow", true),
|
||||
)...,
|
||||
)
|
||||
} else {
|
||||
// Succès
|
||||
// Succès normal
|
||||
logger.Info("Request completed", fields...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// getEnvInt récupère une variable d'environnement entière avec une valeur par défaut
|
||||
// FIX #9: Helper pour lire SLOW_REQUEST_THRESHOLD_MS
|
||||
func getEnvInt(key string, defaultValue int) int {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
if intValue, err := strconv.Atoi(value); err == nil {
|
||||
return intValue
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue