[
https://issues.apache.org/jira/browse/SCB-792?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16573143#comment-16573143
]
ASF GitHub Bot commented on SCB-792:
------------------------------------
asifdxtreme closed pull request #412: SCB-792 More abundant metrics information
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/412
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/integration/integrationtest_suite_test.go
b/integration/integrationtest_suite_test.go
index adad6665..ae25b96d 100644
--- a/integration/integrationtest_suite_test.go
+++ b/integration/integrationtest_suite_test.go
@@ -21,15 +21,10 @@ import (
"github.com/onsi/ginkgo/reporters"
. "github.com/onsi/gomega"
"net/http"
+ "os"
"testing"
)
-func TestIntegration(t *testing.T) {
- RegisterFailHandler(Fail)
- junitReporter := reporters.NewJUnitReporter("model.junit.xml")
- RunSpecsWithDefaultAndCustomReporters(t, "Integration Test for SC",
[]Reporter{junitReporter})
-}
-
var scclient *http.Client
var insecurityConnection = &http.Client{}
@@ -39,3 +34,16 @@ var SCURL = "http://127.0.0.1:30100"
var _ = BeforeSuite(func() {
scclient = insecurityConnection
})
+
+func init() {
+ addr, ok := os.LookupEnv("CSE_REGISTRY_ADDRESS")
+ if ok {
+ SCURL = addr
+ }
+}
+
+func TestIntegration(t *testing.T) {
+ RegisterFailHandler(Fail)
+ junitReporter := reporters.NewJUnitReporter("model.junit.xml")
+ RunSpecsWithDefaultAndCustomReporters(t, "Integration Test for SC",
[]Reporter{junitReporter})
+}
diff --git a/server/metric/calculator.go b/server/metric/calculator.go
index 8b163c6d..99fac216 100644
--- a/server/metric/calculator.go
+++ b/server/metric/calculator.go
@@ -18,17 +18,14 @@ package metric
import (
dto "github.com/prometheus/client_model/go"
- "strings"
)
var (
- calculators = make(map[string]Calculator)
- DefaultCalculator = &CommonCalculator{}
+ DefaultCalculator Calculator = &CommonCalculator{}
)
type Calculator interface {
Calc(mf *dto.MetricFamily) float64
- ReShape()
}
type CommonCalculator struct {
@@ -52,9 +49,6 @@ func (c *CommonCalculator) Calc(mf *dto.MetricFamily) float64
{
}
}
-func (c *CommonCalculator) ReShape() {
-}
-
func metricCounterOf(m []*dto.Metric) float64 {
var sum float64 = 0
for _, d := range m {
@@ -80,19 +74,10 @@ func metricSummaryOf(m []*dto.Metric) float64 {
return sum / float64(count)
}
-func RegisterCalculator(family string, c Calculator) {
- calculators[family] = c
+func RegisterCalculator(c Calculator) {
+ DefaultCalculator = c
}
func Calculate(mf *dto.MetricFamily) float64 {
- if c, ok := calculators[strings.TrimPrefix(mf.GetName(),
familyNamePrefix)]; ok {
- return c.Calc(mf)
- }
return DefaultCalculator.Calc(mf)
}
-
-func ReShape() {
- for _, c := range calculators {
- c.ReShape()
- }
-}
diff --git a/server/metric/calculator_test.go b/server/metric/calculator_test.go
new file mode 100644
index 00000000..50bc42b3
--- /dev/null
+++ b/server/metric/calculator_test.go
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package metric
+
+import (
+ dto "github.com/prometheus/client_model/go"
+ "testing"
+)
+
+func TestCommonCalculator_Calc(t *testing.T) {
+ c := &CommonCalculator{}
+
+ mf := &dto.MetricFamily{}
+ mt := dto.MetricType_UNTYPED
+ v := float64(0)
+ n := uint64(0)
+
+ if c.Calc(mf) != 0 {
+ t.Fatalf("TestCommonCalculator_Calc failed")
+ }
+
+ mf = &dto.MetricFamily{Type: &mt, Metric: []*dto.Metric{{}}}
+ if c.Calc(mf) != 0 {
+ t.Fatalf("TestCommonCalculator_Calc failed")
+ }
+
+ mt = dto.MetricType_GAUGE
+ v = 1
+ mf = &dto.MetricFamily{Type: &mt, Metric: []*dto.Metric{
+ {Gauge: &dto.Gauge{Value: &v}}, {Gauge: &dto.Gauge{Value: &v}}}}
+ if c.Calc(mf) != 1 {
+ t.Fatalf("TestCommonCalculator_Calc failed")
+ }
+
+ mt = dto.MetricType_COUNTER
+ v = 1
+ mf = &dto.MetricFamily{Type: &mt, Metric: []*dto.Metric{
+ {Counter: &dto.Counter{Value: &v}}, {Counter:
&dto.Counter{Value: &v}}}}
+ if c.Calc(mf) != 2 {
+ t.Fatalf("TestCommonCalculator_Calc failed")
+ }
+
+ mt = dto.MetricType_SUMMARY
+ v = 3
+ n = 2
+ mf = &dto.MetricFamily{Type: &mt, Metric: []*dto.Metric{
+ {Summary: &dto.Summary{SampleCount: &n, SampleSum: &v}},
{Summary: &dto.Summary{SampleCount: &n, SampleSum: &v}}}}
+ if c.Calc(mf) != v/float64(n) {
+ t.Fatalf("TestCommonCalculator_Calc failed")
+ }
+}
diff --git a/server/metric/common.go b/server/metric/common.go
index b313cd38..16941e7f 100644
--- a/server/metric/common.go
+++ b/server/metric/common.go
@@ -17,28 +17,47 @@
package metric
import (
+ "github.com/apache/incubator-servicecomb-service-center/pkg/util"
"github.com/astaxie/beego"
"net"
+ "os"
"sync"
"time"
)
const (
- collectInterval = 5 * time.Second
- FamilyName = "service_center"
- familyNamePrefix = FamilyName + "_"
+ defaultCollectPeriod = 30 * time.Second
+ FamilyName = "service_center"
+ familyNamePrefix = FamilyName + "_"
)
var (
- sysMetricNames = map[string]struct{}{
- "process_resident_memory_bytes": {},
- "process_cpu_seconds_total": {},
- "go_threads": {},
- }
+ // metrics collect period
+ Period = 30 * time.Second
+ // system metrics map
+ SysMetrics util.ConcurrentMap
+
getEndpointOnce sync.Once
instance string
)
+func init() {
+ Period = getPeriod()
+ SysMetrics.Put("process_resident_memory_bytes", struct{}{})
+ SysMetrics.Put("process_cpu_seconds_total", struct{}{})
+ SysMetrics.Put("go_threads", struct{}{})
+ SysMetrics.Put("go_goroutines", struct{}{})
+}
+
+func getPeriod() time.Duration {
+ inv := os.Getenv("METRICS_INTERVAL")
+ d, err := time.ParseDuration(inv)
+ if err == nil && d >= time.Second {
+ return d
+ }
+ return defaultCollectPeriod
+}
+
func InstanceName() string {
getEndpointOnce.Do(func() {
restIp := beego.AppConfig.String("httpaddr")
diff --git a/server/metric/common_test.go b/server/metric/common_test.go
new file mode 100644
index 00000000..2652bffd
--- /dev/null
+++ b/server/metric/common_test.go
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package metric
+
+import (
+ "github.com/astaxie/beego"
+ "os"
+ "testing"
+ "time"
+)
+
+func TestInstanceName(t *testing.T) {
+ beego.AppConfig.Set("rpcaddr", "a")
+ beego.AppConfig.Set("rpcport", "b")
+ i := InstanceName()
+ if i != "a:b" {
+ t.Fatalf("TestInstanceName failed")
+ }
+ // case: initialize only one time
+ beego.AppConfig.Set("httpaddr", "c")
+ beego.AppConfig.Set("httpaddr", "d")
+ i = InstanceName()
+ if i != "a:b" {
+ t.Fatalf("TestInstanceName failed")
+ }
+}
+
+func TestPeriod(t *testing.T) {
+ if getPeriod() != 30*time.Second {
+ t.Fatalf("TestPeriod failed")
+ }
+ os.Setenv("METRICS_INTERVAL", time.Millisecond.String())
+ if getPeriod() != 30*time.Second {
+ t.Fatalf("TestPeriod failed")
+ }
+ os.Setenv("METRICS_INTERVAL", "err")
+ if getPeriod() != 30*time.Second {
+ t.Fatalf("TestPeriod failed")
+ }
+ os.Setenv("METRICS_INTERVAL", time.Second.String())
+ if getPeriod() != time.Second {
+ t.Fatalf("TestPeriod failed")
+ }
+}
diff --git a/server/metric/gatherer.go b/server/metric/gatherer.go
index 72a39471..2b9a4337 100644
--- a/server/metric/gatherer.go
+++ b/server/metric/gatherer.go
@@ -60,7 +60,7 @@ func (mm *MetricsGatherer) Start() {
}
func (mm *MetricsGatherer) loop(ctx context.Context) {
- ticker := time.NewTicker(collectInterval)
+ ticker := time.NewTicker(Period)
for {
select {
case <-ctx.Done():
@@ -70,6 +70,8 @@ func (mm *MetricsGatherer) loop(ctx context.Context) {
util.Logger().Errorf(err, "metrics collect
failed.")
return
}
+
+ Report()
}
}
}
@@ -82,10 +84,9 @@ func (mm *MetricsGatherer) Collect() error {
for _, mf := range mfs {
name := mf.GetName()
- if _, ok := sysMetricNames[name]; strings.Index(name,
familyNamePrefix) == 0 || ok {
+ if _, ok := SysMetrics.Get(name); strings.Index(name,
familyNamePrefix) == 0 || ok {
mm.Records.Put(strings.TrimPrefix(name,
familyNamePrefix), Calculate(mf))
}
}
- ReShape()
return nil
}
diff --git a/server/metric/gatherer_test.go b/server/metric/gatherer_test.go
new file mode 100644
index 00000000..4867aca9
--- /dev/null
+++ b/server/metric/gatherer_test.go
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package metric
+
+import "testing"
+
+func TestMetricsGatherer_Collect(t *testing.T) {
+ g := NewGatherer()
+ err := g.Collect()
+ if err != nil {
+ t.Fatalf("TestMetricsGatherer_Collect")
+ }
+}
diff --git a/server/metric/reporter.go b/server/metric/reporter.go
new file mode 100644
index 00000000..1dd6d80e
--- /dev/null
+++ b/server/metric/reporter.go
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package metric
+
+import "github.com/apache/incubator-servicecomb-service-center/pkg/util"
+
+var reporters = make(map[string]Reporter)
+
+type Reporter interface {
+ Report()
+}
+
+type noopReporter struct {
+}
+
+func (*noopReporter) Report() {}
+
+func RegisterReporter(name string, r Reporter) {
+ reporters[name] = r
+ util.Logger().Infof("register metrics reporter '%s'", name)
+}
+
+func Report() {
+ for _, r := range reporters {
+ r.Report()
+ }
+}
diff --git a/server/metric/reporter_test.go b/server/metric/reporter_test.go
new file mode 100644
index 00000000..3962540f
--- /dev/null
+++ b/server/metric/reporter_test.go
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package metric
+
+import "testing"
+
+type mockReporter struct {
+ V bool
+}
+
+func (m *mockReporter) Report() {
+ m.V = true
+}
+
+func TestRegisterReporter(t *testing.T) {
+ r := &mockReporter{}
+ RegisterReporter("test", r)
+ Report()
+ if !r.V {
+ t.Fatalf("TestRegisterReporter failed")
+ }
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> More abundant metrics information
> ---------------------------------
>
> Key: SCB-792
> URL: https://issues.apache.org/jira/browse/SCB-792
> Project: Apache ServiceComb
> Issue Type: Improvement
> Components: Service-Center
> Reporter: little-cui
> Assignee: little-cui
> Priority: Major
> Fix For: service-center-1.1.0
>
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)