dapr / go-sdk

Dapr SDK for go
Apache License 2.0
441 stars 170 forks source link

Go SDK healthz override #579

Closed antontroshin closed 3 weeks ago

antontroshin commented 3 weeks ago

Describe the bug When using Go SDK to start HTTP server, adding custom health check for endpoint healthz or /healthz, like so s.AddHealthCheckHandler("/healthz", healthCheckHandler), it gets overridden by default health check, implemented in registerBaseHandler() here that called in Start() method.

See: https://github.com/dapr/go-sdk/blob/33180dd89a463a16781c02f60cbb909e8ccb6aef/service/http/topic.go#L133-L136

This makes it impossible to use this endpoint for custom logic to provide more accurate app health.

To Reproduce

  1. Use this code as a server example, save it in main.go for example.
    
    package main

import ( "context" "fmt" "log" "log/slog" "net/http"

daprd "github.com/dapr/go-sdk/service/http"

)

func main() { s := daprd.NewService(":8080")

if err := s.AddHealthCheckHandler("/healthz", healthCheckHandler); err != nil {
    slog.Error(fmt.Sprintf("error adding %s health check handler: %v", "/healthz", err))
}

if err := s.Start(); err != nil && err != http.ErrServerClosed {
    log.Fatalf("error listenning: %v", err)
}

}

func healthCheckHandler(ctx context.Context) error { slog.Info("custom health check") return nil }



2. Run `go run main.go`
3. Run `curl -v http://localhost:8080/healthz`, response 200, observe logs, no "custom health check" log printed.
4. Stop the server
5. Modify custom health check path, from `s.AddHealthCheckHandler("/healthz", healthCheckHandler)` to `s.AddHealthCheckHandler("/health", healthCheckHandler)`
6. Run `curl -v http://localhost:8080/health`, response 204 (as intended [here](https://github.com/dapr/go-sdk/blob/33180dd89a463a16781c02f60cbb909e8ccb6aef/service/http/health_check.go#L41)), observe logs, "custom health check" log printed.
7. Run `curl -v http://localhost:8080/healthz`, response 200, observe logs, no "custom health check" log printed.

**Expected behavior**
Using `s.AddHealthCheckHandler("/healthz", healthCheckHandler)` will allow using custom health check handler on the `healthz` endpoint and not being overridden by default empty implementation.