Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add flags metrics and healthz #51

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading