/
metrics.go
93 lines (78 loc) · 3.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package migrators
import (
"github.com/prometheus/client_golang/prometheus"
k8smetrics "k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)
const (
namespace = "storage_migrator"
subsystem = "core_migrator"
)
// metrics provides access to all core migrator metrics.
var metrics *migratorMetrics
func init() {
metrics = newMigratorMetrics(legacyregistry.Register)
}
// migratorMetrics instruments core migrator with prometheus metrics.
type migratorMetrics struct {
objectsMigrated *k8smetrics.CounterVec
migration *k8smetrics.CounterVec
migrationDuration *k8smetrics.HistogramVec
}
// newMigratorMetrics create a new MigratorMetrics, configured with default metric names.
func newMigratorMetrics(registerFunc func(k8smetrics.Registerable) error) *migratorMetrics {
// objectMigrates is defined in kube-storave-version-migrator
objectsMigrated := k8smetrics.NewCounterVec(
&k8smetrics.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "migrated_objects",
Help: "The total number of objects that have been migrated, labeled with the full resource name",
}, []string{"resource"})
registerFunc(objectsMigrated)
// migration is defined in kube-storave-version-migrator
migration := k8smetrics.NewCounterVec(
&k8smetrics.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "migrations",
Help: "The total number of completed migration, labeled with the full resource name, and the status of the migration (failed or succeeded)",
}, []string{"resource", "status"})
registerFunc(migration)
// migrationDuration is not defined upstream but uses the same Namespace and Subsystem
// as the other metrics that are defined in kube-storave-version-migrator
migrationDuration := k8smetrics.NewHistogramVec(
&k8smetrics.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "migration_duration_seconds",
Help: "How long a successful migration takes in seconds, labeled with the full resource name",
Buckets: prometheus.ExponentialBuckets(120, 2, 7),
}, []string{"resource"})
registerFunc(migrationDuration)
return &migratorMetrics{
objectsMigrated: objectsMigrated,
migration: migration,
migrationDuration: migrationDuration,
}
}
func (m *migratorMetrics) Reset() {
m.objectsMigrated.Reset()
m.migration.Reset()
}
// ObserveObjectsMigrated adds the number of migrated objects for a resource type
func (m *migratorMetrics) ObserveObjectsMigrated(added int, resource string) {
m.objectsMigrated.WithLabelValues(resource).Add(float64(added))
}
// ObserveSucceededMigration increments the number of successful migrations for a resource type
func (m *migratorMetrics) ObserveSucceededMigration(resource string) {
m.migration.WithLabelValues(resource, "Succeeded").Add(float64(1))
}
// ObserveFailedMigration increments the number of failed migrations for a resource type
func (m *migratorMetrics) ObserveFailedMigration(resource string) {
m.migration.WithLabelValues(resource, "Failed").Add(float64(1))
}
// ObserveMigrationDuration records migration duration in seconds for a resource type
func (m *migratorMetrics) ObserveSucceededMigrationDuration(seconds float64, resource string) {
m.migrationDuration.WithLabelValues(resource).Observe(seconds)
}