Skip to content

Commit bc715cd

Browse files
committed
feat(metrics): add machine deployment metrics
Signed-off-by: rajaSahil <[email protected]>
1 parent 51cf731 commit bc715cd

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

cmd/machine-controller/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ func (bs *controllerBootstrap) Start(ctx context.Context) error {
393393
machineCollector := machinecontroller.NewMachineCollector(ctx, bs.mgr.GetClient())
394394
metrics.Registry.MustRegister(machineCollector)
395395

396+
machineDeploymentCollector := machinedeploymentcontroller.NewCollector(ctx, bs.mgr.GetClient())
397+
metrics.Registry.MustRegister(machineDeploymentCollector)
398+
396399
if err := machinecontroller.Add(
397400
ctx,
398401
bs.opt.log,
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2019 The Machine Controller Authors.
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 machinedeployment
18+
19+
import (
20+
"context"
21+
"github.com/prometheus/client_golang/prometheus"
22+
"k8c.io/machine-controller/pkg/apis/cluster/v1alpha1"
23+
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
24+
)
25+
26+
const metricsPrefix = "machine_deployment_"
27+
28+
type Collector struct {
29+
ctx context.Context
30+
client ctrlruntimeclient.Client
31+
32+
replicas *prometheus.Desc
33+
availableReplicas *prometheus.Desc
34+
readyReplicas *prometheus.Desc
35+
updatedReplicas *prometheus.Desc
36+
}
37+
38+
// NewCollector creates new machine deployment collector for metrics collection
39+
func NewCollector(ctx context.Context, client ctrlruntimeclient.Client) *Collector {
40+
return &Collector{
41+
ctx: ctx,
42+
client: client,
43+
replicas: prometheus.NewDesc(
44+
metricsPrefix+"replicas",
45+
"The number of replicas defined for a machine deployment",
46+
[]string{"name", "namespace"}, nil,
47+
),
48+
availableReplicas: prometheus.NewDesc(
49+
metricsPrefix+"available_replicas",
50+
"The number of available replicas for a machine deployment",
51+
[]string{"name", "namespace"}, nil,
52+
),
53+
readyReplicas: prometheus.NewDesc(
54+
metricsPrefix+"ready_replicas",
55+
"The number of ready replicas for a machine deployment",
56+
[]string{"name", "namespace"}, nil,
57+
),
58+
updatedReplicas: prometheus.NewDesc(
59+
metricsPrefix+"updated_replicas",
60+
"The number of replicas updated for a machine deployment",
61+
[]string{"name", "namespace"}, nil,
62+
),
63+
}
64+
}
65+
66+
// Describe implements the prometheus.Describe interface.
67+
func (c *Collector) Describe(desc chan<- *prometheus.Desc) {
68+
desc <- c.replicas
69+
desc <- c.readyReplicas
70+
desc <- c.availableReplicas
71+
desc <- c.readyReplicas
72+
}
73+
74+
// Collect implements the prometheus.Collector interface.
75+
func (c *Collector) Collect(metrics chan<- prometheus.Metric) {
76+
machineDeployments := &v1alpha1.MachineDeploymentList{}
77+
if err := c.client.List(c.ctx, machineDeployments); err != nil {
78+
return
79+
}
80+
81+
for _, machineDeployment := range machineDeployments.Items {
82+
metrics <- prometheus.MustNewConstMetric(
83+
c.replicas,
84+
prometheus.GaugeValue,
85+
float64(machineDeployment.Status.Replicas),
86+
machineDeployment.Name,
87+
machineDeployment.Namespace,
88+
)
89+
metrics <- prometheus.MustNewConstMetric(
90+
c.readyReplicas,
91+
prometheus.GaugeValue,
92+
float64(machineDeployment.Status.ReadyReplicas),
93+
machineDeployment.Name,
94+
machineDeployment.Namespace,
95+
)
96+
metrics <- prometheus.MustNewConstMetric(
97+
c.availableReplicas,
98+
prometheus.GaugeValue,
99+
float64(machineDeployment.Status.AvailableReplicas),
100+
machineDeployment.Name,
101+
machineDeployment.Namespace,
102+
)
103+
metrics <- prometheus.MustNewConstMetric(
104+
c.updatedReplicas,
105+
prometheus.GaugeValue,
106+
float64(machineDeployment.Status.UpdatedReplicas),
107+
machineDeployment.Name,
108+
machineDeployment.Namespace,
109+
)
110+
}
111+
}

0 commit comments

Comments
 (0)