Skip to content

Commit 1a13c9b

Browse files
committed
Move metrics package one level up
1 parent 1c5ea71 commit 1a13c9b

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

cmd/sharder/app/app.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"sigs.k8s.io/controller-runtime/pkg/manager"
3333

3434
"github.com/timebertt/kubernetes-controller-sharding/pkg/controller"
35+
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/metrics"
3536
healthzutils "github.com/timebertt/kubernetes-controller-sharding/pkg/utils/healthz"
3637
"github.com/timebertt/kubernetes-controller-sharding/pkg/webhook"
3738
)
@@ -104,6 +105,11 @@ func run(ctx context.Context, log logr.Logger, opts *options) error {
104105
return fmt.Errorf("failed adding webhooks to manager: %w", err)
105106
}
106107

108+
log.Info("Adding metrics to manager")
109+
if err = shardingmetrics.AddToManager(mgr); err != nil {
110+
return fmt.Errorf("failed adding metrics to manager: %w", err)
111+
}
112+
107113
log.Info("Starting manager")
108114
return mgr.Start(ctx)
109115
}

pkg/controller/sharder/reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ import (
3838

3939
configv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/config/v1alpha1"
4040
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
41+
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/metrics"
4142
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/consistenthash"
4243
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/key"
4344
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases"
44-
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/metrics"
4545
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/ring"
4646
utilclient "github.com/timebertt/kubernetes-controller-sharding/pkg/utils/client"
4747
utilerrors "github.com/timebertt/kubernetes-controller-sharding/pkg/utils/errors"

pkg/metrics/add.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
"fmt"
21+
22+
"github.com/prometheus/client_golang/prometheus"
23+
"sigs.k8s.io/controller-runtime/pkg/manager"
24+
"sigs.k8s.io/controller-runtime/pkg/metrics"
25+
)
26+
27+
const Namespace = "controller_sharding"
28+
29+
// AddToManager adds all metrics exporters for sharding objects to the manager.
30+
func AddToManager(mgr manager.Manager) error {
31+
for _, collector := range []prometheus.Collector{
32+
AssignmentsTotal,
33+
MovementsTotal,
34+
DrainsTotal,
35+
RingCalculationsTotal,
36+
} {
37+
if err := metrics.Registry.Register(collector); err != nil {
38+
return fmt.Errorf("failed to add sharding operations metrics: %w", err)
39+
}
40+
}
41+
42+
return nil
43+
}

pkg/sharding/metrics/metrics.go renamed to pkg/metrics/operations.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ package metrics
1818

1919
import (
2020
"github.com/prometheus/client_golang/prometheus"
21-
22-
"sigs.k8s.io/controller-runtime/pkg/metrics"
2321
)
2422

25-
const Namespace = "controller_sharding"
26-
2723
var (
2824
// AssignmentsTotal is a prometheus counter metric which holds the total number of shard assignments by the sharder
2925
// webhook per ControllerRing and GroupResource.
@@ -61,12 +57,3 @@ var (
6157
Help: "Total number of hash ring calculations per ControllerRing",
6258
}, []string{"controllerring"})
6359
)
64-
65-
func init() {
66-
metrics.Registry.MustRegister(
67-
AssignmentsTotal,
68-
MovementsTotal,
69-
DrainsTotal,
70-
RingCalculationsTotal,
71-
)
72-
}

pkg/sharding/ring/ring.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
coordinationv1 "k8s.io/api/coordination/v1"
2323

2424
shardingv1alpha1 "github.com/timebertt/kubernetes-controller-sharding/pkg/apis/sharding/v1alpha1"
25+
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/metrics"
2526
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/consistenthash"
2627
"github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases"
27-
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/metrics"
2828
)
2929

3030
// FromLeases creates a ring from the given membership information (shard leases). It transforms shard leases into a

pkg/webhook/sharder/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package sharder
1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2121

22-
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/metrics"
22+
shardingmetrics "github.com/timebertt/kubernetes-controller-sharding/pkg/metrics"
2323
)
2424

2525
type Metrics interface {

0 commit comments

Comments
 (0)