Skip to content

Commit

Permalink
support report the index of servicecomb_kie_config_count
Browse files Browse the repository at this point in the history
  • Loading branch information
songshiyuan 00649746 committed Jan 24, 2024
1 parent bea6626 commit 8910c73
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/dev/conf/chassis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ servicecomb:
protocols:
rest:
listenAddress: 127.0.0.1:30110
metrics:
enable: true
match:
rateLimitPolicy: |
matches:
Expand Down
4 changes: 4 additions & 0 deletions examples/dev/kie-conf.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
db:
# kind can be mongo, etcd, embedded_etcd
kind: embedded_etcd
reportInterval: 3s

# 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
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 4 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ func GetRBAC() RBAC {
func GetSync() Sync {
return Configurations.Sync
}

func GetReportInterval() string {
return Configurations.ReportInterval
}
9 changes: 5 additions & 4 deletions server/config/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package config

// Config is yaml file struct
type Config struct {
DB DB `yaml:"db"`
RBAC RBAC `yaml:"rbac"`
Sync Sync `yaml:"sync"`
//config from cli
DB DB `yaml:"db"`
RBAC RBAC `yaml:"rbac"`
Sync Sync `yaml:"sync"`
ReportInterval string `yaml:"reportInterval"`
// config from cli
ConfigFile string
NodeName string
ListenPeerAddr string
Expand Down
65 changes: 65 additions & 0 deletions server/metrics/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package metrics

import (
"context"
"fmt"
"os"
"strings"
"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"
)

func InitMetric(m *config.Config) 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
}
domain, project := "", ""
if os.Getenv("DOMAIN") == "" {
domain = "default"
}
if os.Getenv("PROJECT") == "" {
project = "default"
}
reportInterval := 10 * time.Second
if config.GetReportInterval() != "" {
str := strings.TrimSpace(config.GetReportInterval())
reportInterval, _ = time.ParseDuration(str)
}
reportTicker := time.NewTicker(reportInterval)
fmt.Println(m)
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
}
}
11 changes: 8 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/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"
"github.com/go-chassis/go-chassis/v2"
"github.com/go-chassis/go-chassis/v2/core/common"
"github.com/go-chassis/openlog"
)

func Run() {
Expand All @@ -46,6 +48,9 @@ func Run() {
if err := datasource.Init(config.GetDB().Kind); err != nil {
openlog.Fatal(err.Error())
}
if err := metrics.InitMetric(config.Configurations); err != nil {
openlog.Fatal(err.Error())
}
if err := validator.Init(); err != nil {
openlog.Fatal("validate init failed: " + err.Error())
}
Expand Down

0 comments on commit 8910c73

Please sign in to comment.