You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/01/04 07:16:18 UTC

[incubator-servicecomb-service-center] branch master updated: SCB-155 Change the package of metric.go (#242)

This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new 4264771  SCB-155 Change the package of metric.go (#242)
4264771 is described below

commit 42647711644c47f958d8004b279fcf343f59c761
Author: little-cui <su...@qq.com>
AuthorDate: Thu Jan 4 15:16:16 2018 +0800

    SCB-155 Change the package of metric.go (#242)
    
    * SCB-155 Change the package of metric.go
    
    * SCB-155 Change the package of metric.go
---
 pkg/util/metric.go                        |  64 +++++++++++++++
 server/handler/metric/handler.go          |  49 ------------
 server/handler/metric/metric.go           | 127 +++++-------------------------
 server/{handler/metric => rest}/metric.go |  46 +----------
 4 files changed, 85 insertions(+), 201 deletions(-)

diff --git a/pkg/util/metric.go b/pkg/util/metric.go
new file mode 100644
index 0000000..2a80529
--- /dev/null
+++ b/pkg/util/metric.go
@@ -0,0 +1,64 @@
+/*
+ * 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 util
+
+import (
+	dto "github.com/prometheus/client_model/go"
+)
+
+// Get value of metricFamily
+func MetricValueOf(mf *dto.MetricFamily) float64 {
+	if len(mf.GetMetric()) == 0 {
+		return 0
+	}
+
+	switch mf.GetType() {
+	case dto.MetricType_GAUGE:
+		return mf.GetMetric()[0].GetGauge().GetValue()
+	case dto.MetricType_COUNTER:
+		return metricCounterOf(mf.GetMetric())
+	case dto.MetricType_SUMMARY:
+		return metricSummaryOf(mf.GetMetric())
+	default:
+		return 0
+	}
+}
+
+func metricCounterOf(m []*dto.Metric) float64 {
+	var sum float64 = 0
+	for _, d := range m {
+		sum += d.GetCounter().GetValue()
+	}
+	return sum
+}
+
+func metricSummaryOf(m []*dto.Metric) float64 {
+	var (
+		count uint64  = 0
+		sum   float64 = 0
+	)
+	for _, d := range m {
+		count += d.GetSummary().GetSampleCount()
+		sum += d.GetSummary().GetSampleSum()
+	}
+
+	if count == 0 {
+		return 0
+	}
+
+	return sum / float64(count)
+}
diff --git a/server/handler/metric/handler.go b/server/handler/metric/handler.go
deleted file mode 100644
index 5b54d2f..0000000
--- a/server/handler/metric/handler.go
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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/chain"
-	"github.com/apache/incubator-servicecomb-service-center/pkg/rest"
-	"github.com/apache/incubator-servicecomb-service-center/pkg/util"
-	svr "github.com/apache/incubator-servicecomb-service-center/server/rest"
-	"net/http"
-	"time"
-)
-
-type MetricsHandler struct {
-}
-
-func (h *MetricsHandler) Handle(i *chain.Invocation) {
-	w, r := i.Context().Value(rest.CTX_RESPONSE).(http.ResponseWriter),
-		i.Context().Value(rest.CTX_REQUEST).(*http.Request)
-	cb := i.Func
-	i.Invoke(func(ret chain.Result) {
-		cb(ret)
-
-		start, ok := i.Context().Value(svr.CTX_START_TIMESTAMP).(time.Time)
-		if !ok {
-			return
-		}
-		ReportRequestCompleted(w, r, start)
-		util.LogNilOrWarnf(start, "%s %s", r.Method, r.RequestURI)
-	})
-}
-
-func RegisterHandlers() {
-	chain.RegisterHandler(rest.SERVER_CHAIN_NAME, &MetricsHandler{})
-}
diff --git a/server/handler/metric/metric.go b/server/handler/metric/metric.go
index ff46d32..18dcae4 100644
--- a/server/handler/metric/metric.go
+++ b/server/handler/metric/metric.go
@@ -17,120 +17,33 @@
 package metric
 
 import (
-	"fmt"
+	"github.com/apache/incubator-servicecomb-service-center/pkg/chain"
 	"github.com/apache/incubator-servicecomb-service-center/pkg/rest"
-	"github.com/apache/incubator-servicecomb-service-center/server/core"
-	"github.com/prometheus/client_golang/prometheus"
-	dto "github.com/prometheus/client_model/go"
+	"github.com/apache/incubator-servicecomb-service-center/pkg/util"
+	svr "github.com/apache/incubator-servicecomb-service-center/server/rest"
 	"net/http"
-	"strconv"
-	"strings"
 	"time"
 )
 
-var (
-	incomingRequests = prometheus.NewCounterVec(
-		prometheus.CounterOpts{
-			Namespace: "service_center",
-			Subsystem: "http",
-			Name:      "request_total",
-			Help:      "Counter of requests received into ROA handler",
-		}, []string{"method", "code", "instance", "api"})
-
-	successfulRequests = prometheus.NewCounterVec(
-		prometheus.CounterOpts{
-			Namespace: "service_center",
-			Subsystem: "http",
-			Name:      "success_total",
-			Help:      "Counter of successful requests processed by ROA handler",
-		}, []string{"method", "code", "instance", "api"})
-
-	reqDurations = prometheus.NewSummaryVec(
-		prometheus.SummaryOpts{
-			Namespace:  "service_center",
-			Subsystem:  "http",
-			Name:       "request_durations_microseconds",
-			Help:       "HTTP request latency summary of ROA handler",
-			Objectives: prometheus.DefObjectives,
-		}, []string{"method", "instance", "api"})
-)
-
-func init() {
-	prometheus.MustRegister(incomingRequests, successfulRequests, reqDurations)
-
-	http.Handle("/metrics", prometheus.Handler())
-}
-
-func ReportRequestCompleted(w http.ResponseWriter, r *http.Request, start time.Time) {
-	instance := fmt.Sprint(core.Instance.Endpoints)
-	elapsed := float64(time.Since(start).Nanoseconds()) / 1000
-	route, _ := r.Context().Value(rest.CTX_MATCH_PATTERN).(string)
-
-	if strings.Index(r.Method, "WATCH") != 0 {
-		reqDurations.WithLabelValues(r.Method, instance, route).Observe(elapsed)
-	}
-
-	success, code := codeOf(w.Header())
-
-	incomingRequests.WithLabelValues(r.Method, code, instance, route).Inc()
-
-	if success {
-		successfulRequests.WithLabelValues(r.Method, code, instance, route).Inc()
-	}
-}
-
-func codeOf(h http.Header) (bool, string) {
-	statusCode := h.Get("X-Response-Status")
-	if statusCode == "" {
-		return true, "200"
-	}
-
-	if code, _ := strconv.Atoi(statusCode); code >= http.StatusOK && code <= http.StatusAccepted {
-		return true, statusCode
-	}
-
-	return false, statusCode
+type MetricsHandler struct {
 }
 
-// Get value of metricFamily
-func MetricValueOf(mf *dto.MetricFamily) float64 {
-	if len(mf.GetMetric()) == 0 {
-		return 0
-	}
-
-	switch mf.GetType() {
-	case dto.MetricType_GAUGE:
-		return mf.GetMetric()[0].GetGauge().GetValue()
-	case dto.MetricType_COUNTER:
-		return metricCounterOf(mf.GetMetric())
-	case dto.MetricType_SUMMARY:
-		return metricSummaryOf(mf.GetMetric())
-	default:
-		return 0
-	}
-}
-
-func metricCounterOf(m []*dto.Metric) float64 {
-	var sum float64 = 0
-	for _, d := range m {
-		sum += d.GetCounter().GetValue()
-	}
-	return sum
+func (h *MetricsHandler) Handle(i *chain.Invocation) {
+	w, r := i.Context().Value(rest.CTX_RESPONSE).(http.ResponseWriter),
+		i.Context().Value(rest.CTX_REQUEST).(*http.Request)
+	cb := i.Func
+	i.Invoke(func(ret chain.Result) {
+		cb(ret)
+
+		start, ok := i.Context().Value(svr.CTX_START_TIMESTAMP).(time.Time)
+		if !ok {
+			return
+		}
+		svr.ReportRequestCompleted(w, r, start)
+		util.LogNilOrWarnf(start, "%s %s", r.Method, r.RequestURI)
+	})
 }
 
-func metricSummaryOf(m []*dto.Metric) float64 {
-	var (
-		count uint64  = 0
-		sum   float64 = 0
-	)
-	for _, d := range m {
-		count += d.GetSummary().GetSampleCount()
-		sum += d.GetSummary().GetSampleSum()
-	}
-
-	if count == 0 {
-		return 0
-	}
-
-	return sum / float64(count)
+func RegisterHandlers() {
+	chain.RegisterHandler(rest.SERVER_CHAIN_NAME, &MetricsHandler{})
 }
diff --git a/server/handler/metric/metric.go b/server/rest/metric.go
similarity index 77%
copy from server/handler/metric/metric.go
copy to server/rest/metric.go
index ff46d32..350aed8 100644
--- a/server/handler/metric/metric.go
+++ b/server/rest/metric.go
@@ -14,14 +14,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package metric
+package rest
 
 import (
 	"fmt"
 	"github.com/apache/incubator-servicecomb-service-center/pkg/rest"
 	"github.com/apache/incubator-servicecomb-service-center/server/core"
 	"github.com/prometheus/client_golang/prometheus"
-	dto "github.com/prometheus/client_model/go"
 	"net/http"
 	"strconv"
 	"strings"
@@ -91,46 +90,3 @@ func codeOf(h http.Header) (bool, string) {
 
 	return false, statusCode
 }
-
-// Get value of metricFamily
-func MetricValueOf(mf *dto.MetricFamily) float64 {
-	if len(mf.GetMetric()) == 0 {
-		return 0
-	}
-
-	switch mf.GetType() {
-	case dto.MetricType_GAUGE:
-		return mf.GetMetric()[0].GetGauge().GetValue()
-	case dto.MetricType_COUNTER:
-		return metricCounterOf(mf.GetMetric())
-	case dto.MetricType_SUMMARY:
-		return metricSummaryOf(mf.GetMetric())
-	default:
-		return 0
-	}
-}
-
-func metricCounterOf(m []*dto.Metric) float64 {
-	var sum float64 = 0
-	for _, d := range m {
-		sum += d.GetCounter().GetValue()
-	}
-	return sum
-}
-
-func metricSummaryOf(m []*dto.Metric) float64 {
-	var (
-		count uint64  = 0
-		sum   float64 = 0
-	)
-	for _, d := range m {
-		count += d.GetSummary().GetSampleCount()
-		sum += d.GetSummary().GetSampleSum()
-	}
-
-	if count == 0 {
-		return 0
-	}
-
-	return sum / float64(count)
-}

-- 
To stop receiving notification emails like this one, please contact
['"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>'].