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/06/16 11:24:22 UTC

[servicecomb-service-center] branch master updated: Nil point exception when disable metrics (#1055)

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 4693776  Nil point exception when disable metrics (#1055)
4693776 is described below

commit 4693776d6d79024015693820cd327785e033a155
Author: little-cui <su...@qq.com>
AuthorDate: Wed Jun 16 19:24:12 2021 +0800

    Nil point exception when disable metrics (#1055)
---
 etc/conf/app.yaml                 |  1 +
 pkg/metrics/gatherer.go           |  6 ++++++
 pkg/metrics/manager.go            |  2 +-
 server/config/config.go           |  5 -----
 server/config/types.go            | 12 ++----------
 server/rest/metrics/prometheus.go |  2 +-
 server/server.go                  |  4 ++--
 7 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/etc/conf/app.yaml b/etc/conf/app.yaml
index 9bc7574..72de775 100644
--- a/etc/conf/app.yaml
+++ b/etc/conf/app.yaml
@@ -160,6 +160,7 @@ rbac:
   publicKeyFile: ./public.key
 
 metrics:
+  # enable to start metrics gather
   enable: true
   interval: 30s
   exporter: prometheus
diff --git a/pkg/metrics/gatherer.go b/pkg/metrics/gatherer.go
index e5d0397..6f48c51 100644
--- a/pkg/metrics/gatherer.go
+++ b/pkg/metrics/gatherer.go
@@ -28,6 +28,12 @@ import (
 	"github.com/apache/servicecomb-service-center/pkg/prometheus"
 )
 
+// EmptyGather just active when metrics disabled
+var EmptyGather = &Gather{
+	Records: NewMetrics(),
+	closed:  false,
+}
+
 func NewGatherer(opts Options) *Gather {
 	return &Gather{
 		Interval: opts.Interval,
diff --git a/pkg/metrics/manager.go b/pkg/metrics/manager.go
index f6c38b9..0e766b4 100644
--- a/pkg/metrics/manager.go
+++ b/pkg/metrics/manager.go
@@ -33,7 +33,7 @@ var (
 	SysMetrics util.ConcurrentMap
 	// Gatherer is the reader of sc metrics, but can not get not real time metrics
 	// Call the prometheus.Gather() if get the real time metrics
-	Gatherer *Gather
+	Gatherer = EmptyGather
 )
 
 func Init(opts Options) error {
diff --git a/server/config/config.go b/server/config/config.go
index 1a6b0e6..95e5d6f 100644
--- a/server/config/config.go
+++ b/server/config/config.go
@@ -92,11 +92,6 @@ func GetRBAC() ServerConfigDetail {
 	return App.Server.Config
 }
 
-//GetMetrics return the metrics configs
-func GetMetrics() Metrics {
-	return *App.Metrics
-}
-
 func Init() {
 	setCPUs()
 
diff --git a/server/config/types.go b/server/config/types.go
index 65efa58..9b3491b 100644
--- a/server/config/types.go
+++ b/server/config/types.go
@@ -28,9 +28,8 @@ const (
 
 //AppConfig is yaml file struct
 type AppConfig struct {
-	Gov     *Gov          `yaml:"gov"`
-	Server  *ServerConfig `yaml:"server"`
-	Metrics *Metrics      `yaml:"metrics"`
+	Gov    *Gov          `yaml:"gov"`
+	Server *ServerConfig `yaml:"server"`
 }
 type Gov struct {
 	DistOptions []DistributorOptions `yaml:"plugins"`
@@ -48,10 +47,3 @@ 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"`
-	Exporter string `yaml:"exporter"`
-}
diff --git a/server/rest/metrics/prometheus.go b/server/rest/metrics/prometheus.go
index bc8f39b..c96cd70 100644
--- a/server/rest/metrics/prometheus.go
+++ b/server/rest/metrics/prometheus.go
@@ -26,7 +26,7 @@ import (
 const exporterPrometheus = "prometheus"
 
 func init() {
-	if config.GetMetrics().Exporter != exporterPrometheus {
+	if config.GetString("metrics.exporter", "") != exporterPrometheus {
 		return
 	}
 	rest.RegisterServerHandler("/metrics", promhttp.Handler())
diff --git a/server/server.go b/server/server.go
index e2c66ef..e81729e 100644
--- a/server/server.go
+++ b/server/server.go
@@ -122,10 +122,10 @@ func (s *ServiceCenterServer) initDatasource() {
 }
 
 func (s *ServiceCenterServer) initMetrics() {
-	if !config.GetMetrics().Enable {
+	if !config.GetBool("metrics.enable", false) {
 		return
 	}
-	interval, err := time.ParseDuration(strings.TrimSpace(config.GetMetrics().Interval))
+	interval, err := time.ParseDuration(strings.TrimSpace(config.GetString("metrics.interval", defaultCollectPeriod.String())))
 	if err != nil {
 		log.Errorf(err, "invalid metrics config[interval], set default %s", defaultCollectPeriod)
 	}