Skip to content
Permalink
Browse files

Run metrics server in a separate method

This commit adds changes to run metrics server in a separate method and
also removed port 8082 from exposed port a/c to review comments.

It also uses a smaller static timeout value for new server.

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
  • Loading branch information...
viveksyngh authored and alexellis committed Feb 15, 2019
1 parent 90ddd56 commit 54eda5ec0d102b4132a4ee814f54a99991691a50
Showing with 38 additions and 19 deletions.
  1. +0 −1 docker-compose.arm64.yml
  2. +0 −1 docker-compose.armhf.yml
  3. +0 −1 docker-compose.yml
  4. +38 −16 gateway/server.go
@@ -3,7 +3,6 @@ services:
gateway:
ports:
- 8080:8080
- 8082:8082
image: openfaas/gateway:0.11.0-arm64
networks:
- functions
@@ -3,7 +3,6 @@ services:
gateway:
ports:
- 8080:8080
- 8082:8082
image: openfaas/gateway:0.11.0-armhf
networks:
- functions
@@ -3,7 +3,6 @@ services:
gateway:
ports:
- 8080:8080
- 8082:8082
image: openfaas/gateway:0.11.1
networks:
- functions
@@ -206,22 +206,8 @@ func main() {
r.PathPrefix("/ui/").Handler(uiHandler).Methods(http.MethodGet)
}

metricsHandler := metrics.PrometheusHandler()

//Create a new server to serve /metrics endpoint
go func() {
routes := mux.NewRouter()
routes.Handle("/metrics", metricsHandler)
port := 8082
s := &http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadTimeout: config.ReadTimeout,
WriteTimeout: config.WriteTimeout,
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
Handler: routes,
}
log.Fatal(s.ListenAndServe())
}()
//Start metrics server in a goroutine
go runMetricsServer()

r.HandleFunc("/healthz", handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer)).Methods(http.MethodGet)

@@ -239,3 +225,39 @@ func main() {

log.Fatal(s.ListenAndServe())
}

//runMetricsServer Listen on a separate HTTP port for Prometheus metrics to keep this accessible from
// the internal network only.
func runMetricsServer() {
metricsHandler := metrics.PrometheusHandler()
router := mux.NewRouter()
router.Handle("/metrics", metricsHandler)
router.HandleFunc("/healthz", healthzHandler)

port := 8082
readTimeout := time.Duration(30) * time.Second
writeTimeout := time.Duration(30) * time.Second

s := &http.Server{
Addr: fmt.Sprintf(":%d", port),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: http.DefaultMaxHeaderBytes,
Handler: router,
}

log.Fatal(s.ListenAndServe())
}

func healthzHandler(w http.ResponseWriter, r *http.Request) {

switch r.Method {
case http.MethodGet:
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
break

default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

0 comments on commit 54eda5e

Please sign in to comment.
You can’t perform that action at this time.