This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
The following commit(s) were added to refs/heads/master by this push:
new eea2b79 Support customize the metrics listen url (#1163)
eea2b79 is described below
commit eea2b791a79a3ec8e069ccab41676b9bc016ee75
Author: little-cui <[email protected]>
AuthorDate: Thu Oct 28 20:52:59 2021 +0800
Support customize the metrics listen url (#1163)
---
docs/user-guides/metrics.md | 23 +++++-
etc/conf/app.yaml | 3 +
server/api.go | 36 ++++-----
server/metrics/{connection.go => pubsub.go} | 0
server/rest/metrics/{prometheus.go => metrics.go} | 12 ++-
server/rest/metrics/server.go | 96 +++++++++++++++++++++++
server/server.go | 2 +-
7 files changed, 147 insertions(+), 25 deletions(-)
diff --git a/docs/user-guides/metrics.md b/docs/user-guides/metrics.md
index bc394ce..68e1e3d 100644
--- a/docs/user-guides/metrics.md
+++ b/docs/user-guides/metrics.md
@@ -1,4 +1,25 @@
-# All Metrics
+# Metrics
+
+---
+
+## How to export the metrics
+
+Service-Center is compatible with the [Prometheus](https://prometheus.io/)
standard.
+By default, the full metrics can be collected by accessing the `/metrics` API
through the `30100` port.
+
+If you want to customize the metrics configuration.
+```yaml
+metrics:
+ enable: true # enable to start metrics gather
+ interval: 30s # the duration of collection
+ exporter: prometheus # use the prometheus exporter
+ prometheus:
+ # optional, listen another ip-port and path if set, e.g.
http://127.0.0.1:80/other
+ listenURL:
+```
+
+
+## Summary
**FamilyName**: service_center
diff --git a/etc/conf/app.yaml b/etc/conf/app.yaml
index d4cdc3a..8dc992d 100644
--- a/etc/conf/app.yaml
+++ b/etc/conf/app.yaml
@@ -172,6 +172,9 @@ metrics:
enable: true
interval: 30s
exporter: prometheus
+ prometheus:
+ # optional, listen another ip-port and path if set, e.g.
http://127.0.0.1:80/other
+ listenURL:
tracing:
kind:
diff --git a/server/api.go b/server/api.go
index 4b00f22..235532e 100644
--- a/server/api.go
+++ b/server/api.go
@@ -53,7 +53,7 @@ func InitAPI() {
}
type APIServer struct {
- Listeners []string
+ HostPort string
HTTPServer *rest.Server
isClose bool
@@ -78,11 +78,11 @@ func (s *APIServer) MarkForked() {
s.forked = true
}
-func (s *APIServer) AddListener(ip, port string) {
+func (s *APIServer) Listen(ip, port string) {
if len(ip) == 0 {
return
}
- s.Listeners = append(s.Listeners, net.JoinHostPort(ip, port))
+ s.HostPort = net.JoinHostPort(ip, port)
}
func (s *APIServer) populateEndpoint(ipPort string) {
@@ -97,24 +97,22 @@ func (s *APIServer) populateEndpoint(ipPort string) {
}
func (s *APIServer) serve() (err error) {
- for i, addr := range s.Listeners {
- s.HTTPServer, err = rs.NewServer(addr)
- if err != nil {
+ s.HTTPServer, err = rs.NewServer(s.HostPort)
+ if err != nil {
+ return
+ }
+ log.Info(fmt.Sprintf("listen address: rest://%s",
s.HTTPServer.Listener.Addr().String()))
+
+ s.populateEndpoint(s.HTTPServer.Listener.Addr().String())
+
+ s.goroutine.Do(func(_ context.Context) {
+ err := s.HTTPServer.Serve()
+ if s.isClose {
return
}
- log.Info(fmt.Sprintf("listen address[%d]: rest://%s", i,
s.HTTPServer.Listener.Addr().String()))
-
- s.populateEndpoint(s.HTTPServer.Listener.Addr().String())
-
- s.goroutine.Do(func(_ context.Context) {
- err := s.HTTPServer.Serve()
- if s.isClose {
- return
- }
- log.Error(fmt.Sprintf("error to serve %s", addr), err)
- s.err <- err
- })
- }
+ log.Error(fmt.Sprintf("error to serve %s", s.HostPort), err)
+ s.err <- err
+ })
return
}
diff --git a/server/metrics/connection.go b/server/metrics/pubsub.go
similarity index 100%
rename from server/metrics/connection.go
rename to server/metrics/pubsub.go
diff --git a/server/rest/metrics/prometheus.go b/server/rest/metrics/metrics.go
similarity index 80%
rename from server/rest/metrics/prometheus.go
rename to server/rest/metrics/metrics.go
index 5e2869a..af486b8 100644
--- a/server/rest/metrics/prometheus.go
+++ b/server/rest/metrics/metrics.go
@@ -18,16 +18,20 @@
package metrics
import (
- promutil "github.com/apache/servicecomb-service-center/pkg/prometheus"
+ "github.com/apache/servicecomb-service-center/pkg/log"
"github.com/apache/servicecomb-service-center/server/config"
- "github.com/apache/servicecomb-service-center/server/rest"
)
-const exporterPrometheus = "prometheus"
+const (
+ exporterPrometheus = "prometheus"
+)
func init() {
if config.GetString("metrics.exporter", "") != exporterPrometheus {
return
}
- rest.RegisterServerHandler("/metrics", promutil.HTTPHandler())
+ err := ListenAndServe()
+ if err != nil {
+ log.Error("metrics server listen and serve failed", err)
+ }
}
diff --git a/server/rest/metrics/server.go b/server/rest/metrics/server.go
new file mode 100644
index 0000000..3a9d69b
--- /dev/null
+++ b/server/rest/metrics/server.go
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package metrics
+
+import (
+ "context"
+ "net/http"
+ "net/url"
+
+ "github.com/apache/servicecomb-service-center/pkg/log"
+ "github.com/apache/servicecomb-service-center/pkg/prometheus"
+ "github.com/apache/servicecomb-service-center/pkg/rest"
+ "github.com/apache/servicecomb-service-center/server/config"
+
"github.com/apache/servicecomb-service-center/server/plugin/security/tlsconf"
+ restsvc "github.com/apache/servicecomb-service-center/server/rest"
+ "github.com/go-chassis/foundation/gopool"
+)
+
+const (
+ DefaultAPIPath = "/metrics"
+)
+
+func ListenAndServe() error {
+ cfg, listenURL, err := getConfig()
+ if err != nil {
+ return err
+ }
+ if listenURL == "" {
+ restsvc.RegisterServerHandler(DefaultAPIPath,
prometheus.HTTPHandler())
+ return nil
+ }
+
+ srv := rest.NewServer(cfg)
+ if cfg.TLSConfig == nil {
+ err = srv.Listen()
+ } else {
+ err = srv.ListenTLS()
+ }
+ if err != nil {
+ return err
+ }
+ log.Info("listen metrics address: " + listenURL)
+
+ gopool.Go(func(_ context.Context) {
+ if err := srv.Serve(); err != nil {
+ log.Error("metrics server serve failed", err)
+ return
+ }
+ })
+ return nil
+}
+
+func getConfig() (*rest.ServerConfig, string, error) {
+ listenURL := config.GetString("metrics.prometheus.listenURL", "")
+ if listenURL == "" {
+ return nil, "", nil
+ }
+ u, err := url.Parse(listenURL)
+ if err != nil {
+ log.Error("parse metrics server listenURL failed", err)
+ return nil, "", err
+ }
+ apiPath := DefaultAPIPath
+ if u.Path != "" {
+ apiPath = u.Path
+ }
+ mux := http.NewServeMux()
+ mux.Handle(apiPath, prometheus.HTTPHandler())
+
+ cfg := rest.DefaultServerConfig()
+ cfg.Addr = u.Host
+ cfg.Handler = mux
+ if u.Scheme != "http" {
+ cfg.TLSConfig, err = tlsconf.ServerConfig()
+ if err != nil {
+ log.Error("get metrics server tls config failed", err)
+ return nil, "", err
+ }
+ }
+ return cfg, listenURL, nil
+}
diff --git a/server/server.go b/server/server.go
index 4732cd2..4e4dfc3 100644
--- a/server/server.go
+++ b/server/server.go
@@ -230,7 +230,7 @@ func (s *ServiceCenterServer) startServices() {
func (s *ServiceCenterServer) startAPIService() {
core.Instance.HostName = util.HostName()
- s.APIServer.AddListener(s.Endpoint.Host, s.Endpoint.Port)
+ s.APIServer.Listen(s.Endpoint.Host, s.Endpoint.Port)
s.APIServer.Start()
}