enhancement: add readyz and healthz check handler

This commit is contained in:
Florian Schade
2024-10-14 08:39:42 +02:00
committed by Christian Richter
parent c82a7c560e
commit 0671039474
49 changed files with 511 additions and 199 deletions
+20 -29
View File
@@ -1,22 +1,36 @@
package debug
import (
"io"
"context"
"net/http"
"net/url"
"github.com/owncloud/ocis/v2/ocis-pkg/handlers"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/service/debug"
"github.com/owncloud/ocis/v2/ocis-pkg/shared"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
"github.com/owncloud/ocis/v2/services/idp/pkg/config"
)
// Server initializes the debug service and server.
func Server(opts ...Option) (*http.Server, error) {
options := newOptions(opts...)
checkHandler := handlers.NewCheckHandler(
handlers.NewCheckHandlerConfiguration().
WithLogger(options.Logger).
WithCheck("tcp-check", func(ctx context.Context) error {
tcpURL := options.Config.Ldap.URI
u, err := url.Parse(options.Config.Ldap.URI)
if err != nil {
return err
}
if u.Host != "" {
tcpURL = u.Host
}
return handlers.NewTCPCheck(tcpURL)(ctx)
}),
)
return debug.NewService(
debug.Logger(options.Logger),
debug.Name(options.Config.Service.Name),
@@ -25,30 +39,7 @@ func Server(opts ...Option) (*http.Server, error) {
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),
debug.Zpages(options.Config.Debug.Zpages),
debug.Health(health(options.Config, options.Logger)),
debug.Ready(handlers.Ready),
debug.Health(checkHandler),
debug.Ready(checkHandler),
), nil
}
// health implements the health check.
func health(cfg *config.Config, l log.Logger) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
targetHost, err := url.Parse(cfg.Ldap.URI)
if err != nil {
l.Fatal().Err(err).Str("uri", cfg.Ldap.URI).Msg("invalid LDAP URI")
}
err = shared.RunChecklist(shared.TCPConnect(targetHost.Host))
retVal := http.StatusOK
if err != nil {
l.Error().Err(err).Msg("Healtcheck failed")
retVal = http.StatusInternalServerError
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(retVal)
_, err = io.WriteString(w, http.StatusText(retVal))
if err != nil {
l.Fatal().Err(err).Msg("Could not write health check body")
}
}
}