Skip to content

Commit aa272df

Browse files
committed
Add shard state exporter
1 parent c650a60 commit aa272df

File tree

3 files changed

+76
-0
lines changed

3 files changed

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

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)