-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
67 lines (62 loc) · 2.13 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
65
66
67
package backup
import (
"math"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
// metrics
var (
backupSize = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "size",
Namespace: "saas_redis_backup",
Help: `"size of the latest backup in bytes"`,
},
[]string{"shard"})
backupFailureCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "failure_count",
Namespace: "saas_redis_backup",
Help: `"total number of backup failures"`,
},
[]string{"shard"})
backupSuccessCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "success_count",
Namespace: "saas_redis_backup",
Help: `"total number of backup successes"`,
},
[]string{"shard"})
backupDuration = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "duration",
Namespace: "saas_redis_backup",
Help: `"seconds it took to complete the backup"`,
},
[]string{"shard"})
)
func init() {
// Register backup metrics with the global prometheus registry
metrics.Registry.MustRegister(
backupSize, backupFailureCount, backupDuration, backupSuccessCount,
)
}
func (r *Runner) publishMetrics() {
// ensure counters are initialized
if err := backupSuccessCount.With(prometheus.Labels{"shard": r.ShardName}).Write(&dto.Metric{}); err != nil {
backupFailureCount.With(prometheus.Labels{"shard": r.ShardName}).Add(0)
}
if err := backupFailureCount.With(prometheus.Labels{"shard": r.ShardName}).Write(&dto.Metric{}); err != nil {
backupFailureCount.With(prometheus.Labels{"shard": r.ShardName}).Add(0)
}
// update metrics
if r.status.Error != nil {
backupSize.With(prometheus.Labels{"shard": r.ShardName}).Set(float64(0))
backupFailureCount.With(prometheus.Labels{"shard": r.ShardName}).Inc()
} else {
backupSize.With(prometheus.Labels{"shard": r.ShardName}).Set(float64(r.status.BackupSize))
backupDuration.With(prometheus.Labels{"shard": r.ShardName}).Set(math.Round(r.status.FinishedAt.Sub(r.Timestamp).Seconds()))
backupSuccessCount.With(prometheus.Labels{"shard": r.ShardName}).Inc()
}
}