Skip to content

Commit 4ff1ff9

Browse files
authored
feat: expose metrics for the git commit (#1249)
new `HostOperatorCommitGaugeVec` and `HostOperatorShortCommitGaugeVec` metrics to reflect the current git commit and short commit of the host-operator the metrics are named `sandbox_host_operator_commit` and `sandbox_host_operator_short_commit` and the relevant label is `commit` also, update the tests to assert the value of the metric. also, deprecated the `HostOperatorVersionGaugeVec` metric (will be removed in a future release, once Grafana is updated to support the new metrics) Signed-off-by: Xavier Coulon <xcoulon@redhat.com> * update govulncheck exclusions Signed-off-by: Xavier Coulon <xcoulon@redhat.com> * apply coderabbitai suggestion Signed-off-by: Xavier Coulon <xcoulon@redhat.com> * test Signed-off-by: Xavier Coulon <xcoulon@redhat.com> * update govulncheck exclusions Signed-off-by: Xavier Coulon <xcoulon@redhat.com> --------- Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
1 parent 452f707 commit 4ff1ff9

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,4 @@ If you are still confused by the e2e location, execution and branch pairing, see
9999
*** 2. create a branch with the same name within your fork of https://github.com/codeready-toolchain/toolchain-e2e[toolchain-e2e] repo and put all necessary changes there
100100
*** 3. push all changes into both forks of the repositories https://github.com/codeready-toolchain/toolchain-e2e[toolchain-e2e] and https://github.com/codeready-toolchain/host-operator[host-operator]
101101
*** 4. create a PR for https://github.com/codeready-toolchain/host-operator[host-operator]
102-
*** 5. create a PR for https://github.com/codeready-toolchain/toolchain-e2e[toolchain-e2e]
102+
*** 5. create a PR for https://github.com/codeready-toolchain/toolchain-e2e[toolchain-e2e]

pkg/metrics/metrics.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ var (
5555
UserSignupsPerActivationAndDomainGaugeVec *prometheus.GaugeVec
5656
// MasterUserRecordGaugeVec reflects the current number of MasterUserRecords, labelled with their email address domain (`internal` vs `external`)
5757
MasterUserRecordGaugeVec *prometheus.GaugeVec
58-
// HostOperatorVersionGaugeVec reflects the current version of the host-operator (via the `version` label)
59-
HostOperatorVersionGaugeVec *prometheus.GaugeVec
58+
// HostOperatorVersionGaugeVec reflects the current short commit used to identify the version of the host-operator (via the `commit` label)
59+
HostOperatorVersionGaugeVec *prometheus.GaugeVec // DEPRECATED: use HostOperatorShortCommitGaugeVec instead
60+
// HostOperatorShortCommitGaugeVec reflects the current short git commit of the host-operator (via the `commit` label)
61+
HostOperatorShortCommitGaugeVec *prometheus.GaugeVec
62+
// HostOperatorCommitGaugeVec reflects the current full git commit of the host-operator (via the `commit` label)
63+
HostOperatorCommitGaugeVec *prometheus.GaugeVec
6064
// ToolchainStatusGaugeVec reflects the current status of the toolchainstatus, labelled with the status
6165
ToolchainStatusGaugeVec *prometheus.GaugeVec
6266
)
@@ -100,7 +104,9 @@ func initMetrics() {
100104
SpaceGaugeVec = newGaugeVec("spaces_current", "Current number of Spaces (per member cluster)", "cluster_name")
101105
UserSignupsPerActivationAndDomainGaugeVec = newGaugeVec("users_per_activations_and_domain", "Number of UserSignups per activations and domain", []string{"activations", "domain"}...)
102106
MasterUserRecordGaugeVec = newGaugeVec("master_user_records", "Number of MasterUserRecords per email address domain ('internal' vs 'external')", "domain")
103-
HostOperatorVersionGaugeVec = newGaugeVec("host_operator_version", "Current version of the host operator", "commit")
107+
HostOperatorVersionGaugeVec = newGaugeVec("host_operator_version", "Short commit used to identify the version of the host operator", "commit")
108+
HostOperatorCommitGaugeVec = newGaugeVec("host_operator_commit", "Full commit used to identify the version of the host operator", "commit")
109+
HostOperatorShortCommitGaugeVec = newGaugeVec("host_operator_short_commit", "Short commit used to identify the version of the host operator", "commit")
104110
ToolchainStatusGaugeVec = newGaugeVec("toolchain_status", "Current status of the toolchain components", "component")
105111
// Histograms
106112
UserSignupProvisionTimeHistogram = newHistogram("user_signup_provision_time", "UserSignup provision time in seconds")
@@ -199,8 +205,9 @@ func RegisterCustomMetrics() []prometheus.Collector {
199205
collectors = append(collectors, v)
200206
}
201207

202-
// expose the HostOperatorVersionGaugeVec metric (static ie, 1 value per build/deployment)
203-
HostOperatorVersionGaugeVec.WithLabelValues(version.Commit[0:7]).Set(1)
208+
HostOperatorVersionGaugeVec.WithLabelValues(version.Commit[0:7]).Set(1) // the HostOperatorVersionGaugeVec is set to `1`, Grafana will display the 'commit' label as the version for the instant record
209+
HostOperatorCommitGaugeVec.WithLabelValues(version.Commit).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit
210+
HostOperatorShortCommitGaugeVec.WithLabelValues(version.Commit[0:7]).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit
204211

205212
logger.Info("custom metrics registered")
206213
return collectors

pkg/metrics/metrics_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77
"sync"
88
"testing"
9+
"time"
910

1011
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
1112
"github.com/codeready-toolchain/host-operator/pkg/metrics"
@@ -16,6 +17,8 @@ import (
1617
"github.com/codeready-toolchain/toolchain-common/pkg/test/space"
1718
"github.com/codeready-toolchain/toolchain-common/pkg/test/usersignup"
1819

20+
promtestutil "github.com/prometheus/client_golang/prometheus/testutil"
21+
"github.com/stretchr/testify/assert"
1922
"github.com/stretchr/testify/require"
2023
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
2124
logf "sigs.k8s.io/controller-runtime/pkg/log"
@@ -40,6 +43,32 @@ func TestResetMetrics(t *testing.T) {
4043
metricscommontest.AssertAllHistogramBucketsAreEmpty(t, metrics.UserSignupProvisionTimeHistogram)
4144
}
4245

46+
func TestGitCommitGauge(t *testing.T) {
47+
t.Run("HostOperatorCommitGaugeVec", func(t *testing.T) {
48+
// given
49+
metrics.Reset()
50+
defer metrics.Reset()
51+
now := time.Now()
52+
53+
// when
54+
metrics.HostOperatorCommitGaugeVec.WithLabelValues("commit-1234567890").SetToCurrentTime()
55+
56+
// then
57+
assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorCommitGaugeVec.WithLabelValues("commit-1234567890")), float64(time.Minute.Seconds()))
58+
})
59+
t.Run("HostOperatorShortCommitGaugeVec", func(t *testing.T) {
60+
// given
61+
metrics.Reset()
62+
defer metrics.Reset()
63+
now := time.Now()
64+
65+
// when
66+
metrics.HostOperatorShortCommitGaugeVec.WithLabelValues("hash-1234567890").SetToCurrentTime()
67+
68+
// then
69+
assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorShortCommitGaugeVec.WithLabelValues("hash-1234567890")), float64(time.Minute.Seconds()))
70+
})
71+
}
4372
func TestIncrementMasterUserRecordCount(t *testing.T) {
4473
// given
4574
metrics.Reset()

0 commit comments

Comments
 (0)