Copilot commented on code in PR #2285:
URL:
https://github.com/apache/incubator-pegasus/pull/2285#discussion_r2361924092
##########
go-client/pegasus/table_connector.go:
##########
@@ -715,8 +719,44 @@ func (p *pegasusTableConnector) Incr(ctx context.Context,
hashKey []byte, sortKe
}
func (p *pegasusTableConnector) runPartitionOp(ctx context.Context, hashKey
[]byte, req op.Request, optype OpType) (interface{}, error) {
+ start := time.Now()
+ var errResult error
+ if p.enableMetrics {
Review Comment:
The `time.Now()` call and elapsed time calculation are executed even when
metrics are disabled. Move these operations inside the metrics check to avoid
unnecessary overhead when metrics are disabled.
```suggestion
var errResult error
if p.enableMetrics {
start := time.Now()
```
##########
go-client/metrics/prometheus.go:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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 metrics
+
+import (
+ "fmt"
+ "net/http"
+ "os"
+ "sort"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/apache/incubator-pegasus/go-client/config"
+ "github.com/apache/incubator-pegasus/go-client/pegalog"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
+)
+
+// PrometheusMetrics is the metrics implementation for Prometheus.
+type PrometheusMetrics struct {
+ registry prometheus.Registerer
+ counterMap sync.Map
+ summaryMap sync.Map
+ constLabels prometheus.Labels
+}
+
+var (
+ singletonMetrics *PrometheusMetrics
+ initPrometheusMetricsOnce sync.Once
+ startServerOnce sync.Once
+ constLabels map[string]string
+ initRegistry prometheus.Registerer
+)
+
+func InitMetrics(registry prometheus.Registerer, cfg config.Config) {
+ initRegistry = registry
+ constLabels = cfg.PrometheusConstLabels
+ if cfg.EnablePrometheus {
+ startServerOnce.Do(func() {
+ port := 9090
+ if cfg.PrometheusPort > 0 {
+ port = cfg.PrometheusPort
+ }
+ go func() {
+ http.Handle("/metrics", promhttp.Handler())
+ addr := fmt.Sprintf(":%d", port)
+ pegalog.GetLogger().Print("Starting Prometheus
metrics server on", addr)
+ if err := http.ListenAndServe(addr, nil); err
!= nil {
+ pegalog.GetLogger().Fatal("Failed to
start Prometheus metrics server:", err)
+ }
+ }()
+ })
+ }
+}
+
+// GetPrometheusMetrics get singleton PrometheusMetrics
+func GetPrometheusMetrics() *PrometheusMetrics {
+ initPrometheusMetricsOnce.Do(func() {
+ if initRegistry == nil {
+ initRegistry = prometheus.DefaultRegisterer
+ }
+
+ labels := prometheus.Labels{}
+ for k, v := range constLabels {
+ labels[k] = v
+ }
+ endpoint := GetLocalHostName()
+ labels["endpoint"] = endpoint
+
+ singletonMetrics = &PrometheusMetrics{
+ registry: initRegistry,
+ constLabels: labels,
+ counterMap: sync.Map{},
+ summaryMap: sync.Map{},
+ }
+ })
+ return singletonMetrics
+}
+
+func (pm *PrometheusMetrics) GetOrCreateCounter(counterName string,
extraLabels map[string]string) (prometheus.Counter, error) {
+ var varLabelKeys []string
+ if extraLabels != nil {
+ for k := range extraLabels {
+ varLabelKeys = append(varLabelKeys, k)
+ }
+ }
+ sort.Strings(varLabelKeys)
+ key := fmt.Sprintf("%s-%s-%s", counterName,
mapToString(pm.constLabels), strings.Join(varLabelKeys, ","))
+
+ type onceCounterVec struct {
+ once sync.Once
+ vec *prometheus.CounterVec
+ }
+
+ val, _ := pm.counterMap.LoadOrStore(key, &onceCounterVec{})
+ wrapper := val.(*onceCounterVec)
+
+ wrapper.once.Do(func() {
+ wrapper.vec = promauto.With(pm.registry).NewCounterVec(
+ prometheus.CounterOpts{
+ Name: counterName,
+ Help: fmt.Sprintf("Total count of
Pegasus client operations. Labels: op(operation type), status(result:
success/fail/timeout), table(target table name), meta(meta server address)"),
+ ConstLabels: pm.constLabels,
+ },
+ varLabelKeys,
+ )
+ })
+
+ return wrapper.vec.GetMetricWith(extraLabels)
+}
+
+func (pm *PrometheusMetrics) MarkMeter(counter prometheus.Counter, count
int64) {
+ counter.Add(float64(count))
+}
+
+func (pm *PrometheusMetrics) GetOrCreateSummary(summaryName string,
extraLabels map[string]string) (prometheus.Observer, error) {
+ var varLabelKeys []string
+ if extraLabels != nil {
+ for k := range extraLabels {
+ varLabelKeys = append(varLabelKeys, k)
+ }
+ }
+ sort.Strings(varLabelKeys)
+
+ key := fmt.Sprintf("%s-%s-%s", summaryName,
mapToString(pm.constLabels), strings.Join(varLabelKeys, ","))
+
+ type onceSummary struct {
+ once sync.Once
+ summaryVec *prometheus.SummaryVec
+ }
+
+ val, _ := pm.summaryMap.LoadOrStore(key, &onceSummary{})
+ wrapper := val.(*onceSummary)
+
+ wrapper.once.Do(func() {
+ summaryVec := promauto.With(pm.registry).NewSummaryVec(
+ prometheus.SummaryOpts{
+ Name: summaryName,
+ Help: fmt.Sprintf("Summary of Pegasus
client operation latency. Labels: op(operation type), status(result:
success/fail/timeout), table(target table name), meta(meta server address)"),
Review Comment:
Similar to the counter help text, this hardcoded description is used for all
summaries. The help text should be specific to the metric being created or made
configurable.
##########
go-client/metrics/prometheus.go:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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 metrics
+
+import (
+ "fmt"
+ "net/http"
+ "os"
+ "sort"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/apache/incubator-pegasus/go-client/config"
+ "github.com/apache/incubator-pegasus/go-client/pegalog"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
+)
+
+// PrometheusMetrics is the metrics implementation for Prometheus.
+type PrometheusMetrics struct {
+ registry prometheus.Registerer
+ counterMap sync.Map
+ summaryMap sync.Map
+ constLabels prometheus.Labels
+}
+
+var (
+ singletonMetrics *PrometheusMetrics
+ initPrometheusMetricsOnce sync.Once
+ startServerOnce sync.Once
+ constLabels map[string]string
+ initRegistry prometheus.Registerer
+)
+
+func InitMetrics(registry prometheus.Registerer, cfg config.Config) {
+ initRegistry = registry
+ constLabels = cfg.PrometheusConstLabels
+ if cfg.EnablePrometheus {
+ startServerOnce.Do(func() {
+ port := 9090
+ if cfg.PrometheusPort > 0 {
+ port = cfg.PrometheusPort
+ }
+ go func() {
+ http.Handle("/metrics", promhttp.Handler())
+ addr := fmt.Sprintf(":%d", port)
+ pegalog.GetLogger().Print("Starting Prometheus
metrics server on", addr)
+ if err := http.ListenAndServe(addr, nil); err
!= nil {
+ pegalog.GetLogger().Fatal("Failed to
start Prometheus metrics server:", err)
+ }
+ }()
+ })
+ }
+}
+
+// GetPrometheusMetrics get singleton PrometheusMetrics
+func GetPrometheusMetrics() *PrometheusMetrics {
+ initPrometheusMetricsOnce.Do(func() {
+ if initRegistry == nil {
+ initRegistry = prometheus.DefaultRegisterer
+ }
+
+ labels := prometheus.Labels{}
+ for k, v := range constLabels {
+ labels[k] = v
+ }
+ endpoint := GetLocalHostName()
+ labels["endpoint"] = endpoint
+
+ singletonMetrics = &PrometheusMetrics{
+ registry: initRegistry,
+ constLabels: labels,
+ counterMap: sync.Map{},
+ summaryMap: sync.Map{},
+ }
+ })
+ return singletonMetrics
+}
+
+func (pm *PrometheusMetrics) GetOrCreateCounter(counterName string,
extraLabels map[string]string) (prometheus.Counter, error) {
+ var varLabelKeys []string
+ if extraLabels != nil {
+ for k := range extraLabels {
+ varLabelKeys = append(varLabelKeys, k)
+ }
+ }
+ sort.Strings(varLabelKeys)
+ key := fmt.Sprintf("%s-%s-%s", counterName,
mapToString(pm.constLabels), strings.Join(varLabelKeys, ","))
+
+ type onceCounterVec struct {
+ once sync.Once
+ vec *prometheus.CounterVec
+ }
+
+ val, _ := pm.counterMap.LoadOrStore(key, &onceCounterVec{})
+ wrapper := val.(*onceCounterVec)
+
+ wrapper.once.Do(func() {
+ wrapper.vec = promauto.With(pm.registry).NewCounterVec(
+ prometheus.CounterOpts{
+ Name: counterName,
+ Help: fmt.Sprintf("Total count of
Pegasus client operations. Labels: op(operation type), status(result:
success/fail/timeout), table(target table name), meta(meta server address)"),
Review Comment:
The hardcoded help text in `fmt.Sprintf` creates identical descriptions for
all counters regardless of their actual purpose. Consider making the help text
dynamic based on the counter name or pass it as a parameter.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]