Skip to content

Commit

Permalink
Support HEAD method in health endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwarden committed Jan 16, 2025
1 parent 20d1278 commit f34d9b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
17 changes: 10 additions & 7 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ func (app app) healthHandler() func(http.ResponseWriter, *http.Request, loginPar
return func(w http.ResponseWriter, r *http.Request, p loginParams) error {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")

_, err := w.Write([]byte("ok"))
if err != nil {
return errors.Wrap(err, "writing response")
if r.Method != http.MethodHead {
_, err := w.Write([]byte("ok"))
if err != nil {
return errors.Wrap(err, "writing response")
}
}

return nil
Expand All @@ -25,7 +27,6 @@ func (app app) healthHandler() func(http.ResponseWriter, *http.Request, loginPar

func (app app) crawlHealthHandler() func(http.ResponseWriter, *http.Request, loginParams) error {
return func(w http.ResponseWriter, r *http.Request, p loginParams) error {

w.Header().Set("Content-Type", "text/plain; charset=utf-8")

lastSampleTime, err := app.ndb.selectLastCrawlTime()
Expand All @@ -37,9 +38,11 @@ func (app app) crawlHealthHandler() func(http.ResponseWriter, *http.Request, log
return fmt.Errorf("last successful crawl of %d is more than %d minutes ago", lastSampleTime, alertAfterMinutes)
}

_, err = w.Write([]byte("ok"))
if err != nil {
return errors.Wrap(err, "writing response")
if r.Method != http.MethodHead {
_, err = w.Write([]byte("ok"))
if err != nil {
return errors.Wrap(err, "writing response")
}
}

return nil
Expand Down
2 changes: 2 additions & 0 deletions httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func (app app) httpServer(onPanic func(error)) *http.Server {
router.GET("/logout", middleware("logout", l, onPanic, app.logoutHandler()))

router.GET("/health", middleware("health", l, onPanic, app.healthHandler()))
router.HEAD("/health", middleware("health", l, onPanic, app.healthHandler()))
router.GET("/crawl-health", middleware("crawl-health", l, onPanic, app.crawlHealthHandler()))
router.HEAD("/crawl-health", middleware("crawl-health", l, onPanic, app.crawlHealthHandler()))

server.Handler = app.preRouterMiddleware(router, writeTimeout-100*time.Millisecond)

Expand Down
26 changes: 22 additions & 4 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,21 @@ var (
databaseSizeBytes *metrics.Gauge
databaseFragmentationPercent *metrics.Gauge
vacuumOperationsTotal = metrics.NewCounter(`database_vacuum_operations_total{database="frontpage"}`)

// Store histograms per route to avoid duplicate registration
routeHistograms = make(map[string]*metrics.Histogram)
)

// getRouteHistogram returns an existing histogram for a route or creates a new one
func getRouteHistogram(routeName string) *metrics.Histogram {
if h, exists := routeHistograms[routeName]; exists {
return h
}
h := metrics.NewHistogram(`requests_duration_seconds{route="` + routeName + `"}`)
routeHistograms[routeName] = h
return h
}

func servePrometheusMetrics() func(ctx context.Context) error {
mux := http.NewServeMux()

Expand All @@ -51,15 +64,20 @@ func servePrometheusMetrics() func(ctx context.Context) error {
}

func prometheusMiddleware[P any](routeName string, h httperror.XHandler[P]) httperror.XHandlerFunc[P] {
// Register summary with a single label.
requestDuration := metrics.NewHistogram(`requests_duration_seconds{route="` + routeName + `"}`)
requestDuration := getRouteHistogram(routeName)

return func(w http.ResponseWriter, r *http.Request, p P) error {
startTime := time.Now()
defer requestDuration.UpdateDuration(startTime)
var startTime time.Time
if r.Method != http.MethodHead {
startTime = time.Now()
}

err := h.Serve(w, r, p)

if r.Method != http.MethodHead {
requestDuration.UpdateDuration(startTime)
}

return err
}
}
Expand Down

0 comments on commit f34d9b8

Please sign in to comment.