From 22bdbaf56f9b9747fe2d59f11f55542151f1e102 Mon Sep 17 00:00:00 2001 From: tornado-ssy <64736788+tornado-ssy@users.noreply.github.com> Date: Fri, 26 Jan 2024 00:09:38 +0800 Subject: [PATCH] [feat]support report the index of servicecomb_kie_config_count to prometheus (#316) Co-authored-by: songshiyuan 00649746 (cherry picked from commit 0167fb0d655a998daa67f64b3b3e8791cbaf35c0) --- examples/dev/conf/chassis.yaml | 3 ++ server/config/struct.go | 2 +- server/metrics/prometheus.go | 54 ++++++++++++++++++++++++++++++++++ server/server.go | 4 +++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 server/metrics/prometheus.go diff --git a/examples/dev/conf/chassis.yaml b/examples/dev/conf/chassis.yaml index 4eab0d22..cb2cc691 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/server/config/struct.go b/server/config/struct.go index 3047ded6..73910272 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 00000000..6d38031d --- /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 4214262f..eaf13e5a 100644 --- a/server/server.go +++ b/server/server.go @@ -22,6 +22,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" @@ -46,6 +47,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()) }