-
Notifications
You must be signed in to change notification settings - Fork 693
Expand file tree
/
Copy pathhedged_requests.go
More file actions
50 lines (44 loc) · 1.29 KB
/
hedged_requests.go
File metadata and controls
50 lines (44 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package frontend
import (
"net/http"
"time"
"github.com/cristalhq/hedgedhttp"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
hedgedMetricsPublishDuration = 10 * time.Second
)
var (
hedgedRequestsMetrics = promauto.NewGauge(
prometheus.GaugeOpts{
Namespace: "tempo",
Name: "query_frontend_hedged_roundtrips_total",
Help: "Total number of hedged search requests. Registered as a gauge for code sanity. This is a counter.",
},
)
)
func newHedgedRequestWare(hedgeRequestsAt time.Duration, hedgeRequestsUpTo int) Middleware {
return MiddlewareFunc(func(r http.RoundTripper) http.RoundTripper {
ret, stats, err := hedgedhttp.NewRoundTripperAndStats(hedgeRequestsAt, hedgeRequestsUpTo, r)
if err != nil {
panic(err)
}
publishHedgedMetrics(stats)
return ret
})
}
// PublishHedgedMetrics flushes metrics from hedged requests every 10 seconds
func publishHedgedMetrics(s *hedgedhttp.Stats) {
ticker := time.NewTicker(hedgedMetricsPublishDuration)
go func() {
for range ticker.C {
snap := s.Snapshot()
hedgedRequests := int64(snap.ActualRoundTrips) - int64(snap.RequestedRoundTrips)
if hedgedRequests < 0 {
hedgedRequests = 0
}
hedgedRequestsMetrics.Set(float64(hedgedRequests))
}
}()
}