You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2022/08/15 07:46:19 UTC

[GitHub] [dubbo-go-pixiu] baerwang commented on a diff in pull request #468: [ASoC 2022] Pixiu Metrics Implementation

baerwang commented on code in PR #468:
URL: https://github.com/apache/dubbo-go-pixiu/pull/468#discussion_r938628801


##########
pkg/metrics/collector/api_health.go:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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 collector
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"net/url"
+	"path"
+)
+import (
+	"github.com/prometheus/client_golang/prometheus"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/logger"
+	"github.com/apache/dubbo-go-pixiu/pkg/metrics/global"
+)
+
+// A collection of many Api states
+type ApiStatsResponse struct {
+	Share    ApiStatShare `json:"share"`
+	ApiStats []ApiStat    `json:"api_stats"`
+}
+
+//The state shared by the Api

Review Comment:
   space



##########
pkg/metrics/collector/api_health.go:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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 collector
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"net/url"
+	"path"
+)
+
+import (
+	"github.com/prometheus/client_golang/prometheus"
+)
+
+import (
+	"github.com/apache/dubbo-go-pixiu/pkg/logger"
+	"github.com/apache/dubbo-go-pixiu/pkg/metrics/global"
+)
+
+type ApiStatsResponse struct {
+	Share    ApiStatShare `json:"share"`
+	ApiStats []ApiStat    `json:"api_stats"`
+}
+
+type ApiStatShare struct {
+	Total      int64 `json:"total"`
+	Successful int64 `json:"successful"`
+	Failed     int64 `json:"failed"`
+}
+
+type ApiStat struct {
+	ApiName            string `json:"api_name"`
+	ApiRequests        int64  `json:"api_requests"`
+	ApiRequestsLatency int64  `json:"api_requests_latency"`
+}
+
+type apiMetric struct {
+	Type   prometheus.ValueType
+	Desc   *prometheus.Desc
+	Value  func(dataStreamStats ApiStat) float64
+	Labels func(dataStreamStats ApiStat) []string
+}
+
+var (
+	defaultApiMetricLabels      = []string{"api"}
+	defaultApiMetricLabelValues = func(apiStat ApiStat) []string {
+		return []string{apiStat.ApiName}
+	}
+)
+
+type ApiHealth struct {
+	logger logger.Logger
+	client *http.Client
+	url    *url.URL
+
+	up                prometheus.Gauge
+	totalScrapes      prometheus.Counter
+	jsonParseFailures prometheus.Counter
+
+	apiMetrics []*apiMetric
+}
+
+func NewApiHealth(logger logger.Logger, client *http.Client, url *url.URL) *ApiHealth {
+	return &ApiHealth{
+		logger: logger,
+		client: client,
+		url:    url,
+
+		up: prometheus.NewGauge(prometheus.GaugeOpts{
+			Name: prometheus.BuildFQName(global.Namespace, "api_stats", "up"),
+			Help: "Was the last scrape of the Pixiu Api Stat Data successful.",
+		}),
+		totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
+			Name: prometheus.BuildFQName(global.Namespace, "api_stats", "total_scrapes"),
+			Help: "Current total Pixiu Api scrapes.",
+		}),
+		jsonParseFailures: prometheus.NewCounter(prometheus.CounterOpts{
+			Name: prometheus.BuildFQName(global.Namespace, "api_stats", "json_parse_failures"),
+			Help: "Number of errors while parsing JSON.",
+		}),
+		apiMetrics: []*apiMetric{
+			{
+				Type: prometheus.CounterValue,
+				Desc: prometheus.NewDesc(
+					prometheus.BuildFQName(global.Namespace, "api_stats", "api_Requests_total"),
+					"Number of Api Requests",
+					defaultApiMetricLabels, nil,
+				),
+				Value: func(apiStats ApiStat) float64 {
+					return float64(apiStats.ApiRequests)
+				},
+				Labels: defaultApiMetricLabelValues,
+			},
+		},
+	}
+}
+
+func (ds *ApiHealth) Describe(ch chan<- *prometheus.Desc) {
+	for _, metric := range ds.apiMetrics {
+		ch <- metric.Desc
+	}
+
+	ch <- ds.up.Desc()

Review Comment:
   多个channel 改成一个channel



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org