Skip to content

Commit 2fb8412

Browse files
committed
Add shard state exporter
1 parent 71f7c73 commit 2fb8412

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-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 := ShardExporter.AddToManager(mgr); err != nil {
32+
return fmt.Errorf("failed to add shard exporter: %w", err)
33+
}
34+
3135
for _, collector := range []prometheus.Collector{
3236
AssignmentsTotal,
3337
MovementsTotal,

pkg/metrics/shard.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
coordinationv1 "k8s.io/api/coordination/v1"
22+
"k8s.io/utils/ptr"
23+
24+
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
25+
"github.com/timebertt/kubernetes-controller-sharding/pkg/metrics/exporter"
26+
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases"
27+
)
28+
29+
var ShardExporter = exporter.Exporter[*coordinationv1.Lease, *coordinationv1.LeaseList]{
30+
Namespace: Namespace,
31+
Subsystem: "shard",
32+
33+
StaticLabelKeys: []string{"namespace", "shard", "controllerring"},
34+
GenerateStaticLabelValues: func(lease *coordinationv1.Lease) []string {
35+
return []string{lease.Namespace, lease.Name, lease.Labels[shardingv1alpha1.LabelControllerRing]}
36+
},
37+
38+
Metrics: []exporter.Metric[*coordinationv1.Lease]{
39+
{
40+
Name: "info",
41+
Help: "Information about a shard",
42+
LabelKeys: []string{"uid"},
43+
44+
Generate: func(desc *prometheus.Desc, lease *coordinationv1.Lease, staticLabelValues []string, ch chan<- prometheus.Metric) {
45+
ch <- prometheus.MustNewConstMetric(
46+
desc,
47+
prometheus.GaugeValue,
48+
1,
49+
append(staticLabelValues, string(lease.UID))...,
50+
)
51+
},
52+
},
53+
{
54+
Name: "state",
55+
Help: "The shard's current state observed by the shardlease controller",
56+
LabelKeys: []string{"state"},
57+
58+
Generate: exporter.GenerateStateSet[*coordinationv1.Lease](
59+
exporter.KnownStatesStringer(leases.KnownShardStates), ptr.To(leases.Unknown.String()),
60+
func(lease *coordinationv1.Lease) string {
61+
return lease.Labels[shardingv1alpha1.LabelState]
62+
},
63+
),
64+
},
65+
},
66+
}

pkg/sharding/leases/state.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const (
4242
Ready
4343
)
4444

45+
// KnownShardStates is a list of all known shard states.
46+
var KnownShardStates = []ShardState{Orphaned, Dead, Uncertain, Expired, Ready}
47+
4548
// String returns a string representation of this ShardState.
4649
func (s ShardState) String() string {
4750
switch s {

0 commit comments

Comments
 (0)