Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support report the index of servicecomb_kie_config_count to prometheus #315

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions examples/dev/kie-conf.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions 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 Expand Up @@ -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=
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 GetMetric() MetricObject {
return Configurations.MetricObject
}
8 changes: 7 additions & 1 deletion server/config/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要配置,这些通过环境变量控制即可

}

type TLS struct {
Expand Down
57 changes: 57 additions & 0 deletions server/datasource/metric/prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package metric
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是server/metrics包


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个需要可配置


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

局部变量命名大写开头不规范

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
}
}
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/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() {
Expand All @@ -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())
}
Expand Down
Loading