Skip to content

Commit 73f57a3

Browse files
committed
Add ControllerRing state exporter
1 parent aa272df commit 73f57a3

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

pkg/metrics/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const Namespace = "controller_sharding"
2828

2929
// AddToManager adds all metrics exporters for sharding objects to the manager.
3030
func AddToManager(mgr manager.Manager) error {
31+
if err := ControllerRingExporter.AddToManager(mgr); err != nil {
32+
return fmt.Errorf("failed to add ControllerRing exporter: %w", err)
33+
}
34+
3135
if err := ShardExporter.AddToManager(mgr); err != nil {
3236
return fmt.Errorf("failed to add shard exporter: %w", err)
3337
}

pkg/metrics/controllerring.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2025 Tim Ebert.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package metrics
18+
19+
import (
20+
"github.com/prometheus/client_golang/prometheus"
21+
22+
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
23+
"github.com/timebertt/kubernetes-controller-sharding/pkg/metrics/exporter"
24+
)
25+
26+
var ControllerRingExporter = exporter.Exporter[*shardingv1alpha1.ControllerRing, *shardingv1alpha1.ControllerRingList]{
27+
Namespace: Namespace,
28+
Subsystem: "controllerring",
29+
30+
StaticLabelKeys: []string{"controllerring", "uid"},
31+
GenerateStaticLabelValues: func(controllerRing *shardingv1alpha1.ControllerRing) []string {
32+
return []string{controllerRing.Name, string(controllerRing.UID)}
33+
},
34+
35+
Metrics: []exporter.Metric[*shardingv1alpha1.ControllerRing]{
36+
{
37+
Name: "metadata_generation",
38+
Help: "The generation of a ControllerRing",
39+
40+
Generate: func(desc *prometheus.Desc, controllerRing *shardingv1alpha1.ControllerRing, staticLabelValues []string, ch chan<- prometheus.Metric) {
41+
ch <- prometheus.MustNewConstMetric(
42+
desc,
43+
prometheus.GaugeValue,
44+
float64(controllerRing.Generation),
45+
staticLabelValues...,
46+
)
47+
},
48+
},
49+
{
50+
Name: "observed_generation",
51+
Help: "The latest generation observed by the ControllerRing controller",
52+
53+
Generate: func(desc *prometheus.Desc, controllerRing *shardingv1alpha1.ControllerRing, staticLabelValues []string, ch chan<- prometheus.Metric) {
54+
ch <- prometheus.MustNewConstMetric(
55+
desc,
56+
prometheus.GaugeValue,
57+
float64(controllerRing.Status.ObservedGeneration),
58+
staticLabelValues...,
59+
)
60+
},
61+
},
62+
{
63+
Name: "status_shards",
64+
Help: "The ControllerRing's total number of shards observed by the ControllerRing controller",
65+
66+
Generate: func(desc *prometheus.Desc, controllerRing *shardingv1alpha1.ControllerRing, staticLabelValues []string, ch chan<- prometheus.Metric) {
67+
ch <- prometheus.MustNewConstMetric(
68+
desc,
69+
prometheus.GaugeValue,
70+
float64(controllerRing.Status.Shards),
71+
staticLabelValues...,
72+
)
73+
},
74+
},
75+
{
76+
Name: "status_available_shards",
77+
Help: "The ControllerRing's number of available shards observed by the ControllerRing controller",
78+
79+
Generate: func(desc *prometheus.Desc, controllerRing *shardingv1alpha1.ControllerRing, staticLabelValues []string, ch chan<- prometheus.Metric) {
80+
ch <- prometheus.MustNewConstMetric(
81+
desc,
82+
prometheus.GaugeValue,
83+
float64(controllerRing.Status.AvailableShards),
84+
staticLabelValues...,
85+
)
86+
},
87+
},
88+
},
89+
}

0 commit comments

Comments
 (0)