From 827b23bb3c36e7c26ece7fbd55adf3e2d96257d1 Mon Sep 17 00:00:00 2001 From: songshiyuan 00649746 Date: Tue, 23 Jan 2024 20:23:02 +0800 Subject: [PATCH] support report the index of servicecomb_kie_config_count --- examples/dev/conf/chassis.yaml | 2 + examples/dev/kie-conf.yaml | 3 ++ go.sum | 3 +- server/config/config.go | 4 ++ server/config/struct.go | 8 +++- server/datasource/metric/prometheus.go | 57 ++++++++++++++++++++++++++ server/server.go | 11 +++-- 7 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 server/datasource/metric/prometheus.go diff --git a/examples/dev/conf/chassis.yaml b/examples/dev/conf/chassis.yaml index 4eab0d22..cf55b641 100755 --- a/examples/dev/conf/chassis.yaml +++ b/examples/dev/conf/chassis.yaml @@ -6,6 +6,8 @@ servicecomb: protocols: rest: listenAddress: 127.0.0.1:30110 + metrics: + enable: true match: rateLimitPolicy: | matches: diff --git a/examples/dev/kie-conf.yaml b/examples/dev/kie-conf.yaml index 56aff3a3..5066d3e6 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, embedded_etcd_with_localstorage, etcd_with_localstorage kind: embedded_etcd +metricObject: + domain: default + project: default # localFilePath: is the root path to store local kv files # uri: http://127.0.0.1:2379 diff --git a/go.sum b/go.sum index e67db898..949f57fc 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= @@ -69,7 +68,7 @@ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da h1:8GUt8eRujhVEGZ github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= -github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/armon/go-radix v1.0.0/go.mod h1:TsTFsXBVHVK4HQ+UrFSsQEhBXZGCDqoY+cr+sUq5ZmA= github.com/aws/aws-sdk-go v1.34.28 h1:sscPpn/Ns3i0F4HPEWAVcwdIRaZZCuL7llJ2/60yPIk= github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= github.com/benbjohnson/clock v1.0.3 h1:vkLuvpK4fmtSCuo60+yC63p7y0BmQ8gm5ZXGuBCJyXg= diff --git a/server/config/config.go b/server/config/config.go index cb8db17c..9c248f2d 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -52,3 +52,7 @@ func GetRBAC() RBAC { func GetSync() Sync { return Configurations.Sync } + +func GetMetric() MetricObject { + return Configurations.MetricObject +} diff --git a/server/config/struct.go b/server/config/struct.go index 3047ded6..b1db9a1d 100644 --- a/server/config/struct.go +++ b/server/config/struct.go @@ -22,12 +22,18 @@ 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 PeerAddr string AdvertiseAddr string + MetricObject MetricObject `yaml:"metricObject"` +} + +type MetricObject struct { + Domain string `yaml:"domain"` + Project string `yaml:"project"` } type TLS struct { diff --git a/server/datasource/metric/prometheus.go b/server/datasource/metric/prometheus.go new file mode 100644 index 00000000..7d66abff --- /dev/null +++ b/server/datasource/metric/prometheus.go @@ -0,0 +1,57 @@ +package metric + +import ( + "context" + "time" + + "github.com/go-chassis/go-chassis/v2/pkg/metrics" + "github.com/go-chassis/openlog" + + "github.com/apache/servicecomb-kie/server/config" + "github.com/apache/servicecomb-kie/server/datasource" +) + +const ReportInterval = 15 + +func InitMetric(m config.MetricObject) 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 + } + if m.Domain == "" { + m.Domain = "default" + } + if m.Project == "" { + m.Project = "default" + } + ReportTicker := time.NewTicker(ReportInterval * time.Second) + go func() { + for { + _, ok := <-ReportTicker.C + if !ok { + return + } + getTotalConfigCount(m.Project, m.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..7071e2b6 100644 --- a/server/server.go +++ b/server/server.go @@ -18,16 +18,18 @@ package server import ( + "github.com/go-chassis/go-chassis/v2" + "github.com/go-chassis/go-chassis/v2/core/common" + "github.com/go-chassis/openlog" + "github.com/apache/servicecomb-kie/pkg/validator" "github.com/apache/servicecomb-kie/server/config" "github.com/apache/servicecomb-kie/server/datasource" + "github.com/apache/servicecomb-kie/server/datasource/metric" "github.com/apache/servicecomb-kie/server/db" "github.com/apache/servicecomb-kie/server/pubsub" "github.com/apache/servicecomb-kie/server/rbac" v1 "github.com/apache/servicecomb-kie/server/resource/v1" - "github.com/go-chassis/go-chassis/v2" - "github.com/go-chassis/go-chassis/v2/core/common" - "github.com/go-chassis/openlog" ) func Run() { @@ -46,6 +48,9 @@ func Run() { if err := datasource.Init(config.GetDB().Kind); err != nil { openlog.Fatal(err.Error()) } + if err := metric.InitMetric(config.GetMetric()); err != nil { + openlog.Fatal(err.Error()) + } if err := validator.Init(); err != nil { openlog.Fatal("validate init failed: " + err.Error()) }