Skip to content

Commit

Permalink
Merge pull request #27 from vinted/add_tweaks
Browse files Browse the repository at this point in the history
gate: add metric for wait
  • Loading branch information
GiedriusS authored Sep 29, 2022
2 parents a44a550 + db8652f commit 9575ea9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion storage/remote/read_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewReadHandler(logger log.Logger, r prometheus.Registerer, queryable storag
queryable: queryable,
config: config,
remoteReadSampleLimit: remoteReadSampleLimit,
remoteReadGate: gate.New(remoteReadConcurrencyLimit),
remoteReadGate: gate.New(remoteReadConcurrencyLimit, "read_handler", r),
remoteReadMaxBytesInFrame: remoteReadMaxBytesInFrame,

queries: prometheus.NewGauge(prometheus.GaugeOpts{
Expand Down
25 changes: 22 additions & 3 deletions util/gate/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,42 @@

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.Histogram
}

// 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).NewHistogram(prometheus.HistogramOpts{
Namespace: "prometheus",
Subsystem: "api",
Help: "How long was the wait at the gate before the query execution",
Buckets: prometheus.ExponentialBuckets(0.1, 2, 10),
Name: "gate_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.Observe(float64(time.Since(startTime).Seconds()))
}()
select {
case <-ctx.Done():
return ctx.Err()
Expand Down

0 comments on commit 9575ea9

Please sign in to comment.