acelyc111 commented on code in PR #2285: URL: https://github.com/apache/incubator-pegasus/pull/2285#discussion_r2328722574
########## go-client/pegasus/table_connector.go: ########## @@ -715,8 +719,29 @@ 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 { + defer func() { + elapsed := time.Since(start).Nanoseconds() + pm := metrics.GetPrometheusMetrics() + status := "success" + if errResult != nil { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + status = "timeout" + } else { + status = "fail" + } + } + meta := strings.Join(p.meta.GetMetaIPAddrs(), ",") + pm.MarkMeter(fmt.Sprintf("pegasus_client_%s_%s_%s_total", p.tableName, optype.String(), status), 1, map[string]string{"meta": meta}) Review Comment: How about add more labels rather than name components? the labels in Prometheus is more flexible, we can filter or aggregation metrics by labels. ########## go-client/metrics/prometheus.go: ########## @@ -0,0 +1,202 @@ +/* + * 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) MarkMeter(counterName string, count int64, extraLabels map[string]string) { + 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("Counter for %s", counterName), Review Comment: Is this metric name too rough? We only know its name and type via this help, and they are duplicate because we know its name and type according to its declaration. ########## go-client/metrics/prometheus.go: ########## @@ -0,0 +1,202 @@ +/* + * 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) MarkMeter(counterName string, count int64, extraLabels map[string]string) { + 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("Counter for %s", counterName), + ConstLabels: pm.constLabels, + }, + varLabelKeys, + ) + }) + + counter, err := wrapper.vec.GetMetricWith(extraLabels) + if err != nil { + pegalog.GetLogger().Fatalf("Failed to get metric with extra labels %v: %v, counter name: %v", extraLabels, err, counterName) + return + } + counter.Add(float64(count)) Review Comment: Is it possible to separate its declaration and usage? -- 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: dev-unsubscr...@pegasus.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@pegasus.apache.org For additional commands, e-mail: dev-h...@pegasus.apache.org