From 024f69b2c7ecfdefd9d9b5dbea155e679aafc03d Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Thu, 11 Feb 2021 00:13:07 +0100 Subject: [PATCH] feat: add health check to telemetry server Signed-off-by: Mark Sagi-Kazar --- cmd/dex/serve.go | 25 +++++++++++++++++++++++++ go.mod | 1 + go.sum | 6 ++++++ storage/health.go | 29 +++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 storage/health.go diff --git a/cmd/dex/serve.go b/cmd/dex/serve.go index 7afb8851..469f1e13 100644 --- a/cmd/dex/serve.go +++ b/cmd/dex/serve.go @@ -14,6 +14,9 @@ import ( "syscall" "time" + gosundheit "github.com/AppsFlyer/go-sundheit" + "github.com/AppsFlyer/go-sundheit/checks" + gosundheithttp "github.com/AppsFlyer/go-sundheit/http" "github.com/ghodss/yaml" grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/oklog/run" @@ -325,6 +328,28 @@ func runServe(options serveOptions) error { telemetryServ := http.NewServeMux() telemetryServ.Handle("/metrics", promhttp.HandlerFor(prometheusRegistry, promhttp.HandlerOpts{})) + // Configure health checker + healthChecker := gosundheit.New() + { + handler := gosundheithttp.HandleHealthJSON(healthChecker) + telemetryServ.Handle("/healthz", handler) + + // Kubernetes style health checks + telemetryServ.HandleFunc("/healthz/live", func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte("ok")) + }) + telemetryServ.Handle("/healthz/ready", handler) + } + + healthChecker.RegisterCheck(&gosundheit.Config{ + Check: &checks.CustomCheck{ + CheckName: "storage", + CheckFunc: storage.NewCustomHealthCheckFunc(serverConfig.Storage, serverConfig.Now), + }, + ExecutionPeriod: 15 * time.Second, + InitiallyPassing: true, + }) + var gr run.Group if c.Telemetry.HTTP != "" { telemetrySrv := &http.Server{Addr: c.Telemetry.HTTP, Handler: telemetryServ} diff --git a/go.mod b/go.mod index 2b279879..b08c64d4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/dexidp/dex go 1.15 require ( + github.com/AppsFlyer/go-sundheit v0.3.1 github.com/Microsoft/hcsshim v0.8.14 // indirect github.com/beevik/etree v1.1.0 github.com/coreos/go-oidc/v3 v3.0.0 diff --git a/go.sum b/go.sum index 1147f6e5..9affe62c 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/AppsFlyer/go-sundheit v0.3.1 h1:Zqnr3wV3WQmXonc234k9XZAoV2KHUHw3osR5k2iHQZE= +github.com/AppsFlyer/go-sundheit v0.3.1/go.mod h1:iZ8zWMS7idcvmqewf5mEymWWgoOiG/0WD4+aeh+heX4= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= @@ -102,6 +104,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= +github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw= +github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= @@ -339,6 +343,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48= +github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/storage/health.go b/storage/health.go new file mode 100644 index 00000000..5df0373d --- /dev/null +++ b/storage/health.go @@ -0,0 +1,29 @@ +package storage + +import ( + "fmt" + "time" +) + +// NewCustomHealthCheckFunc returns a new health check function. +func NewCustomHealthCheckFunc(s Storage, now func() time.Time) func() (details interface{}, err error) { + return func() (details interface{}, err error) { + a := AuthRequest{ + ID: NewID(), + ClientID: NewID(), + + // Set a short expiry so if the delete fails this will be cleaned up quickly by garbage collection. + Expiry: now().Add(time.Minute), + } + + if err := s.CreateAuthRequest(a); err != nil { + return nil, fmt.Errorf("create auth request: %v", err) + } + + if err := s.DeleteAuthRequest(a.ID); err != nil { + return nil, fmt.Errorf("delete auth request: %v", err) + } + + return nil, nil + } +}