feat(health): add health checking endpoints and logic
Introduce health checking functionality with liveness and readiness endpoints to monitor the application's status. Implement a health checker that verifies database connectivity and provides a simple liveness check. Update service routing to use the new health checker functionality. Add corresponding unit tests for validation.
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Checker struct {
|
||||
db *sql.DB
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func New(db *sql.DB, logger *slog.Logger) *Checker {
|
||||
return &Checker{
|
||||
db: db,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
type HealthStatus struct {
|
||||
Status string `json:"status"`
|
||||
Checks map[string]string `json:"checks,omitempty"`
|
||||
}
|
||||
|
||||
// LivenessHandler checks if the application is running
|
||||
// This is a simple check that always returns OK if the handler is reached
|
||||
func (h *Checker) LivenessHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(HealthStatus{
|
||||
Status: "UP",
|
||||
})
|
||||
}
|
||||
|
||||
// ReadinessHandler checks if the application is ready to accept traffic
|
||||
// This checks database connectivity and other critical dependencies
|
||||
func (h *Checker) ReadinessHandler(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
checks := make(map[string]string)
|
||||
allHealthy := true
|
||||
|
||||
// Check database connectivity
|
||||
if err := h.db.PingContext(ctx); err != nil {
|
||||
h.logger.With("error", err).Warn("database health check failed")
|
||||
checks["database"] = "DOWN"
|
||||
allHealthy = false
|
||||
} else {
|
||||
checks["database"] = "UP"
|
||||
}
|
||||
|
||||
status := HealthStatus{
|
||||
Status: "UP",
|
||||
Checks: checks,
|
||||
}
|
||||
|
||||
if !allHealthy {
|
||||
status.Status = "DOWN"
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
_ = json.NewEncoder(w).Encode(status)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(status)
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLivenessHandler(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
db, _, err := sqlmock.New()
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
checker := New(db, logger)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/health/live", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
checker.LivenessHandler(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Contains(t, rec.Body.String(), `"status":"UP"`)
|
||||
}
|
||||
|
||||
func TestReadinessHandler_Healthy(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
db, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
// Expect a ping and return success
|
||||
mock.ExpectPing().WillReturnError(nil)
|
||||
|
||||
checker := New(db, logger)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/health/ready", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
checker.ReadinessHandler(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.Contains(t, rec.Body.String(), `"status":"UP"`)
|
||||
assert.Contains(t, rec.Body.String(), `"database":"UP"`)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
||||
func TestReadinessHandler_DatabaseDown(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
db, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||
require.NoError(t, err)
|
||||
defer db.Close()
|
||||
|
||||
// Expect a ping and return error
|
||||
mock.ExpectPing().WillReturnError(sql.ErrConnDone)
|
||||
|
||||
checker := New(db, logger)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/health/ready", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
checker.ReadinessHandler(rec, req)
|
||||
|
||||
assert.Equal(t, http.StatusServiceUnavailable, rec.Code)
|
||||
assert.Contains(t, rec.Body.String(), `"status":"DOWN"`)
|
||||
assert.Contains(t, rec.Body.String(), `"database":"DOWN"`)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
Reference in New Issue
Block a user