Skip to content

Commit 5f113c0

Browse files
committed
Add current time metric and update mechanism (#260)
- Introduced a new Prometheus gauge metric `CurrentTimeSeconds` to track the current time in Unix Epoch format. - Implemented a goroutine that updates the `CurrentTimeSeconds` metric every second, ensuring real-time monitoring of the current time. These changes enhance the observability of the system by providing a metric for the current time, which can be useful for various monitoring and alerting purposes. Signed-off-by: dviejokfs <[email protected]>
1 parent c66a300 commit 5f113c0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

controllers/hlfmetrics/metrics.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package hlfmetrics
22

33
import (
44
"crypto/x509"
5+
"time"
6+
57
"github.com/prometheus/client_golang/prometheus"
68
)
79

@@ -13,6 +15,13 @@ var (
1315
},
1416
[]string{"node_type", "crt_type", "namespace", "name"},
1517
)
18+
19+
CurrentTimeSeconds = prometheus.NewGauge(
20+
prometheus.GaugeOpts{
21+
Name: "hlf_operator_current_time_seconds",
22+
Help: "The current time in Unix Epoch Time.",
23+
},
24+
)
1625
)
1726

1827
func UpdateCertificateExpiry(
@@ -31,3 +40,7 @@ func UpdateCertificateExpiry(
3140
"crt_type": crtType,
3241
}).Set(expiryTime)
3342
}
43+
44+
func UpdateCurrentTime() {
45+
CurrentTimeSeconds.Set(float64(time.Now().Unix()))
46+
}

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ func main() {
126126
}
127127
// Register custom metrics with the global prometheus registry
128128
metrics.Registry.MustRegister(hlfmetrics.CertificateExpiryTimeSeconds)
129+
metrics.Registry.MustRegister(hlfmetrics.CurrentTimeSeconds)
130+
131+
// Start a goroutine to update the current time metric every second
132+
go func() {
133+
for {
134+
hlfmetrics.UpdateCurrentTime()
135+
time.Sleep(time.Second)
136+
}
137+
}()
138+
129139
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
130140
Scheme: scheme,
131141
LeaderElection: enableLeaderElection,

0 commit comments

Comments
 (0)