Skip to content

Commit 763798b

Browse files
authored
Use native metrics instead of collector (#395)
1 parent 73dc9f9 commit 763798b

File tree

4 files changed

+72
-197
lines changed

4 files changed

+72
-197
lines changed

cmd/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ func main() {
308308
os.Exit(1)
309309
}
310310

311-
if err := monitoring.SetupMetrics(); err != nil {
312-
setupLog.Error(err, "unable to set up metrics")
311+
if err := monitoring.RegisterMetrics(); err != nil {
312+
setupLog.Error(err, "unable to register metrics")
313313
os.Exit(1)
314314
}
315315

internal/controller/coralogix/coralogix-reconciler/coralogix_reconciler.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
cxsdk "github.com/coralogix/coralogix-management-sdk/go"
3636

3737
"github.com/coralogix/coralogix-operator/internal/config"
38+
"github.com/coralogix/coralogix-operator/internal/monitoring"
3839
"github.com/coralogix/coralogix-operator/internal/utils"
3940
)
4041

@@ -196,6 +197,19 @@ func ManageErrorWithRequeue(ctx context.Context, obj client.Object, reason strin
196197
}
197198
}
198199

200+
monitoring.DeleteResourceInfoMetric(
201+
obj.GetObjectKind().GroupVersionKind().Kind,
202+
obj.GetName(),
203+
obj.GetNamespace(),
204+
"synced",
205+
)
206+
monitoring.SetResourceInfoMetric(
207+
obj.GetObjectKind().GroupVersionKind().Kind,
208+
obj.GetName(),
209+
obj.GetNamespace(),
210+
"unsynced",
211+
)
212+
199213
return reconcile.Result{}, err
200214
}
201215

@@ -211,6 +225,19 @@ func ManageSuccessWithRequeue(ctx context.Context, obj client.Object,
211225
}
212226
}
213227

228+
monitoring.DeleteResourceInfoMetric(
229+
obj.GetObjectKind().GroupVersionKind().Kind,
230+
obj.GetName(),
231+
obj.GetNamespace(),
232+
"unsynced",
233+
)
234+
monitoring.SetResourceInfoMetric(
235+
obj.GetObjectKind().GroupVersionKind().Kind,
236+
obj.GetName(),
237+
obj.GetNamespace(),
238+
"synced",
239+
)
240+
214241
return reconcile.Result{RequeueAfter: interval}, nil
215242
}
216243

internal/monitoring/collectors.go

Lines changed: 0 additions & 168 deletions
This file was deleted.

internal/monitoring/metrics.go

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ import (
2222

2323
var metricsLog = logf.Log.WithName("metrics")
2424

25-
func SetupMetrics() error {
26-
metricsLog.V(1).Info("Setting up metrics")
27-
if err := RegisterMetrics(); err != nil {
28-
return err
29-
}
30-
31-
if err := RegisterCollectors(); err != nil {
32-
return err
33-
}
34-
35-
return nil
36-
}
37-
3825
func RegisterMetrics() error {
3926
metricsLog.V(1).Info("Registering metrics")
4027
for _, metric := range metricsList {
@@ -50,15 +37,32 @@ func RegisterMetrics() error {
5037

5138
var metricsList = []prometheus.Collector{
5239
operatorInfoMetric,
40+
resourceInfoMetric,
5341
resourceRejectionsTotalMetric,
5442
}
5543

56-
var operatorInfoMetric = prometheus.NewGaugeVec(
57-
prometheus.GaugeOpts{
58-
Name: "cx_operator_info",
59-
Help: "Coralogix Operator information.",
60-
},
61-
[]string{"go_version", "operator_version", "coralogix_url"},
44+
var (
45+
operatorInfoMetric = prometheus.NewGaugeVec(
46+
prometheus.GaugeOpts{
47+
Name: "cx_operator_info",
48+
Help: "Coralogix Operator information.",
49+
},
50+
[]string{"go_version", "operator_version", "coralogix_url"},
51+
)
52+
resourceInfoMetric = prometheus.NewGaugeVec(
53+
prometheus.GaugeOpts{
54+
Name: "cx_operator_resource_info",
55+
Help: "Coralogix Operator custom resource information.",
56+
},
57+
[]string{"kind", "name", "namespace", "status"},
58+
)
59+
resourceRejectionsTotalMetric = prometheus.NewCounterVec(
60+
prometheus.CounterOpts{
61+
Name: "cx_operator_resource_rejections_total",
62+
Help: "The total count of rejections by Coralogix Operator validation webhook.",
63+
},
64+
[]string{"kind", "name", "namespace"},
65+
)
6266
)
6367

6468
func SetOperatorInfoMetric(goVersion, operatorVersion, url string) {
@@ -70,15 +74,27 @@ func SetOperatorInfoMetric(goVersion, operatorVersion, url string) {
7074
operatorInfoMetric.WithLabelValues(goVersion, operatorVersion, url).Set(1)
7175
}
7276

73-
var resourceRejectionsTotalMetric = prometheus.NewCounterVec(
74-
prometheus.CounterOpts{
75-
Name: "cx_operator_resource_rejections_total",
76-
Help: "The total count of rejections by Coralogix Operator validation webhook.",
77-
},
78-
[]string{"kind", "name", "namespace"},
79-
)
80-
8177
func IncResourceRejectionsTotalMetric(kind, name, namespace string) {
8278
metricsLog.V(1).Info("Incrementing resource total rejected metric", "kind", kind)
8379
resourceRejectionsTotalMetric.WithLabelValues(kind, name, namespace).Inc()
8480
}
81+
82+
func SetResourceInfoMetric(kind, name, namespace, status string) {
83+
metricsLog.V(1).Info("Setting resource info metric",
84+
"kind", kind,
85+
"name", name,
86+
"namespace", namespace,
87+
"status", status,
88+
)
89+
resourceInfoMetric.WithLabelValues(kind, name, namespace, status).Set(1)
90+
}
91+
92+
func DeleteResourceInfoMetric(kind, name, namespace, status string) {
93+
metricsLog.V(1).Info("Deleting resource info metric",
94+
"kind", kind,
95+
"name", name,
96+
"namespace", namespace,
97+
"status", status,
98+
)
99+
resourceInfoMetric.DeleteLabelValues(kind, name, namespace, status)
100+
}

0 commit comments

Comments
 (0)