Merge pull request #19 from stefanprodan/metrics-port

Add option to run the metrics exporter on a different port
This commit is contained in:
Stefan Prodan
2019-06-15 17:14:01 +03:00
committed by GitHub
3 changed files with 22 additions and 1 deletions

View File

@@ -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")

View File

@@ -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()

View File

@@ -1,4 +1,4 @@
package version
var VERSION = "1.5.1"
var VERSION = "1.6.0"
var REVISION = "unknown"