/
metrics.go
244 lines (206 loc) · 6.91 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0
package controllers
import (
"github.com/prometheus/client_golang/prometheus"
rmn "github.com/ramendr/ramen/api/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
const (
metricNamespace = "ramen"
)
const (
DRPolicySyncIntervalSeconds = "policy_schedule_interval_seconds"
)
const (
LastSyncTimestampSeconds = "last_sync_timestamp_seconds"
LastSyncDurationSeconds = "last_sync_duration_seconds"
LastSyncDataBytes = "last_sync_data_bytes"
WorkloadProtectionStatus = "workload_protection_status"
)
type SyncTimeMetrics struct {
LastSyncTime prometheus.Gauge
}
type DRPolicySyncMetrics struct {
DRPolicySyncInterval prometheus.Gauge
}
type SyncDurationMetrics struct {
LastSyncDuration prometheus.Gauge
}
type SyncDataBytesMetrics struct {
LastSyncDataBytes prometheus.Gauge
}
type WorkloadProtectionMetrics struct {
WorkloadProtectionStatus prometheus.Gauge
}
type SyncMetrics struct {
SyncTimeMetrics
SyncDurationMetrics
SyncDataBytesMetrics
}
const (
ObjType = "obj_type"
ObjName = "obj_name"
ObjNamespace = "obj_namespace"
Policyname = "policyname"
SchedulingInterval = "scheduling_interval"
)
var (
syncTimeMetricLabelNames = []string{
ObjType, // Name of the type of the resource [drpc|vrg]
ObjName, // Name of the resource [drpc-name|vrg-name]
ObjNamespace, // DRPC namespace name
Policyname, // DRPolicy name
SchedulingInterval, // Value from DRPolicy
}
drpolicySyncIntervalMetricLabelNames = []string{
Policyname, // DRPolicy name
}
syncDurationMetricLabelNames = []string{
ObjType, // Name of the type of the resource [drpc]
ObjName, // Name of the resoure [drpc-name]
ObjNamespace, // DRPC namespace name
SchedulingInterval, // Value from DRPolicy
}
syncDataBytesMetricLabels = []string{
ObjType, // Name of the type of the resource [drpc]
ObjName, // Name of the resoure [drpc-name]
ObjNamespace, // DRPC namespace name
SchedulingInterval, // Value from DRPolicy
}
workloadProtectionStatusLabels = []string{
ObjType, // Name of the type of the resource [drpc]
ObjName, // Name of the resoure [drpc-name]
ObjNamespace, // DRPC namespace
}
)
var (
lastSyncTime = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: LastSyncTimestampSeconds,
Namespace: metricNamespace,
Help: "Duration of last sync time in seconds",
},
syncTimeMetricLabelNames,
)
dRPolicySyncInterval = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: DRPolicySyncIntervalSeconds,
Namespace: metricNamespace,
Help: "Schedule interval for a policy in seconds",
},
drpolicySyncIntervalMetricLabelNames,
)
lastSyncDuration = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: LastSyncDurationSeconds,
Namespace: metricNamespace,
Help: "Duration of max sync time in seconds",
},
syncDurationMetricLabelNames,
)
lastSyncDataBytes = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: LastSyncDataBytes,
Namespace: metricNamespace,
Help: "Total data synced in bytes since last sync",
},
syncDataBytesMetricLabels,
)
workloadProtectionStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: WorkloadProtectionStatus,
Namespace: metricNamespace,
Help: "Status regarding workload protection health",
},
workloadProtectionStatusLabels,
)
)
// lastSyncTime metrics reports value from lastGrpupSyncTime taken from DRPC status
func SyncTimeMetricLabels(drPolicy *rmn.DRPolicy, drpc *rmn.DRPlacementControl) prometheus.Labels {
return prometheus.Labels{
ObjType: "DRPlacementControl",
ObjName: drpc.Name,
ObjNamespace: drpc.Namespace,
Policyname: drPolicy.Name,
SchedulingInterval: drPolicy.Spec.SchedulingInterval,
}
}
func NewSyncTimeMetric(labels prometheus.Labels) SyncTimeMetrics {
return SyncTimeMetrics{
LastSyncTime: lastSyncTime.With(labels),
}
}
func DeleteSyncTimeMetric(labels prometheus.Labels) bool {
return lastSyncTime.Delete(labels)
}
// dRPolicySyncInterval Metrics reports the value from schedulingInterval from DRPolicy
func DRPolicySyncIntervalMetricLabels(drPolicy *rmn.DRPolicy) prometheus.Labels {
return prometheus.Labels{Policyname: drPolicy.Name}
}
func NewDRPolicySyncIntervalMetrics(labels prometheus.Labels) DRPolicySyncMetrics {
return DRPolicySyncMetrics{
DRPolicySyncInterval: dRPolicySyncInterval.With(labels),
}
}
func DeleteDRPolicySyncIntervalMetrics(labels prometheus.Labels) bool {
return dRPolicySyncInterval.Delete(labels)
}
// lastSyncDuration Metrics reports value from lastGroupSyncDuration from DRPC status
func SyncDurationMetricLabels(drPolicy *rmn.DRPolicy, drpc *rmn.DRPlacementControl) prometheus.Labels {
return prometheus.Labels{
ObjType: "DRPlacementControl",
ObjName: drpc.Name,
ObjNamespace: drpc.Namespace,
SchedulingInterval: drPolicy.Spec.SchedulingInterval,
}
}
func NewSyncDurationMetric(labels prometheus.Labels) SyncDurationMetrics {
return SyncDurationMetrics{
LastSyncDuration: lastSyncDuration.With(labels),
}
}
func DeleteSyncDurationMetric(labels prometheus.Labels) bool {
return lastSyncDuration.Delete(labels)
}
// lastSyncDataBytes Metric reports value from lastGroupSyncBytes taken from DRPC status
func SyncDataBytesMetricLabels(drPolicy *rmn.DRPolicy, drpc *rmn.DRPlacementControl) prometheus.Labels {
return prometheus.Labels{
ObjType: "DRPlacementControl",
ObjName: drpc.Name,
ObjNamespace: drpc.Namespace,
SchedulingInterval: drPolicy.Spec.SchedulingInterval,
}
}
func NewSyncDataBytesMetric(labels prometheus.Labels) SyncDataBytesMetrics {
return SyncDataBytesMetrics{
LastSyncDataBytes: lastSyncDataBytes.With(labels),
}
}
func DeleteSyncDataBytesMetric(labels prometheus.Labels) bool {
return lastSyncDataBytes.Delete(labels)
}
// workloadProtectionStatus Metric reports information regarding workload protection condition from DRPC
func WorkloadProtectionStatusLabels(drpc *rmn.DRPlacementControl) prometheus.Labels {
return prometheus.Labels{
ObjType: "DRPlacementControl",
ObjName: drpc.Name,
ObjNamespace: drpc.Namespace,
}
}
func NewWorkloadProtectionStatusMetric(labels prometheus.Labels) WorkloadProtectionMetrics {
return WorkloadProtectionMetrics{
WorkloadProtectionStatus: workloadProtectionStatus.With(labels),
}
}
func DeleteWorkloadProtectionStatusMetric(labels prometheus.Labels) bool {
return workloadProtectionStatus.Delete(labels)
}
func init() {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(dRPolicySyncInterval)
metrics.Registry.MustRegister(lastSyncTime)
metrics.Registry.MustRegister(lastSyncDuration)
metrics.Registry.MustRegister(lastSyncDataBytes)
metrics.Registry.MustRegister(workloadProtectionStatus)
}