Allow exposing custom error pages /metrics on different port
Signed-off-by: Ricardo Lopes <ricardoapl.dev@gmail.com>
This commit is contained in:
parent
54990cea7f
commit
b502b5fcdc
1 changed files with 46 additions and 11 deletions
|
@ -72,6 +72,13 @@ const (
|
||||||
// IsExposeSignalsVar is the name of the environment variable indicating
|
// IsExposeSignalsVar is the name of the environment variable indicating
|
||||||
// whether or not to expose signals such as /metrics and /healthz.
|
// whether or not to expose signals such as /metrics and /healthz.
|
||||||
IsExposeSignalsVar = "IS_EXPOSE_SIGNALS"
|
IsExposeSignalsVar = "IS_EXPOSE_SIGNALS"
|
||||||
|
|
||||||
|
// ExposeSignalsPortVar is the name of the environment variable indicating
|
||||||
|
// the port on which to expose signals such as /metrics and /healthz.
|
||||||
|
ExposeSignalsPortVar = "EXPOSE_SIGNALS_PORT"
|
||||||
|
|
||||||
|
// CustomErrorPagesPort is the port on which to listen to serve custom error pages.
|
||||||
|
CustomErrorPagesPort = "8080"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -98,23 +105,51 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exposeSignalsPort := "8080"
|
||||||
|
if os.Getenv(ExposeSignalsPortVar) != "" {
|
||||||
|
exposeSignalsPort = os.Getenv(ExposeSignalsPortVar)
|
||||||
|
}
|
||||||
|
|
||||||
var mux *http.ServeMux
|
var mux *http.ServeMux
|
||||||
if isExposeSignals {
|
if isExposeSignals {
|
||||||
|
if exposeSignalsPort == CustomErrorPagesPort {
|
||||||
mux = http.DefaultServeMux
|
mux = http.DefaultServeMux
|
||||||
|
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
|
||||||
mux.Handle("/metrics", promhttp.Handler())
|
mux.Handle("/metrics", promhttp.Handler())
|
||||||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), mux)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mux = http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
|
||||||
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
go func() {
|
||||||
|
err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), mux)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
err := http.ListenAndServe(fmt.Sprintf(":%s", exposeSignalsPort), nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Use a new ServerMux because expvar HTTP handler registers itself against DefaultServerMux
|
// Use a new ServerMux because expvar HTTP handler registers itself against DefaultServerMux
|
||||||
// as a consequence of importing it in client_golang/prometheus.
|
// as a consequence of importing it in client_golang/prometheus.
|
||||||
mux = http.NewServeMux()
|
mux = http.NewServeMux()
|
||||||
}
|
|
||||||
|
|
||||||
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
|
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
|
||||||
err := http.ListenAndServe(":8080", mux)
|
err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), mux)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue