/
metrics.go
50 lines (42 loc) · 1.17 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
package metrics
import (
"time"
"github.com/prometheus/client_golang/prometheus"
)
const (
registryNamespace = "openshift"
registrySubsystem = "registry"
)
var (
RegistryAPIRequests *prometheus.SummaryVec
)
// Register the metrics.
func Register() {
RegistryAPIRequests = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: registryNamespace,
Subsystem: registrySubsystem,
Name: "request_duration_seconds",
Help: "Request latency summary in microseconds for each operation",
},
[]string{"operation", "name"},
)
prometheus.MustRegister(RegistryAPIRequests)
}
// NewTimer wraps the SummaryVec and used to track amount of time passed since the Timer was created.
func NewTimer(collector *prometheus.SummaryVec, labels []string) *metricTimer {
return &metricTimer{
collector: collector,
labels: labels,
startTime: time.Now(),
}
}
type metricTimer struct {
collector *prometheus.SummaryVec
labels []string
startTime time.Time
}
// Stop records the duration passed since the Timer was created with NewTimer.
func (m *metricTimer) Stop() {
m.collector.WithLabelValues(m.labels...).Observe(float64(time.Since(m.startTime) / time.Second))
}