This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
64 lines (57 loc) · 1.86 KB
/
metrics.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"net/http"
"strconv"
"time"
"rrinterceptor/promutils"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/prometheus/prompb"
)
var (
promRegistry *prometheus.Registry
driftMetric *prometheus.CounterVec
)
func initMetrics() (err error) {
driftMetric = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "rrinterceptor",
Subsystem: "queries",
Name: "startdrift",
Help: "Returns the number of requests splitted by start drift and stepping. Drift is the number of hours between query start and now rounded to 3 hours. Stepping is the number of seconds used as stepping, rouded to 15s (0 if not present).",
// ConstLabels: nil,
}, []string{
"drift",
"step",
})
promRegistry = prometheus.NewRegistry()
return promRegistry.Register(driftMetric)
}
func promHandler() http.Handler {
return promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{})
}
func updateDriftStats(req prompb.ReadRequest) {
var (
drift, step time.Duration
hourDrift, secondsStep int64
)
now := time.Now()
for _, query := range req.Queries {
// Let's be safe
if query == nil {
continue
}
// Compute rounded drift and stepping to get meaningfull values
drift = now.Sub(promutils.GetTimeFromTS(promutils.GetEffectiveStart(query)))
hourDrift = int64(drift.Round(3*time.Hour) / time.Hour)
if promutils.IsSteppingUsable(query) {
step = time.Duration(query.Hints.StepMs) * time.Millisecond
secondsStep = int64(step.Round(15*time.Second) / time.Second)
} else {
secondsStep = 0
}
// Update stats
driftMetric.WithLabelValues(strconv.FormatInt(hourDrift, 10), strconv.FormatInt(secondsStep, 10)).Inc()
log.Debugf("[Metrics] Incrementing the counter for startDrift metric with dimension: drift(%dh) step(%ds)",
hourDrift, secondsStep)
}
}