Skip to content

Commit

Permalink
fixup: bind metrics in separate func
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert committed Feb 27, 2023
1 parent 6e9c8b8 commit 6cfff7f
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions pkg/service/connect_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,8 @@ func (s *ConnectService) setupServer(svcConf Configuration) (net.Listener, error
Logger: s.Logger,
})
h := Handler("", mdlw, mux)
go func() {
s.Logger.Info(fmt.Sprintf("metrics and probes listening at %d", s.ConnectServiceConfiguration.MetricsPort))
server := &http.Server{
Addr: fmt.Sprintf(":%d", s.ConnectServiceConfiguration.MetricsPort),
ReadHeaderTimeout: 3 * time.Second,
}
server.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/healthz":
w.WriteHeader(http.StatusOK)
case "/readyz":
if svcConf.ReadinessProbe() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusPreconditionFailed)
}
case "/metrics":
promhttp.Handler().ServeHTTP(w, r)
default:
w.WriteHeader(http.StatusNotFound)
}
})
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}()

go bindMetrics(s, svcConf)

if s.ConnectServiceConfiguration.ServerCertPath != "" && s.ConnectServiceConfiguration.ServerKeyPath != "" {
handler = s.newCORS().Handler(h)
Expand Down Expand Up @@ -392,6 +367,34 @@ func (s *ConnectService) newCORS() *cors.Cors {
})
}

func bindMetrics(s *ConnectService, svcConf Configuration) {
s.Logger.Info(fmt.Sprintf("metrics and probes listening at %d", s.ConnectServiceConfiguration.MetricsPort))
server := &http.Server{
Addr: fmt.Sprintf(":%d", s.ConnectServiceConfiguration.MetricsPort),
ReadHeaderTimeout: 3 * time.Second,
}
server.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/healthz":
w.WriteHeader(http.StatusOK)
case "/readyz":
if svcConf.ReadinessProbe() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusPreconditionFailed)
}
case "/metrics":
promhttp.Handler().ServeHTTP(w, r)
default:
w.WriteHeader(http.StatusNotFound)
}
})
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}

func errFormat(err error) error {
switch err.Error() {
case model.FlagNotFoundErrorCode:
Expand Down

0 comments on commit 6cfff7f

Please sign in to comment.