Skip to content

Commit 44f588d

Browse files
authored
Merge pull request #19 from stefanprodan/metrics-port
Add option to run the metrics exporter on a different port
2 parents 3301f6f + 951d82a commit 44f588d

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

cmd/podinfo/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func main() {
2121
// flags definition
2222
fs := pflag.NewFlagSet("default", pflag.ContinueOnError)
2323
fs.Int("port", 9898, "port")
24+
fs.Int("port-metrics", 0, "metrics port")
2425
fs.String("level", "info", "log level debug, info, warn, error, flat or panic")
2526
fs.String("backend-url", "", "backend service URL")
2627
fs.Duration("http-client-timeout", 2*time.Minute, "client timeout duration")

pkg/api/server.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Config struct {
3333
DataPath string `mapstructure:"data-path"`
3434
ConfigPath string `mapstructure:"config-path"`
3535
Port string `mapstructure:"port"`
36+
PortMetrics int `mapstructure:"port-metrics"`
3637
Hostname string `mapstructure:"hostname"`
3738
RandomDelay bool `mapstructure:"random-delay"`
3839
RandomError bool `mapstructure:"random-error"`
@@ -96,6 +97,7 @@ func (s *Server) registerMiddlewares() {
9697
}
9798

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

100102
s.registerHandlers()
101103
s.registerMiddlewares()
@@ -157,6 +159,24 @@ func (s *Server) ListenAndServe(stopCh <-chan struct{}) {
157159
}
158160
}
159161

162+
func (s *Server) startMetricsServer() {
163+
if s.config.PortMetrics > 0 {
164+
mux := http.DefaultServeMux
165+
mux.Handle("/metrics", promhttp.Handler())
166+
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
167+
w.WriteHeader(http.StatusOK)
168+
w.Write([]byte("OK"))
169+
})
170+
171+
srv := &http.Server{
172+
Addr: fmt.Sprintf(":%v", s.config.PortMetrics),
173+
Handler: mux,
174+
}
175+
176+
srv.ListenAndServe()
177+
}
178+
}
179+
160180
func (s *Server) printRoutes() {
161181
s.router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
162182
pathTemplate, err := route.GetPathTemplate()

pkg/version/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package version
22

3-
var VERSION = "1.5.1"
3+
var VERSION = "1.6.0"
44
var REVISION = "unknown"

0 commit comments

Comments
 (0)