[ 
https://issues.apache.org/jira/browse/SCB-155?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16310892#comment-16310892
 ] 

ASF GitHub Bot commented on SCB-155:
------------------------------------

little-cui closed pull request #242: SCB-155 Change the package of metric.go
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/242
 
 
   

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/pkg/util/metric.go b/pkg/util/metric.go
new file mode 100644
index 00000000..2a805298
--- /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 5b54d2fe..00000000
--- 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 ff46d32f..18dcae4f 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/rest/metric.go b/server/rest/metric.go
new file mode 100644
index 00000000..350aed81
--- /dev/null
+++ b/server/rest/metric.go
@@ -0,0 +1,92 @@
+/*
+ * 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 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"
+       "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
+}


 

----------------------------------------------------------------
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:
us...@infra.apache.org


> SC panic when report metrics concurrently
> -----------------------------------------
>
>                 Key: SCB-155
>                 URL: https://issues.apache.org/jira/browse/SCB-155
>             Project: Apache ServiceComb
>          Issue Type: Bug
>          Components: Service-Center
>            Reporter: little-cui
>             Fix For: service-center-1.0.0-m1
>
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to