Explorar o código

Merge pull request #6926 from xiang90/metrics

grpcproxy: add richer metrics for watch
Xiang Li %!s(int64=9) %!d(string=hai) anos
pai
achega
1f954dc9f4
Modificáronse 2 ficheiros con 51 adicións e 0 borrados
  1. 37 0
      proxy/grpcproxy/metrics.go
  2. 14 0
      proxy/grpcproxy/watch_broadcast.go

+ 37 - 0
proxy/grpcproxy/metrics.go

@@ -0,0 +1,37 @@
+// Copyright 2016 The etcd Authors
+//
+// Licensed 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 grpcproxy
+
+import "github.com/prometheus/client_golang/prometheus"
+
+var (
+	watchersCoalescing = prometheus.NewGauge(prometheus.GaugeOpts{
+		Namespace: "etcd",
+		Subsystem: "grpc_proxy",
+		Name:      "watchers_coalescing_total",
+		Help:      "Total number of current watchers coalescing",
+	})
+	eventsCoalescing = prometheus.NewCounter(prometheus.CounterOpts{
+		Namespace: "etcd",
+		Subsystem: "grpc_proxy",
+		Name:      "events_coalescing_total",
+		Help:      "Total number of events coalescing",
+	})
+)
+
+func init() {
+	prometheus.MustRegister(watchersCoalescing)
+	prometheus.MustRegister(eventsCoalescing)
+}

+ 14 - 0
proxy/grpcproxy/watch_broadcast.go

@@ -89,6 +89,9 @@ func (wb *watchBroadcast) bcast(wr clientv3.WatchResponse) {
 	for r := range wb.receivers {
 		r.send(wr)
 	}
+	if wb.size() > 0 {
+		eventsCoalescing.Add(float64(wb.size() - 1))
+	}
 }
 
 // add puts a watcher into receiving a broadcast if its revision at least
@@ -121,6 +124,8 @@ func (wb *watchBroadcast) add(w *watcher) bool {
 		return false
 	}
 	wb.receivers[w] = struct{}{}
+	watchersCoalescing.Inc()
+
 	return true
 }
 func (wb *watchBroadcast) delete(w *watcher) {
@@ -130,6 +135,10 @@ func (wb *watchBroadcast) delete(w *watcher) {
 		panic("deleting missing watcher from broadcast")
 	}
 	delete(wb.receivers, w)
+	if !wb.empty() {
+		// do not dec the only left watcher for coalescing.
+		watchersCoalescing.Dec()
+	}
 }
 
 func (wb *watchBroadcast) size() int {
@@ -141,6 +150,11 @@ func (wb *watchBroadcast) size() int {
 func (wb *watchBroadcast) empty() bool { return wb.size() == 0 }
 
 func (wb *watchBroadcast) stop() {
+	if !wb.empty() {
+		// do not dec the only left watcher for coalescing.
+		watchersCoalescing.Sub(float64(wb.size() - 1))
+	}
+
 	wb.cancel()
 	<-wb.donec
 }