[sync] merge master to dev: 1,[fix] cancel the depency between healthcheck and etcd 2,[feat]support report the index of servicecomb_kie_config_count to prometheus (#321)
* [feat]support report the index of servicecomb_kie_config_count to prometheus (#316)
Co-authored-by: songshiyuan 00649746 <songshiyuan3@huawei.com>
(cherry picked from commit 0167fb0d655a998daa67f64b3b3e8791cbaf35c0)
* [fix] cancel the depency between healthcheck and etcd (#319)
Co-authored-by: songshiyuan 00649746 <songshiyuan3@huawei.com>
(cherry picked from commit fcacc0dabea387ed319d677bc26b056781a4a942)
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 @@
protocols:
rest:
listenAddress: 127.0.0.1:30110
+ metrics:
+ enable: true
+ interval: 10s
match:
rateLimitPolicy: |
matches:
diff --git a/pkg/common/common.go b/pkg/common/common.go
index 9e452f3..7eb1d3d 100644
--- a/pkg/common/common.go
+++ b/pkg/common/common.go
@@ -40,6 +40,7 @@
QueryParamURLPath = "urlPath"
QueryParamUserAgent = "userAgent"
QueryParamOverride = "override"
+ QueryParamMode = "mode"
)
// http headers
diff --git a/server/config/struct.go b/server/config/struct.go
index 3047ded..7391027 100644
--- a/server/config/struct.go
+++ b/server/config/struct.go
@@ -22,7 +22,7 @@
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/resource/v1/admin_resource.go b/server/resource/v1/admin_resource.go
index 4d79eb1..d63b0a4 100644
--- a/server/resource/v1/admin_resource.go
+++ b/server/resource/v1/admin_resource.go
@@ -22,13 +22,15 @@
"strconv"
"time"
- "github.com/apache/servicecomb-kie/pkg/model"
- "github.com/apache/servicecomb-kie/server/datasource"
goRestful "github.com/emicklei/go-restful"
"github.com/go-chassis/cari/config"
"github.com/go-chassis/go-chassis/v2/pkg/runtime"
"github.com/go-chassis/go-chassis/v2/server/restful"
"github.com/go-chassis/openlog"
+
+ "github.com/apache/servicecomb-kie/pkg/common"
+ "github.com/apache/servicecomb-kie/pkg/model"
+ "github.com/apache/servicecomb-kie/server/datasource"
)
type AdminResource struct {
@@ -57,6 +59,10 @@
// HealthCheck provider version info and time info
func (r *AdminResource) HealthCheck(context *restful.Context) {
+ healthCheckMode := context.ReadQueryParameter(common.QueryParamMode)
+ if healthCheckMode == "liveness" {
+ return
+ }
domain := ReadDomain(context.Ctx)
resp := &model.DocHealthCheck{}
latest, err := datasource.GetBroker().GetRevisionDao().GetRevision(context.Ctx, domain)
diff --git a/server/resource/v1/admin_resource_test.go b/server/resource/v1/admin_resource_test.go
index 9344bf0..f7ed64d 100644
--- a/server/resource/v1/admin_resource_test.go
+++ b/server/resource/v1/admin_resource_test.go
@@ -19,6 +19,7 @@
import (
"encoding/json"
+ "fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@@ -26,10 +27,11 @@
_ "github.com/apache/servicecomb-kie/test"
- "github.com/apache/servicecomb-kie/pkg/model"
- v1 "github.com/apache/servicecomb-kie/server/resource/v1"
"github.com/go-chassis/go-chassis/v2/server/restful/restfultest"
"github.com/stretchr/testify/assert"
+
+ "github.com/apache/servicecomb-kie/pkg/model"
+ v1 "github.com/apache/servicecomb-kie/server/resource/v1"
)
func Test_HeathCheck(t *testing.T) {
@@ -47,3 +49,16 @@
assert.NoError(t, err)
assert.NotEmpty(t, data)
}
+
+func Test_HeakthCheckLiveMode(t *testing.T) {
+ path := fmt.Sprintf("/v1/health?mode=liveness")
+ r, _ := http.NewRequest("GET", path, nil)
+
+ revision := &v1.AdminResource{}
+ c, err := restfultest.New(revision, nil)
+ assert.NoError(t, err)
+ resp := httptest.NewRecorder()
+ c.ServeHTTP(resp, r)
+ respcode := resp.Code
+ assert.NotEmpty(t, respcode)
+}
diff --git a/server/server.go b/server/server.go
index 4214262..eaf13e5 100644
--- a/server/server.go
+++ b/server/server.go
@@ -22,6 +22,7 @@
"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 @@
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())
}