-
Notifications
You must be signed in to change notification settings - Fork 237
/
metrics.go
48 lines (40 loc) · 1.47 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
package metrics
import (
"time"
"github.com/k3s-io/kine/pkg/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
const (
ResultSuccess = "success"
ResultError = "error"
)
var (
SQLTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "kine_sql_total",
Help: "Total number of SQL operations",
}, []string{"error_code"})
SQLTime = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "kine_sql_time_seconds",
Help: "Length of time per SQL operation",
Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30},
}, []string{"error_code"})
CompactTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "kine_compact_total",
Help: "Total number of compactions",
}, []string{"result"})
)
var (
// SlowSQLThreshold is a duration which SQL executed longer than will be logged.
// This can be directly modified to override the default value when kine is used as a library.
SlowSQLThreshold = time.Second
)
func ObserveSQL(start time.Time, errCode string, sql util.Stripped, args ...interface{}) {
SQLTotal.WithLabelValues(errCode).Inc()
duration := time.Since(start)
SQLTime.WithLabelValues(errCode).Observe(duration.Seconds())
if SlowSQLThreshold > 0 && duration >= SlowSQLThreshold {
logrus.Infof("Slow SQL (started: %v) (total time: %v): %s : %v", start, duration, sql, args)
}
}