You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2024/01/25 16:09:44 UTC

(servicecomb-kie) branch master updated: [feat]support report the index of servicecomb_kie_config_count to prometheus (#316)

This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-kie.git


The following commit(s) were added to refs/heads/master by this push:
     new 0167fb0  [feat]support report the index of servicecomb_kie_config_count to prometheus (#316)
0167fb0 is described below

commit 0167fb0d655a998daa67f64b3b3e8791cbaf35c0
Author: tornado-ssy <64...@users.noreply.github.com>
AuthorDate: Fri Jan 26 00:09:38 2024 +0800

    [feat]support report the index of servicecomb_kie_config_count to prometheus (#316)
    
    Co-authored-by: songshiyuan 00649746 <so...@huawei.com>
---
 examples/dev/conf/chassis.yaml |  3 +++
 examples/dev/kie-conf.yaml     |  3 +++
 go.sum                         |  1 -
 server/config/struct.go        |  2 +-
 server/metrics/prometheus.go   | 54 ++++++++++++++++++++++++++++++++++++++++++
 server/server.go               |  4 ++++
 6 files changed, 65 insertions(+), 2 deletions(-)

diff --git a/examples/dev/conf/chassis.yaml b/examples/dev/conf/chassis.yaml
index 4eab0d2..cb2cc69 100755
--- a/examples/dev/conf/chassis.yaml
+++ b/examples/dev/conf/chassis.yaml
@@ -6,6 +6,9 @@ servicecomb:
   protocols:
     rest:
       listenAddress: 127.0.0.1:30110
+  metrics:
+    enable: true
+    interval: 10s
   match:
     rateLimitPolicy: |
       matches:
diff --git a/examples/dev/kie-conf.yaml b/examples/dev/kie-conf.yaml
index 11b9106..f2db2cb 100644
--- a/examples/dev/kie-conf.yaml
+++ b/examples/dev/kie-conf.yaml
@@ -1,6 +1,9 @@
 db:
   # kind can be mongo, etcd, embedded_etcd
   kind: embedded_etcd
+
+# localFilePath: is the root path to store local kv files
+#  uri: http://127.0.0.1:2379
   # uri is the db endpoints list
   #   kind=mongo, then is the mongodb cluster's uri, e.g. mongodb://127.0.0.1:27017/kie
   #   kind=etcd, then is the  remote etcd server's advertise-client-urls, e.g. http://127.0.0.1:2379
diff --git a/go.sum b/go.sum
index e67db89..1452d82 100644
--- a/go.sum
+++ b/go.sum
@@ -37,7 +37,6 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
 github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
 github.com/Azure/go-autorest/autorest v0.9.6/go.mod h1:/FALq9T/kS7b5J5qsQ+RSTUdAmGFqi0vUdVNNx8q630=
 github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
-github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
 github.com/Azure/go-autorest/autorest/adal v0.8.2/go.mod h1:ZjhuQClTqx435SRJ2iMlOxPYt3d2C/T/7TiQCVZSn3Q=
 github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA=
 github.com/Azure/go-autorest/autorest/date v0.2.0/go.mod h1:vcORJHLJEh643/Ioh9+vPmf1Ij9AEBM5FuBIXLmIy0g=
diff --git a/server/config/struct.go b/server/config/struct.go
index 83e91f0..6dea36f 100644
--- a/server/config/struct.go
+++ b/server/config/struct.go
@@ -22,7 +22,7 @@ type Config struct {
 	DB   DB   `yaml:"db"`
 	RBAC RBAC `yaml:"rbac"`
 	Sync Sync `yaml:"sync"`
-	//config from cli
+	// config from cli
 	ConfigFile     string
 	NodeName       string
 	ListenPeerAddr string
diff --git a/server/metrics/prometheus.go b/server/metrics/prometheus.go
new file mode 100644
index 0000000..6d38031
--- /dev/null
+++ b/server/metrics/prometheus.go
@@ -0,0 +1,54 @@
+package metrics
+
+import (
+	"context"
+	"time"
+
+	"github.com/go-chassis/go-archaius"
+	"github.com/go-chassis/go-chassis/v2/pkg/metrics"
+	"github.com/go-chassis/openlog"
+
+	"github.com/apache/servicecomb-kie/server/datasource"
+)
+
+const domain = "default"
+const project = "default"
+
+func InitMetric() error {
+	err := metrics.CreateGauge(metrics.GaugeOpts{
+		Key:    "servicecomb_kie_config_count",
+		Help:   "use to show the number of config under a specifical domain and project pair",
+		Labels: []string{"domain", "project"},
+	})
+	if err != nil {
+		openlog.Error("init servicecomb_kie_config_count Gauge fail:" + err.Error())
+		return err
+	}
+	reportIntervalstr := archaius.GetString("servicecomb.metrics.interval", "5s")
+	reportInterval, _ := time.ParseDuration(reportIntervalstr)
+	reportTicker := time.NewTicker(reportInterval)
+	go func() {
+		for {
+			_, ok := <-reportTicker.C
+			if !ok {
+				return
+			}
+			getTotalConfigCount(project, domain)
+		}
+	}()
+	return nil
+}
+
+func getTotalConfigCount(project, domain string) {
+	total, err := datasource.GetBroker().GetKVDao().Total(context.TODO(), project, domain)
+	if err != nil {
+		openlog.Error("set total config number fail: " + err.Error())
+		return
+	}
+	labels := map[string]string{"domain": domain, "project": project}
+	err = metrics.GaugeSet("servicecomb_kie_config_count", float64(total), labels)
+	if err != nil {
+		openlog.Error("set total config number fail:" + err.Error())
+		return
+	}
+}
diff --git a/server/server.go b/server/server.go
index 93e4587..a6f27a1 100644
--- a/server/server.go
+++ b/server/server.go
@@ -26,6 +26,7 @@ import (
 	"github.com/apache/servicecomb-kie/server/config"
 	"github.com/apache/servicecomb-kie/server/datasource"
 	"github.com/apache/servicecomb-kie/server/db"
+	"github.com/apache/servicecomb-kie/server/metrics"
 	"github.com/apache/servicecomb-kie/server/pubsub"
 	"github.com/apache/servicecomb-kie/server/rbac"
 	v1 "github.com/apache/servicecomb-kie/server/resource/v1"
@@ -47,6 +48,9 @@ func Run() {
 	if err := datasource.Init(config.GetDB().Kind); err != nil {
 		openlog.Fatal(err.Error())
 	}
+	if err := metrics.InitMetric(); err != nil {
+		openlog.Fatal(err.Error())
+	}
 	if err := validator.Init(); err != nil {
 		openlog.Fatal("validate init failed: " + err.Error())
 	}