diff --git a/cmd/podinfo/main.go b/cmd/podinfo/main.go index 61dd7f8..e37a1b1 100644 --- a/cmd/podinfo/main.go +++ b/cmd/podinfo/main.go @@ -21,6 +21,7 @@ func main() { // flags definition fs := pflag.NewFlagSet("default", pflag.ContinueOnError) fs.Int("port", 9898, "port") + fs.Int("port-metrics", 0, "metrics port") fs.String("level", "info", "log level debug, info, warn, error, flat or panic") fs.String("backend-url", "", "backend service URL") fs.Duration("http-client-timeout", 2*time.Minute, "client timeout duration") diff --git a/pkg/api/server.go b/pkg/api/server.go index 1d33041..2239c7e 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -33,6 +33,7 @@ type Config struct { DataPath string `mapstructure:"data-path"` ConfigPath string `mapstructure:"config-path"` Port string `mapstructure:"port"` + PortMetrics int `mapstructure:"port-metrics"` Hostname string `mapstructure:"hostname"` RandomDelay bool `mapstructure:"random-delay"` RandomError bool `mapstructure:"random-error"` @@ -96,6 +97,7 @@ func (s *Server) registerMiddlewares() { } func (s *Server) ListenAndServe(stopCh <-chan struct{}) { + go s.startMetricsServer() s.registerHandlers() s.registerMiddlewares() @@ -157,6 +159,24 @@ func (s *Server) ListenAndServe(stopCh <-chan struct{}) { } } +func (s *Server) startMetricsServer() { + if s.config.PortMetrics > 0 { + mux := http.DefaultServeMux + mux.Handle("/metrics", promhttp.Handler()) + mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK")) + }) + + srv := &http.Server{ + Addr: fmt.Sprintf(":%v", s.config.PortMetrics), + Handler: mux, + } + + srv.ListenAndServe() + } +} + func (s *Server) printRoutes() { s.router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error { pathTemplate, err := route.GetPathTemplate() diff --git a/pkg/version/version.go b/pkg/version/version.go index 13cf3f8..951e20e 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,4 +1,4 @@ package version -var VERSION = "1.5.1" +var VERSION = "1.6.0" var REVISION = "unknown"