Skip to content

Commit

Permalink
Merge pull request #19 from stefanprodan/metrics-port
Browse files Browse the repository at this point in the history
Add option to run the metrics exporter on a different port
  • Loading branch information
stefanprodan committed Jun 15, 2019
2 parents 3301f6f + 951d82a commit 44f588d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/podinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -96,6 +97,7 @@ func (s *Server) registerMiddlewares() {
}

func (s *Server) ListenAndServe(stopCh <-chan struct{}) {
go s.startMetricsServer()

s.registerHandlers()
s.registerMiddlewares()
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package version

var VERSION = "1.5.1"
var VERSION = "1.6.0"
var REVISION = "unknown"

0 comments on commit 44f588d

Please sign in to comment.