You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ti...@apache.org on 2021/05/10 01:35:22 UTC

[servicecomb-service-center] branch master updated: Fix: add metrics config (#970)

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 bfad184  Fix: add metrics config (#970)
bfad184 is described below

commit bfad1841faa591f549846d716ec126abf4ded58a
Author: little-cui <su...@qq.com>
AuthorDate: Mon May 10 09:35:16 2021 +0800

    Fix: add metrics config (#970)
---
 etc/conf/app.yaml                                 |  1 +
 server/bootstrap/bootstrap.go                     |  2 +-
 server/config/config.go                           |  6 ++----
 server/config/server.go                           |  2 --
 server/config/types.go                            | 11 +++++++++--
 server/rest/{prometheus => metrics}/prometheus.go |  6 +++++-
 server/server.go                                  |  9 ++++++++-
 server/service/rbac/rbac.go                       |  1 -
 8 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/etc/conf/app.yaml b/etc/conf/app.yaml
index dc05732..ea3cf24 100644
--- a/etc/conf/app.yaml
+++ b/etc/conf/app.yaml
@@ -170,6 +170,7 @@ rbac:
   publicKeyFile:
 
 metrics:
+  enable: true
   interval: 30s
 
 tracing:
diff --git a/server/bootstrap/bootstrap.go b/server/bootstrap/bootstrap.go
index 6e00e8b..270d707 100644
--- a/server/bootstrap/bootstrap.go
+++ b/server/bootstrap/bootstrap.go
@@ -64,7 +64,7 @@ import (
 	_ "github.com/apache/servicecomb-service-center/server/service/gov/kie"
 
 	//metrics
-	_ "github.com/apache/servicecomb-service-center/server/rest/prometheus"
+	_ "github.com/apache/servicecomb-service-center/server/rest/metrics"
 
 	"github.com/apache/servicecomb-service-center/pkg/log"
 	"github.com/apache/servicecomb-service-center/server/broker"
diff --git a/server/config/config.go b/server/config/config.go
index 0775fe0..a9e35e2 100644
--- a/server/config/config.go
+++ b/server/config/config.go
@@ -93,8 +93,8 @@ func GetRBAC() serverConfig {
 }
 
 //GetMetrics return the metrics configs
-func GetMetrics() serverConfig {
-	return App.Server.Config
+func GetMetrics() Metrics {
+	return *App.Metrics
 }
 
 func Init() {
@@ -198,8 +198,6 @@ func loadServerConfig() ServerConfig {
 			SchemaEditable: GetBool("registry.schema.editable", false, WithENV("SCHEMA_EDITABLE")),
 
 			EnableRBAC: GetBool("rbac.enable", false, WithStandby("rbac_enabled")),
-
-			MetricsInterval: GetDuration("metrics.interval", 30*time.Second, WithENV("METRICS_INTERVAL")),
 		},
 	}
 }
diff --git a/server/config/server.go b/server/config/server.go
index 311933b..162ca36 100644
--- a/server/config/server.go
+++ b/server/config/server.go
@@ -80,8 +80,6 @@ type serverConfig struct {
 
 	// instance ttl in seconds
 	InstanceTTL int64 `json:"-"`
-
-	MetricsInterval time.Duration `json:"-"`
 }
 
 func (si *ServerConfig) IsDev() bool {
diff --git a/server/config/types.go b/server/config/types.go
index 9b3491b..46c9d7d 100644
--- a/server/config/types.go
+++ b/server/config/types.go
@@ -28,8 +28,9 @@ const (
 
 //AppConfig is yaml file struct
 type AppConfig struct {
-	Gov    *Gov          `yaml:"gov"`
-	Server *ServerConfig `yaml:"server"`
+	Gov     *Gov          `yaml:"gov"`
+	Server  *ServerConfig `yaml:"server"`
+	Metrics *Metrics      `yaml:"metrics"`
 }
 type Gov struct {
 	DistOptions []DistributorOptions `yaml:"plugins"`
@@ -47,3 +48,9 @@ func (c *AppConfig) GetImplName(kind plugin.Kind) string {
 func (c *AppConfig) GetPluginDir() string {
 	return c.Server.Config.PluginsDir
 }
+
+// Metrics is the configurations of metrics
+type Metrics struct {
+	Enable   bool   `yaml:"enable"`
+	Interval string `yaml:"interval"`
+}
diff --git a/server/rest/prometheus/prometheus.go b/server/rest/metrics/prometheus.go
similarity index 89%
rename from server/rest/prometheus/prometheus.go
rename to server/rest/metrics/prometheus.go
index 65ef0be..adc94e1 100644
--- a/server/rest/prometheus/prometheus.go
+++ b/server/rest/metrics/prometheus.go
@@ -15,13 +15,17 @@
  * limitations under the License.
  */
 
-package prometheus
+package metrics
 
 import (
+	"github.com/apache/servicecomb-service-center/server/config"
 	"github.com/apache/servicecomb-service-center/server/rest"
 	"github.com/prometheus/client_golang/prometheus/promhttp"
 )
 
 func init() {
+	if !config.GetMetrics().Enable {
+		return
+	}
 	rest.RegisterServerHandler("/metrics", promhttp.Handler())
 }
diff --git a/server/server.go b/server/server.go
index 13d9546..ac7dee7 100644
--- a/server/server.go
+++ b/server/server.go
@@ -21,6 +21,7 @@ import (
 	"context"
 	"net"
 	"os"
+	"strings"
 	"time"
 
 	"github.com/apache/servicecomb-service-center/datasource"
@@ -120,7 +121,13 @@ func (s *ServiceCenterServer) initDatasource() {
 }
 
 func (s *ServiceCenterServer) initMetrics() {
-	interval := config.GetDuration("metrics.interval", defaultCollectPeriod, config.WithENV("METRICS_INTERVAL"))
+	if !config.GetMetrics().Enable {
+		return
+	}
+	interval, err := time.ParseDuration(strings.TrimSpace(config.GetMetrics().Interval))
+	if err != nil {
+		log.Errorf(err, "invalid metrics config[interval], set default %s", defaultCollectPeriod)
+	}
 	if interval <= time.Second {
 		interval = defaultCollectPeriod
 	}
diff --git a/server/service/rbac/rbac.go b/server/service/rbac/rbac.go
index 5f85938..7b60c5b 100644
--- a/server/service/rbac/rbac.go
+++ b/server/service/rbac/rbac.go
@@ -73,7 +73,6 @@ func Init() {
 	initAdminRole()
 	initDevRole()
 	rbacframe.Add2WhiteAPIList(APITokenGranter)
-	config.Server.Config.EnableRBAC = true
 	log.Info("rbac is enabled")
 }