33 *
44 * This source file is part of the FoundationDB open source project
55 *
6- * Copyright 2021 Apple Inc. and the FoundationDB project authors
6+ * Copyright 2021-2025 Apple Inc. and the FoundationDB project authors
77 *
88 * Licensed under the Apache License, Version 2.0 (the "License");
99 * you may not use this file except in compliance with the License.
@@ -24,84 +24,11 @@ import (
2424 "context"
2525
2626 fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/v2/api/v1beta2"
27+ internalMetrics "github.com/FoundationDB/fdb-kubernetes-operator/v2/internal/metrics"
2728 "github.com/prometheus/client_golang/prometheus"
2829 "sigs.k8s.io/controller-runtime/pkg/metrics"
2930)
3031
31- var (
32- descClusterDefaultLabels = []string {"namespace" , "name" }
33-
34- descClusterCreated = prometheus .NewDesc (
35- "fdb_operator_cluster_created_time" ,
36- "Creation time in unix timestamp for Fdb Cluster." ,
37- descClusterDefaultLabels ,
38- nil ,
39- )
40-
41- descClusterStatus = prometheus .NewDesc (
42- "fdb_operator_cluster_status" ,
43- "status of the Fdb Cluster." ,
44- append (descClusterDefaultLabels , "status_type" ),
45- nil ,
46- )
47-
48- descClusterLastReconciled = prometheus .NewDesc (
49- "fdb_operator_cluster_latest_reconciled_status" ,
50- "the latest generation that was reconciled." ,
51- descClusterDefaultLabels ,
52- nil ,
53- )
54-
55- descProcessGroupsToRemove = prometheus .NewDesc (
56- "fdb_operator_process_groups_to_remove_total" ,
57- "the count of process groups that should be removed from the cluster." ,
58- descClusterDefaultLabels ,
59- nil ,
60- )
61-
62- descProcessGroupsToRemoveWithoutExclusion = prometheus .NewDesc (
63- "fdb_operator_process_group_to_remove_without_exclusion_total" ,
64- "the count of process groups that should be removed from the cluster without excluding." ,
65- descClusterDefaultLabels ,
66- nil ,
67- )
68-
69- descClusterReconciled = prometheus .NewDesc (
70- "fdb_operator_cluster_reconciled_status" ,
71- "status if the Fdb Cluster is reconciled." ,
72- descClusterDefaultLabels ,
73- nil ,
74- )
75-
76- descProcessGroupStatus = prometheus .NewDesc (
77- "fdb_operator_process_group_total" ,
78- "the count of Fdb process groups in a specific condition." ,
79- append (descClusterDefaultLabels , "process_class" , "condition" ),
80- nil ,
81- )
82-
83- descProcessGroupMarkedRemoval = prometheus .NewDesc (
84- "fdb_operator_process_group_marked_removal" ,
85- "the count of Fdb process groups that are marked for removal." ,
86- append (descClusterDefaultLabels , "process_class" ),
87- nil ,
88- )
89-
90- descProcessGroupMarkedExcluded = prometheus .NewDesc (
91- "fdb_operator_process_group_marked_excluded" ,
92- "the count of Fdb process groups that are marked as excluded." ,
93- append (descClusterDefaultLabels , "process_class" ),
94- nil ,
95- )
96-
97- desDesiredProcessGroups = prometheus .NewDesc (
98- "fdb_operator_desired_process_group_total" ,
99- "the count of the desired Fdb process groups" ,
100- append (descClusterDefaultLabels , "process_class" ),
101- nil ,
102- )
103- )
104-
10532type fdbClusterCollector struct {
10633 reconciler * FoundationDBClusterReconciler
10734}
@@ -112,8 +39,8 @@ func newFDBClusterCollector(reconciler *FoundationDBClusterReconciler) *fdbClust
11239
11340// Describe implements the prometheus.Collector interface
11441func (c * fdbClusterCollector ) Describe (ch chan <- * prometheus.Desc ) {
115- ch <- descClusterCreated
116- ch <- descClusterStatus
42+ ch <- internalMetrics . DescClusterCreated
43+ ch <- internalMetrics . DescClusterStatus
11744}
11845
11946// Collect implements the prometheus.Collector interface
@@ -124,118 +51,14 @@ func (c *fdbClusterCollector) Collect(ch chan<- prometheus.Metric) {
12451 return
12552 }
12653 for _ , cluster := range clusters .Items {
127- collectMetrics (ch , & cluster )
128- }
129- }
130-
131- func collectMetrics (ch chan <- prometheus.Metric , cluster * fdbv1beta2.FoundationDBCluster ) {
132- addConstMetric := func (desc * prometheus.Desc , t prometheus.ValueType , v float64 , lv ... string ) {
133- lv = append ([]string {cluster .Namespace , cluster .Name }, lv ... )
134- ch <- prometheus .MustNewConstMetric (desc , t , v , lv ... )
135- }
136- addGauge := func (desc * prometheus.Desc , v float64 , lv ... string ) {
137- addConstMetric (desc , prometheus .GaugeValue , v , lv ... )
138- }
139- // These are the correct metrics with the prefix "fdb_operator"
140- addGauge (descClusterCreated , float64 (cluster .CreationTimestamp .Unix ()))
141- addGauge (descClusterStatus , boolFloat64 (cluster .Status .Health .Healthy ), "health" )
142- addGauge (descClusterStatus , boolFloat64 (cluster .Status .Health .Available ), "available" )
143- addGauge (descClusterStatus , boolFloat64 (cluster .Status .Health .FullReplication ), "replication" )
144- addGauge (
145- descClusterStatus ,
146- float64 (cluster .Status .Health .DataMovementPriority ),
147- "datamovementpriority" ,
148- )
149- addGauge (descClusterLastReconciled , float64 (cluster .Status .Generations .Reconciled ))
150- addGauge (
151- descClusterReconciled ,
152- boolFloat64 (cluster .ObjectMeta .Generation == cluster .Status .Generations .Reconciled ),
153- )
154- addGauge (descProcessGroupsToRemove , float64 (len (cluster .Spec .ProcessGroupsToRemove )))
155- addGauge (
156- descProcessGroupsToRemoveWithoutExclusion ,
157- float64 (len (cluster .Spec .ProcessGroupsToRemoveWithoutExclusion )),
158- )
159-
160- // Calculate the process group metrics
161- conditionMap , removals , exclusions := getProcessGroupMetrics (cluster )
162-
163- for pclass , conditionMap := range conditionMap {
164- for condition , count := range conditionMap {
165- addGauge (descProcessGroupStatus , float64 (count ), string (pclass ), string (condition ))
166- }
167-
168- addGauge (descProcessGroupMarkedRemoval , float64 (removals [pclass ]), string (pclass ))
169- addGauge (descProcessGroupMarkedExcluded , float64 (exclusions [pclass ]), string (pclass ))
170- }
171-
172- counts , err := cluster .GetProcessCountsWithDefaults ()
173- if err != nil {
174- return
175- }
176-
177- for processCount , count := range counts .Map () {
178- addGauge (desDesiredProcessGroups , float64 (count ), string (processCount ))
54+ internalMetrics .CollectMetrics (ch , & cluster )
17955 }
18056}
18157
182- func getProcessGroupMetrics (
183- cluster * fdbv1beta2.FoundationDBCluster ,
184- ) (map [fdbv1beta2.ProcessClass ]map [fdbv1beta2.ProcessGroupConditionType ]int , map [fdbv1beta2.ProcessClass ]int , map [fdbv1beta2.ProcessClass ]int ) {
185- metricMap := map [fdbv1beta2.ProcessClass ]map [fdbv1beta2.ProcessGroupConditionType ]int {}
186- removals := map [fdbv1beta2.ProcessClass ]int {}
187- exclusions := map [fdbv1beta2.ProcessClass ]int {}
188-
189- for _ , processGroup := range cluster .Status .ProcessGroups {
190- if _ , exits := metricMap [processGroup .ProcessClass ]; ! exits {
191- metricMap [processGroup .ProcessClass ] = map [fdbv1beta2.ProcessGroupConditionType ]int {}
192- removals [processGroup .ProcessClass ] = 0
193- exclusions [processGroup .ProcessClass ] = 0
194- }
195-
196- if processGroup .IsMarkedForRemoval () {
197- removals [processGroup .ProcessClass ]++
198- }
199-
200- if processGroup .IsExcluded () {
201- exclusions [processGroup .ProcessClass ]++
202- }
203-
204- if len (processGroup .ProcessGroupConditions ) == 0 {
205- metricMap [processGroup.ProcessClass ][fdbv1beta2.ReadyCondition ]++
206- }
207-
208- for _ , condition := range processGroup .ProcessGroupConditions {
209- metricMap [processGroup.ProcessClass ][condition.ProcessGroupConditionType ]++
210- }
211- }
212-
213- // Ensure that all conditions are present
214- for pClass := range metricMap {
215- if _ , exits := metricMap [pClass ]; ! exits {
216- metricMap [pClass ] = map [fdbv1beta2.ProcessGroupConditionType ]int {}
217- }
218-
219- for _ , condition := range fdbv1beta2 .AllProcessGroupConditionTypes () {
220- if _ , exists := metricMap [pClass ][condition ]; ! exists {
221- metricMap [pClass ][condition ] = 0
222- }
223- }
224- }
225-
226- return metricMap , removals , exclusions
227- }
228-
22958// InitCustomMetrics initializes the metrics collectors for the operator.
23059func InitCustomMetrics (reconciler * FoundationDBClusterReconciler ) {
23160 metrics .Registry .MustRegister (
23261 newFDBClusterCollector (reconciler ),
62+ internalMetrics .CoordinatorChangesCounter ,
23363 )
23464}
235-
236- func boolFloat64 (b bool ) float64 {
237- if b {
238- return 1
239- }
240- return 0
241- }
0 commit comments