-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
75 lines (64 loc) · 1.92 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"log/slog"
"time"
v2 "github.com/metal-stack/firewall-controller-manager/api/v2"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
var (
firewallDeploymentReadyReplicasDesc = prometheus.NewDesc(
"firewall_deployment_ready_replicas",
"provide information on firewall deployment ready replicas",
[]string{"name", "namespace"},
nil,
)
firewallDeploymentTargetReplicasDesc = prometheus.NewDesc(
"firewall_deployment_target_replicas",
"provide information on firewall deployment target replicas",
[]string{"name", "namespace"},
nil,
)
)
type collector struct {
log *slog.Logger
seedClient client.Client
namespace string
}
func mustRegisterCustomMetrics(log *slog.Logger, seedClient client.Client, namespace string) {
c := &collector{
log: log,
seedClient: seedClient,
namespace: namespace,
}
metrics.Registry.MustRegister(c)
}
func (c *collector) Describe(ch chan<- *prometheus.Desc) {
ch <- firewallDeploymentReadyReplicasDesc
ch <- firewallDeploymentTargetReplicasDesc
}
func (c *collector) Collect(ch chan<- prometheus.Metric) {
c.log.Info("collecting custom metrics")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
deploys := &v2.FirewallDeploymentList{}
err := c.seedClient.List(ctx, deploys, client.InNamespace(c.namespace))
if err != nil {
c.log.Error("unable to list firewall deployments", "error", err)
return
}
for _, deploy := range deploys.Items {
ch <- prometheus.MustNewConstMetric(firewallDeploymentReadyReplicasDesc, prometheus.GaugeValue,
float64(deploy.Status.ReadyReplicas),
deploy.Name,
deploy.Namespace,
)
ch <- prometheus.MustNewConstMetric(firewallDeploymentTargetReplicasDesc, prometheus.GaugeValue,
float64(deploy.Status.TargetReplicas),
deploy.Name,
deploy.Namespace,
)
}
}