Skip to content

Commit

Permalink
chore: add flags metrics and healthz (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod authored Oct 17, 2024
1 parent ef9fda6 commit 6d72da4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"--fix"
],
"go.lintOnSave": "package"
}
}
7 changes: 3 additions & 4 deletions internal/controller/resources_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func buildKimupArgs(extra v1alpha1.KimupExtraSpec) (args []string) {
args = []string{}
if extra.Healthz.Enabled {
// enable healthz
// TODO
// args = append(args, fmt.Sprintf("--%s", models.HealthzFlagName))
args = append(args, fmt.Sprintf("--%s", models.HealthzFlagName))

// set the healthz port
healthzPort := extra.Healthz.Port
Expand All @@ -40,7 +39,7 @@ func buildKimupArgs(extra v1alpha1.KimupExtraSpec) (args []string) {

if extra.Metrics.Enabled {
// enable metrics
// args = append(args, fmt.Sprintf("--%s", models.MetricsFlagName))
args = append(args, fmt.Sprintf("--%s", models.MetricsFlagName))

// set the metrics port
metricsPort := extra.Metrics.Port
Expand All @@ -60,7 +59,7 @@ func buildKimupArgs(extra v1alpha1.KimupExtraSpec) (args []string) {
}

// TODO
// args = append(args, fmt.Sprintf("--%s=%s", models.LogLevelFlagName, extra.LogLevel))
args = append(args, fmt.Sprintf("--%s=%s", models.LogLevelFlagName, extra.LogLevel))

return args
}
Expand Down
29 changes: 22 additions & 7 deletions internal/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,15 @@ var (
)

func init() {
flag.StringVar(&healthzPort, models.HealthzPortFlagName, models.HealthzDefaultAddr, fmt.Sprintf("Healthz server port. (ex: %s)", models.HealthzDefaultAddr))
flag.StringVar(&healthzPath, models.HealthzPathFlagName, models.HealthzDefaultPath, fmt.Sprintf("Healthz server path. (ex: %s)", models.HealthzDefaultPath))
flag.StringVar(&metricsPort, models.MetricsPortFlagName, models.MetricsDefaultAddr, fmt.Sprintf("Metrics server port. (ex: %s)", models.MetricsDefaultAddr))
flag.StringVar(&metricsPath, models.MetricsPathFlagName, models.HealthzDefaultPath, fmt.Sprintf("Metrics server path. (ex: %s)", models.MetricsDefaultPath))
// * Healthz
flag.Bool(models.HealthzFlagName, false, "Enable the healthz server.")
flag.StringVar(&healthzPort, models.HealthzPortFlagName, models.HealthzDefaultAddr, "Healthz server port.")
flag.StringVar(&healthzPath, models.HealthzPathFlagName, models.HealthzDefaultPath, "Healthz server path.")

// * Metrics
flag.Bool(models.MetricsFlagName, false, "Enable the metrics server.")
flag.StringVar(&metricsPort, models.MetricsPortFlagName, models.MetricsDefaultAddr, "Metrics server port.")
flag.StringVar(&metricsPath, models.MetricsPathFlagName, models.HealthzDefaultPath, "Metrics server path.")
}

// Function to initialize application, return app struct and a func waitgroup.
Expand All @@ -92,9 +97,14 @@ func Init(ctx context.Context, opts ...OptionServer) (InterfaceServer, CancelFun
wg: &sync.WaitGroup{},
}

a.list["health"] = a.createHealth()
WithCustomHandlerForHealth(DefaultFuncHealthz)(a)
a.list["metrics"] = a.createMetrics()
if flag.Lookup(models.HealthzFlagName).Value.String() == "true" {
a.list["health"] = a.createHealth()
WithCustomHandlerForHealth(DefaultFuncHealthz)(a)
}

if flag.Lookup(models.MetricsFlagName).Value.String() == "true" {
a.list["metrics"] = a.createMetrics()
}

// create a new server for health
for _, opt := range opts {
Expand Down Expand Up @@ -263,6 +273,11 @@ func (a *app) checkIfPortIsAlreadyUsed(s *server) bool {
// and the endpoint path (e.g. /healthz)
func WithCustomHandlerForHealth(req HealthzFunc) OptionServer {
return func(a *app) {
// Prevent panic if the health server is not enabled
if _, ok := a.list["health"]; !ok {
return
}

a.list["health"].Config.Get(healthzPath, func(w http.ResponseWriter, r *http.Request) {
ok, err := req()
if err != nil {
Expand Down

0 comments on commit 6d72da4

Please sign in to comment.