Skip to content

Commit

Permalink
Merge pull request #169 from kaleido-io/monitoring-health-route
Browse files Browse the repository at this point in the history
Add Livez route to monitoring server
  • Loading branch information
EnriqueL8 authored Feb 21, 2025
2 parents 1434b62 + 0fefd80 commit 253fa7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
15 changes: 14 additions & 1 deletion pkg/ffapi/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"net"
"net/http"
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -63,6 +64,7 @@ type apiServer[T any] struct {
handleYAML bool
monitoringEnabled bool
metricsPath string
livenessPath string
monitoringPublicURL string
mux *mux.Router

Expand Down Expand Up @@ -103,6 +105,7 @@ func NewAPIServer[T any](ctx context.Context, options APIServerOptions[T]) APISe
requestMaxTimeout: options.APIConfig.GetDuration(ConfAPIRequestMaxTimeout),
monitoringEnabled: options.MonitoringConfig.GetBool(ConfMonitoringServerEnabled),
metricsPath: options.MonitoringConfig.GetString(ConfMonitoringServerMetricsPath),
livenessPath: options.MonitoringConfig.GetString(ConfMonitoringServerLivenessPath),
alwaysPaginate: options.APIConfig.GetBool(ConfAPIAlwaysPaginate),
handleYAML: options.HandleYAML,
apiDynamicPublicURLHeader: options.APIConfig.GetString(ConfAPIDynamicPublicURLHeader),
Expand Down Expand Up @@ -301,6 +304,11 @@ func (as *apiServer[T]) notFoundHandler(res http.ResponseWriter, req *http.Reque
return 404, i18n.NewError(req.Context(), i18n.Msg404NotFound)
}

func (as *apiServer[T]) emptyJSONHandler(res http.ResponseWriter, _ *http.Request) (status int, err error) {
res.Header().Add("Content-Type", "application/json")
return 200, nil
}

func (as *apiServer[T]) createMonitoringMuxRouter(ctx context.Context) *mux.Router {
r := mux.NewRouter().UseEncodedPath()
hf := as.handlerFactory() // TODO separate factory for monitoring ??
Expand All @@ -310,9 +318,14 @@ func (as *apiServer[T]) createMonitoringMuxRouter(ctx context.Context) *mux.Rout
panic(err)
}
r.Path(as.metricsPath).Handler(h)
r.HandleFunc(as.livenessPath, hf.APIWrapper(as.emptyJSONHandler))

for _, route := range as.MonitoringRoutes {
r.HandleFunc(route.Path, as.routeHandler(hf, route)).Methods(route.Method)
path := route.Path
if !strings.HasPrefix(route.Path, "/") {
path = fmt.Sprintf("/%s", route.Path)
}
r.HandleFunc(path, as.routeHandler(hf, route)).Methods(route.Method)
}

r.NotFoundHandler = hf.APIWrapper(as.notFoundHandler)
Expand Down
6 changes: 4 additions & 2 deletions pkg/ffapi/apiserver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
)

var (
ConfMonitoringServerEnabled = "enabled"
ConfMonitoringServerMetricsPath = "metricsPath"
ConfMonitoringServerEnabled = "enabled"
ConfMonitoringServerMetricsPath = "metricsPath"
ConfMonitoringServerLivenessPath = "livenessPath"

ConfAPIDefaultFilterLimit = "defaultFilterLimit"
ConfAPIMaxFilterLimit = "maxFilterLimit"
Expand All @@ -49,4 +50,5 @@ func InitAPIServerConfig(apiConfig, monitoringConfig, corsConfig config.Section)
httpserver.InitHTTPConfig(monitoringConfig, 6000)
monitoringConfig.AddKnownKey(ConfMonitoringServerEnabled, true)
monitoringConfig.AddKnownKey(ConfMonitoringServerMetricsPath, "/metrics")
monitoringConfig.AddKnownKey(ConfMonitoringServerLivenessPath, "/livez")
}

0 comments on commit 253fa7e

Please sign in to comment.