From c9d5fe9f41dd646f448b3bb3748c01ea9ea9787c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Giedrius=20Statkevi=C4=8Dius?= Date: Tue, 19 Dec 2023 09:54:30 +0200 Subject: [PATCH] gate: implement wait metric MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Giedrius Statkevičius --- util/gate/gate.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/util/gate/gate.go b/util/gate/gate.go index 6cb9d583c6c..62aa19e26f9 100644 --- a/util/gate/gate.go +++ b/util/gate/gate.go @@ -13,23 +13,41 @@ package gate -import "context" +import ( + "context" + "time" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) // A Gate controls the maximum number of concurrently running and waiting queries. type Gate struct { - ch chan struct{} + ch chan struct{} + waitDuration prometheus.Counter } // New returns a query gate that limits the number of queries // being concurrently executed. -func New(length int) *Gate { +func New(length int, name string, r prometheus.Registerer) *Gate { return &Gate{ ch: make(chan struct{}, length), + waitDuration: promauto.With(r).NewCounter(prometheus.CounterOpts{ + Namespace: "prometheus", + Subsystem: "api", + Help: "Total sum of how long was the wait at the gate before query execution", + Name: "gate_total_wait_duration_seconds", + ConstLabels: prometheus.Labels{"name": name}, + }), } } // Start blocks until the gate has a free spot or the context is done. func (g *Gate) Start(ctx context.Context) error { + startTime := time.Now() + defer func() { + g.waitDuration.Add(time.Since(startTime).Seconds()) + }() select { case <-ctx.Done(): return ctx.Err()