Skip to content

Commit f2cdce1

Browse files
fix: move health/readiness probes to its own port so they are not logged
1 parent a1f1b6c commit f2cdce1

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

chart/templates/deployment.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ spec:
5353
- name: metrics
5454
containerPort: {{ .Values.configuration.metrics.port }}
5555
protocol: TCP
56+
- name: probes
57+
containerPort: {{ .Values.configuration.probes_port }}
58+
protocol: TCP
5659
livenessProbe:
5760
httpGet:
5861
path: /
59-
port: http
62+
port: probes
6063
readinessProbe:
6164
httpGet:
6265
path: /
63-
port: http
66+
port: probes
6467
resources:
6568
{{- toYaml .Values.resources | nindent 12 }}
6669
{{- with .Values.nodeSelector }}

chart/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ configuration:
2525
# Whether to print http logs to stdout
2626
http_logs: true
2727

28+
# The port the health/readiness probes listen on
29+
probes_port: 9744
30+
2831
# Whether to expose prompage metrics
2932
metrics:
3033
enabled: false

cmd/serve/serve.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"syscall"
1111

1212
"github.com/henrywhitaker3/prompage/internal/app"
13+
"github.com/henrywhitaker3/prompage/internal/health"
1314
"github.com/henrywhitaker3/prompage/internal/http"
1415
"github.com/henrywhitaker3/prompage/internal/metrics"
1516
"github.com/spf13/cobra"
@@ -34,6 +35,7 @@ func NewServeCommand(app *app.App) *cobra.Command {
3435
http := http.NewHttp(app, cache)
3536
metrics := metrics.NewServer(app.Config.Metrics.Port)
3637
metrics.Init()
38+
probes := health.NewHealth(app.Config.ProbesPort)
3739

3840
go cache.Work(ctx)
3941
go func() {
@@ -54,9 +56,18 @@ func NewServeCommand(app *app.App) *cobra.Command {
5456
}
5557
}()
5658
}
59+
go func() {
60+
if err := probes.Start(); err != nil {
61+
if !errors.Is(err, stdhttp.ErrServerClosed) {
62+
fmt.Println(fmt.Errorf("probes server failed: %v", err))
63+
cancel()
64+
}
65+
}
66+
}()
5767

5868
<-ctx.Done()
5969
metrics.Stop(context.Background())
70+
probes.Stop(context.Background())
6071
return http.Stop(context.Background())
6172
},
6273
}

internal/config/config.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ type Datasource struct {
5656
}
5757

5858
type Config struct {
59-
Port int `yaml:"port"`
60-
HttpLogs *bool `yaml:"http_logs"`
61-
Metrics Metrics `yaml:"metrics"`
62-
Services []Service `yaml:"services"`
59+
Port int `yaml:"port"`
60+
ProbesPort int `yaml:"probes_port"`
61+
HttpLogs *bool `yaml:"http_logs"`
62+
Metrics Metrics `yaml:"metrics"`
63+
Services []Service `yaml:"services"`
6364

6465
Datasources []Datasource `yaml:"datasources"`
6566

@@ -101,6 +102,9 @@ func setDefaults(conf *Config) {
101102
if conf.Metrics.Port == 0 {
102103
conf.Metrics.Port = 9743
103104
}
105+
if conf.ProbesPort == 0 {
106+
conf.ProbesPort = 9744
107+
}
104108
if conf.HttpLogs == nil {
105109
en := true
106110
conf.HttpLogs = &en

internal/health/health.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package health
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/labstack/echo/v4"
9+
)
10+
11+
type Health struct {
12+
e *echo.Echo
13+
port int
14+
15+
ready bool
16+
healthy bool
17+
}
18+
19+
// TODO: add an actual health/readiness check so these aren't meaningless
20+
21+
func NewHealth(port int) *Health {
22+
e := echo.New()
23+
e.HideBanner = true
24+
h := &Health{
25+
e: e,
26+
healthy: true,
27+
ready: true,
28+
port: port,
29+
}
30+
31+
h.e.GET("/healthz", h.healthHandler())
32+
h.e.GET("/readyz", h.readyHandler())
33+
34+
return h
35+
}
36+
37+
func (h *Health) Start() error {
38+
return h.e.Start(fmt.Sprintf(":%d", h.port))
39+
}
40+
41+
func (h *Health) Stop(ctx context.Context) error {
42+
return h.e.Shutdown(ctx)
43+
}
44+
45+
func (h *Health) healthHandler() echo.HandlerFunc {
46+
return func(c echo.Context) error {
47+
if h.healthy {
48+
return c.String(http.StatusOK, "HEALTHY")
49+
}
50+
return c.String(http.StatusServiceUnavailable, "UNHEALTHY")
51+
}
52+
}
53+
54+
func (h *Health) readyHandler() echo.HandlerFunc {
55+
return func(c echo.Context) error {
56+
if h.ready {
57+
return c.String(http.StatusOK, "READY")
58+
}
59+
return c.String(http.StatusServiceUnavailable, "NOT READY")
60+
}
61+
}

0 commit comments

Comments
 (0)