...
1 package metrics
2
3 import (
4 "github.com/prometheus/client_golang/prometheus"
5 ctrl "sigs.k8s.io/controller-runtime"
6
7 v1patch "edge-infra.dev/pkg/sds/patching/k8s/apis/ienpatch/v1"
8
9 "edge-infra.dev/pkg/k8s/runtime/controller/metrics"
10 runtimeMetrics "edge-infra.dev/pkg/k8s/runtime/controller/metrics"
11 )
12
13 type Metrics = runtimeMetrics.Metrics
14
15 func PatchingMetrics(mgr ctrl.Manager) Metrics {
16 metrics := runtimeMetrics.New(mgr, "patchctl",
17 runtimeMetrics.WithSuspend(),
18 runtimeMetrics.WithCollectors(
19 PatchctlReadinessMetric,
20 PatchctlDurationMetric,
21 ),
22 )
23 return metrics
24 }
25
26 type ReconcileTimeSet map[string]float64
27
28 type IControllerMetrics interface {
29 RecordReadiness()
30 recordDuration(controllerReconcileTime float64)
31 RecordMetrics(controllerReconcileTime float64)
32 }
33
34 type ControllerMetrics struct {
35 patch *v1patch.IENPatch
36 hostname string
37 currentVer string
38 targetVer string
39 }
40
41 var PatchctlReadinessMetric prometheus.GaugeVec = *prometheus.NewGaugeVec(
42 prometheus.GaugeOpts{
43 Name: metrics.Name("patch_manager", "reconcile_readiness"),
44 Help: "Metric for patching readiness",
45 },
46 []string{"hostname", "current_version", "target_version", "status", "message"})
47
48 var PatchctlDurationMetric prometheus.HistogramVec = *prometheus.NewHistogramVec(
49 prometheus.HistogramOpts{
50 Name: metrics.Name("patch_manager", "reconcile_duration_seconds"),
51 Help: "Metric for patch controllers and plugin reconcile duration in seconds",
52 Buckets: prometheus.LinearBuckets(0.0, 0.2, 10),
53 },
54 []string{"hostname", "current_version", "target_version", "status", "message"})
55
56 var lastPatchctlReadinessLabels prometheus.Labels
57 var lastPatchctlDurationLabels prometheus.Labels
58
59 func CreateNewControllerMetrics(patch *v1patch.IENPatch, hostname, currentVer, targetVer string) *ControllerMetrics {
60 return &ControllerMetrics{
61 patch: patch,
62 hostname: hostname,
63 currentVer: currentVer,
64 targetVer: targetVer,
65 }
66 }
67
68 func (m *ControllerMetrics) RecordReadiness() {
69 for _, condition := range m.patch.Status.Conditions {
70 if condition.Type != m.hostname {
71 continue
72 }
73 labels := prometheus.Labels{
74 "hostname": m.hostname,
75 "target_version": m.targetVer,
76 "current_version": m.currentVer,
77 "status": condition.Reason,
78 "message": condition.Message,
79 }
80 if len(lastPatchctlReadinessLabels) != 0 {
81 PatchctlReadinessMetric.Delete(lastPatchctlReadinessLabels)
82 }
83 if condition.Status == "True" {
84 PatchctlReadinessMetric.With(labels).Set(1)
85 } else {
86 PatchctlReadinessMetric.With(labels).Set(0)
87 }
88 lastPatchctlReadinessLabels = labels
89 break
90 }
91 }
92
93 func (m *ControllerMetrics) recordDuration(controllerReconcileTime float64) {
94 for _, condition := range m.patch.Status.Conditions {
95 if condition.Type != m.hostname {
96 continue
97 }
98 labels := prometheus.Labels{
99 "hostname": m.hostname,
100 "target_version": m.targetVer,
101 "current_version": m.currentVer,
102 "status": condition.Reason,
103 "message": condition.Message,
104 }
105 if len(lastPatchctlDurationLabels) != 0 {
106 PatchctlDurationMetric.Delete(lastPatchctlDurationLabels)
107 }
108 PatchctlDurationMetric.With(labels).Observe(controllerReconcileTime)
109 lastPatchctlDurationLabels = labels
110 break
111 }
112 }
113
114 func (m *ControllerMetrics) RecordMetrics(controllerReconcileTime float64) {
115 m.RecordReadiness()
116 m.recordDuration(controllerReconcileTime)
117 }
118
View as plain text