Skip to content

Commit 4fc6319

Browse files
author
Jeff Peeler
committed
fix(metrics): add logging and separate muxer in OLM
Also, don't use default muxer for health either. Although it's pretty low risk, technically vendored packages can modify the global default mux.
1 parent eef76a2 commit 4fc6319

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

cmd/olm/main.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,40 @@ func main() {
123123
if *tlsCertPath != "" && *tlsKeyPath == "" || *tlsCertPath == "" && *tlsKeyPath != "" {
124124
logger.Warn("both --tls-key and --tls-crt must be provided for TLS to be enabled, falling back to non-https")
125125
} else if *tlsCertPath == "" && *tlsKeyPath == "" {
126-
logger.Info("TLS keys not set, using non-https")
126+
logger.Info("TLS keys not set, using non-https for metrics")
127127
} else {
128+
logger.Info("TLS keys set, using https for metrics")
128129
useTLS = true
129130
}
130131

131132
// Serve a health check.
132-
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
133+
healthMux := http.NewServeMux()
134+
healthMux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
133135
w.WriteHeader(http.StatusOK)
134136
})
135-
go http.ListenAndServe(":8080", nil)
137+
go func() {
138+
err := http.ListenAndServe(":8080", healthMux)
139+
if err != nil {
140+
logger.Errorf("Health serving failed: %v", err)
141+
}
142+
}()
136143

137-
http.Handle("/metrics", promhttp.Handler())
144+
metricsMux := http.NewServeMux()
145+
metricsMux.Handle("/metrics", promhttp.Handler())
138146
if useTLS {
139-
go http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, nil)
147+
go func() {
148+
err := http.ListenAndServeTLS(":8081", *tlsCertPath, *tlsKeyPath, metricsMux)
149+
if err != nil {
150+
logger.Errorf("Metrics (https) serving failed: %v", err)
151+
}
152+
}()
140153
} else {
141-
go http.ListenAndServe(":8081", nil)
154+
go func() {
155+
err := http.ListenAndServe(":8081", metricsMux)
156+
if err != nil {
157+
logger.Errorf("Metrics (http) serving failed: %v", err)
158+
}
159+
}()
142160
}
143161

144162
ready, done, sync := operator.Run(stopCh)

0 commit comments

Comments
 (0)