Skip to content

Commit

Permalink
add endpoints for kubernetes probes (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetr authored Dec 3, 2024
1 parent f25a232 commit dfede31
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
5 changes: 3 additions & 2 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,9 @@ type OutboundProxyConfig struct {
}

type MetricsConfig struct {
Disabled bool `mapstructure:"disabled" json:"disabled"`
Addr string `mapstructure:"addr" json:"addr" default:":9000"`
Disabled bool `mapstructure:"disabled" json:"disabled"`
Addr string `mapstructure:"addr" json:"addr" default:":9000"`
HealthcheckGracePeriodSeconds int `mapstructure:"healthcheckGracePeriodSeconds" json:"healthcheckGracePeriodSeconds" validate:"gte=0" default:"10"`
}

type Config struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"golang.zx2c4.com/wireguard/tun/netstack"
)

var hasSeenSuccessfulHeartbeat bool
var lastSuccessfulHeartbeat time.Time

func (config *HeartbeatConfig) Start(tnet *netstack.Net, userAgent string) (func(), error) {
ticker := time.NewTicker(time.Duration(config.IntervalSeconds) * time.Second)
done := make(chan bool)
Expand Down Expand Up @@ -51,6 +54,8 @@ func (config *HeartbeatConfig) Start(tnet *netstack.Net, userAgent string) (func
failures = 0
heartbeatSuccessCounter.Inc()
heartbeatLastSuccessTimestamp.SetToCurrentTime()
hasSeenSuccessfulHeartbeat = true
lastSuccessfulHeartbeat = time.Now()
return true
}
}
Expand Down
28 changes: 27 additions & 1 deletion pkg/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package pkg

import (
"fmt"
"io"
"net"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -47,7 +49,31 @@ func StartMetrics(config *Config) error {
prometheus.MustRegister(logEventsCounter, heartbeatCounter, heartbeatLastSuccessTimestamp, proxyInFlightGauge, proxyCounter)

promHandler := promhttp.Handler()
httpServer := &http.Server{Addr: config.Metrics.Addr, Handler: promHandler}
mux := http.NewServeMux()
mux.Handle("/metrics", promHandler)
mux.HandleFunc("/probes/startup", func(w http.ResponseWriter, r *http.Request) {
if hasSeenSuccessfulHeartbeat {
w.WriteHeader(200)
io.WriteString(w, "OK")
} else {
w.WriteHeader(503)
io.WriteString(w, "Not Ready")
}
})
mux.HandleFunc("/probes/readiness", func(w http.ResponseWriter, r *http.Request) {
timeSinceLastHeartbeat := time.Since(lastSuccessfulHeartbeat)
heartbeatCutoff := time.Second * time.Duration(config.Inbound.Heartbeat.IntervalSeconds+config.Metrics.HealthcheckGracePeriodSeconds)

if timeSinceLastHeartbeat < heartbeatCutoff {
w.WriteHeader(200)
io.WriteString(w, "OK")
} else {
w.WriteHeader(503)
io.WriteString(w, fmt.Sprintf("Not Ready: no successful heartbeat within %v", heartbeatCutoff))
}
})

httpServer := &http.Server{Addr: config.Metrics.Addr, Handler: mux}
listener, err := net.Listen("tcp", httpServer.Addr)
if err != nil {
return fmt.Errorf("failed to start external metrics server: %w", err)
Expand Down

0 comments on commit dfede31

Please sign in to comment.