Browse Source

feat(server): allow to use the same port for REST and prometheus

Signed-off-by: Jiyong Huang <huangjy@emqx.io>
Jiyong Huang 2 years ago
parent
commit
d5b9d3a52c

+ 3 - 1
docs/en_US/operation/config/configuration_file.md

@@ -86,7 +86,9 @@ basic:
   prometheus: true
   prometheusPort: 20499
 ```
-For such a default configuration, eKuiper will export metrics and serve prometheus at `http://localhost:20499/metrics`
+For such a default configuration, eKuiper will export metrics and serve prometheus at `http://localhost:20499/metrics`.
+
+The prometheus port can be the same as the eKuiper REST API port. If so, both service will be served on the same server.
 
 ## Pluginhosts Configuration
 

+ 3 - 1
docs/zh_CN/operation/config/configuration_file.md

@@ -87,7 +87,9 @@ basic:
   prometheus: true
   prometheusPort: 20499
 ```
-在如上默认配置中,eKuiper 暴露于 Prometheusd 运行指标可通过 `http://localhost:20499/metrics` 访问。
+在如上默认配置中,eKuiper 暴露于 Prometheus 运行指标可通过 `http://localhost:20499/metrics` 访问。
+
+Prometheus 端口可设置为与 eKuiper 的 REST 服务端口相同。这样设置的话,两个服务将运行在同一个 HTTP 服务中。
 
 ## Pluginhosts 配置
 

+ 40 - 19
internal/server/prome_init.go

@@ -20,6 +20,7 @@ package server
 import (
 	"context"
 	"fmt"
+	"github.com/gorilla/mux"
 	"github.com/lf-edge/ekuiper/internal/conf"
 	"github.com/prometheus/client_golang/prometheus/promhttp"
 	"net/http"
@@ -27,38 +28,58 @@ import (
 )
 
 func init() {
-	servers["prometheus"] = promeComp{}
+	p := promeComp{}
+	servers["prometheus"] = p
+	components["prometheus"] = p
 }
 
 type promeComp struct {
 	s *http.Server
 }
 
+func (p promeComp) register() {
+	// Do nothing
+}
+
+func (p promeComp) rest(r *mux.Router) {
+	portPrometheus := conf.Config.Basic.PrometheusPort
+	portRest := conf.Config.Basic.RestPort
+	if portPrometheus == portRest {
+		r.Handle("/metrics", promhttp.Handler())
+		msg := fmt.Sprintf("Register prometheus metrics to http://localhost:%d/metrics", portPrometheus)
+		logger.Infof(msg)
+		fmt.Println(msg)
+	}
+}
+
 func (p promeComp) serve() {
-	//Start prometheus service
 	if conf.Config.Basic.Prometheus {
+		//Start prometheus service
 		portPrometheus := conf.Config.Basic.PrometheusPort
 		if portPrometheus <= 0 {
 			logger.Fatal("Miss configuration prometheusPort")
 		}
-		mux := http.NewServeMux()
-		mux.Handle("/metrics", promhttp.Handler())
-		srvPrometheus := &http.Server{
-			Addr:         fmt.Sprintf("0.0.0.0:%d", portPrometheus),
-			WriteTimeout: time.Second * 15,
-			ReadTimeout:  time.Second * 15,
-			IdleTimeout:  time.Second * 60,
-			Handler:      mux,
-		}
-		go func() {
-			if err := srvPrometheus.ListenAndServe(); err != nil && err != http.ErrServerClosed {
-				logger.Fatal("Listen prometheus error: ", err)
+		portRest := conf.Config.Basic.RestPort
+		if portPrometheus != portRest {
+			mux := http.NewServeMux()
+			mux.Handle("/metrics", promhttp.Handler())
+			srvPrometheus := &http.Server{
+				Addr:         fmt.Sprintf("0.0.0.0:%d", portPrometheus),
+				WriteTimeout: time.Second * 15,
+				ReadTimeout:  time.Second * 15,
+				IdleTimeout:  time.Second * 60,
+				Handler:      mux,
 			}
-		}()
-		p.s = srvPrometheus
-		msg := fmt.Sprintf("Serving prometheus metrics on port http://localhost:%d/metrics", portPrometheus)
-		logger.Infof(msg)
-		fmt.Println(msg)
+			go func() {
+				if err := srvPrometheus.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+					logger.Fatal("Listen prometheus error: ", err)
+				}
+			}()
+			p.s = srvPrometheus
+			msg := fmt.Sprintf("Serving prometheus metrics on port http://localhost:%d/metrics", portPrometheus)
+			logger.Infof(msg)
+			fmt.Println(msg)
+		}
 	}
 }