Improve request logging and make health check lightweight
- Log slow requests (>1s) at WARNING level with slow=true flag - Log request timeouts at WARNING level in TimeoutMiddleware - Replace heavy GetStatsContext with lightweight Ping in health check - Add Ping method to database interface (SELECT 1)
This commit is contained in:
@@ -37,6 +37,7 @@ type HealthCheckResponse struct {
|
||||
|
||||
// handleHealthCheck returns a handler that performs health checks.
|
||||
// Returns 200 if healthy, 503 if any check fails.
|
||||
// Uses lightweight checks to avoid timeout issues under load.
|
||||
func (s *Server) handleHealthCheck() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), healthCheckTimeout)
|
||||
@@ -45,13 +46,11 @@ func (s *Server) handleHealthCheck() http.HandlerFunc {
|
||||
checks := make(map[string]string)
|
||||
healthy := true
|
||||
|
||||
// Check database connectivity
|
||||
dbStats, err := s.db.GetStatsContext(ctx)
|
||||
// Check database connectivity with lightweight ping
|
||||
err := s.db.Ping(ctx)
|
||||
if err != nil {
|
||||
checks["database"] = "error: " + err.Error()
|
||||
healthy = false
|
||||
} else if dbStats.ASNs == 0 && dbStats.Prefixes == 0 {
|
||||
checks["database"] = "warning: empty database"
|
||||
} else {
|
||||
checks["database"] = "ok"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user