Skip to content

Commit 8dda917

Browse files
authored
feat: expose metrics for the git commit (#588)
similar to codeready-toolchain/host-operator#1249 Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
1 parent daaec3b commit 8dda917

8 files changed

Lines changed: 159 additions & 14 deletions

File tree

.govulncheck.yaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
ignored-vulnerabilities: []
1+
ignored-vulnerabilities:
2+
# Incorrect parsing of IPv6 host literals in net/url
3+
# Found in: net/url@go1.24.13
4+
# Fixed in: net/url@go1.25.8
5+
- id: GO-2026-4601
6+
info: https://pkg.go.dev/vuln/GO-2026-4601
7+
silence-until: 2026-04-23
8+
# FileInfo can escape from a Root in os
9+
# Found in: os@go1.24.13
10+
# Fixed in: os@go1.25.8
11+
- id: GO-2026-4602
12+
info: https://pkg.go.dev/vuln/GO-2026-4602
13+
silence-until: 2026-04-23
14+
# URLs in meta content attribute actions are not escaped in html/template
15+
# Found in: html/template@go1.24.13
16+
# Fixed in: html/template@go1.25.8
17+
- id: GO-2026-4603
18+
info: https://pkg.go.dev/vuln/GO-2026-4603
19+
silence-until: 2026-04-23

cmd/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"fmt"
77
"net/http"
88
"os"
9-
"sigs.k8s.io/controller-runtime/pkg/cache"
109
"time"
1110

11+
"sigs.k8s.io/controller-runtime/pkg/cache"
12+
1213
toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
1314
"github.com/codeready-toolchain/registration-service/pkg/auth"
1415
"github.com/codeready-toolchain/registration-service/pkg/configuration"
@@ -119,6 +120,7 @@ func main() {
119120
// Registration Service
120121
// ---------------------------------------------
121122
regsvcRegistry := prometheus.NewRegistry()
123+
configuration.RegisterVersionMetrics(regsvcRegistry)
122124
regsvcMetricsSrv, _ := server.StartMetricsServer(regsvcRegistry, server.RegSvcMetricsPort)
123125
regsvcSrv := server.New(app)
124126
err = regsvcSrv.SetupRoutes(proxy.DefaultPort, regsvcRegistry, nsClient)

pkg/configuration/configuration.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ import (
1818
logf "sigs.k8s.io/controller-runtime/pkg/log"
1919
)
2020

21-
var (
22-
// Commit current build commit set by build script.
23-
Commit = "0"
24-
// BuildTime set by build script in ISO 8601 (UTC) format:
25-
// YYYY-MM-DDThh:mm:ssTZD (see https://www.w3.org/TR/NOTE-datetime for
26-
// details).
27-
BuildTime = "0"
28-
// StartTime in ISO 8601 (UTC) format.
29-
StartTime = time.Now().UTC().Format("2006-01-02T15:04:05Z")
30-
)
31-
3221
var logger = logf.Log.WithName("configuration")
3322

3423
const (

pkg/configuration/version.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package configuration
2+
3+
import (
4+
"time"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
)
8+
9+
var (
10+
// Commit current build commit set by build script.
11+
Commit = "0"
12+
// BuildTime set by build script in ISO 8601 (UTC) format:
13+
// YYYY-MM-DDThh:mm:ssTZD (see https://www.w3.org/TR/NOTE-datetime for
14+
// details).
15+
BuildTime = "0"
16+
// StartTime in ISO 8601 (UTC) format.
17+
StartTime = time.Now().UTC().Format("2006-01-02T15:04:05Z")
18+
)
19+
20+
var (
21+
// RegistrationServiceShortCommitGaugeVec reflects the current short git commit of the registration service (via the `commit` label)
22+
RegistrationServiceShortCommitGaugeVec *prometheus.GaugeVec
23+
// RegistrationServiceCommitGaugeVec reflects the current full git commit of the registration service (via the `commit` label)
24+
RegistrationServiceCommitGaugeVec *prometheus.GaugeVec
25+
)
26+
27+
func RegisterVersionMetrics(registry *prometheus.Registry) {
28+
// RegistrationServiceCommitGaugeVec reflects the current full git commit of the registration service (via the `commit` label)
29+
RegistrationServiceCommitGaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
30+
Name: "sandbox_registration_service_commit",
31+
Help: "The commit of the registration service",
32+
}, []string{"commit"})
33+
RegistrationServiceCommitGaugeVec.WithLabelValues(Commit).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit
34+
35+
// RegistrationServiceShortCommitGaugeVec reflects the current short git commit of the registration service (via the `commit` label)
36+
RegistrationServiceShortCommitGaugeVec = prometheus.NewGaugeVec(prometheus.GaugeOpts{
37+
Name: "sandbox_registration_service_short_commit",
38+
Help: "The short commit of the registration service",
39+
}, []string{"commit"})
40+
shortCommit := Commit
41+
if len(Commit) >= 7 {
42+
shortCommit = Commit[0:7]
43+
}
44+
RegistrationServiceShortCommitGaugeVec.WithLabelValues(shortCommit).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit
45+
registry.MustRegister(RegistrationServiceCommitGaugeVec, RegistrationServiceShortCommitGaugeVec)
46+
}

pkg/configuration/version_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package configuration_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/codeready-toolchain/registration-service/pkg/configuration"
8+
9+
"github.com/prometheus/client_golang/prometheus"
10+
promtestutil "github.com/prometheus/client_golang/prometheus/testutil"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestVersionMetrics(t *testing.T) {
15+
16+
testData := []struct {
17+
name string
18+
commit string
19+
shortCommit string
20+
}{
21+
{name: "when commit is longer than 7 characters", commit: "short12-34567890", shortCommit: "short12"},
22+
{name: "when commit is shorter than 7 characters", commit: "short", shortCommit: "short"},
23+
{name: "when commit is empty", commit: "", shortCommit: ""},
24+
}
25+
26+
for _, test := range testData {
27+
t.Run(test.name, func(t *testing.T) {
28+
// given
29+
configuration.Commit = test.commit
30+
registry := prometheus.NewRegistry()
31+
32+
// when
33+
configuration.RegisterVersionMetrics(registry)
34+
35+
// then
36+
assert.InDelta(t, float64(time.Now().Unix()), promtestutil.ToFloat64(configuration.RegistrationServiceShortCommitGaugeVec.WithLabelValues(test.shortCommit)), float64(time.Minute.Seconds()))
37+
assert.InDelta(t, float64(time.Now().Unix()), promtestutil.ToFloat64(configuration.RegistrationServiceCommitGaugeVec.WithLabelValues(test.commit)), float64(time.Minute.Seconds()))
38+
})
39+
}
40+
41+
}

pkg/server/metrics_server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func StartMetricsServer(reg *prometheus.Registry, port int) (*http.Server, *gin.
2424
DisableCompression: true,
2525
}),
2626
)))
27+
2728
log.Info("Starting the registration-service metrics server...")
2829
srv := &http.Server{
2930
Addr: fmt.Sprintf(":%d", port),

pkg/server/metrics_server_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package server_test
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"testing"
8+
"time"
9+
10+
"github.com/codeready-toolchain/registration-service/pkg/configuration"
11+
"github.com/codeready-toolchain/registration-service/pkg/server"
12+
"github.com/prometheus/client_golang/prometheus"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestStartMetricsServer(t *testing.T) {
17+
// given
18+
19+
configuration.Commit = "1234567890"
20+
registry := prometheus.NewRegistry()
21+
configuration.RegisterVersionMetrics(registry)
22+
srv, _ := server.StartMetricsServer(registry, 8080)
23+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
24+
defer cancel()
25+
defer func(ctx context.Context) {
26+
err := srv.Shutdown(ctx)
27+
if err != nil {
28+
t.Fatalf("failed to shutdown metrics server: %v", err)
29+
}
30+
}(ctx)
31+
// make a request to the metrics server
32+
33+
resp, err := http.Get("http://localhost:8080/metrics")
34+
if err != nil {
35+
t.Fatalf("failed to make request to metrics server: %v", err)
36+
}
37+
defer resp.Body.Close()
38+
39+
// check the response
40+
body, err := io.ReadAll(resp.Body)
41+
if err != nil {
42+
t.Fatalf("failed to read response body: %v", err)
43+
}
44+
45+
// check the response body
46+
assert.Contains(t, string(body), "sandbox_registration_service_commit")
47+
assert.Contains(t, string(body), "sandbox_registration_service_short_commit")
48+
}

pkg/verification/sender/twilio_sender_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *MockTwilioConfig) TwilioSenderConfigs() []toolchainv1alpha1.TwilioSende
4040

4141
func TestTwilioSenderID(t *testing.T) {
4242

43-
cfg := &MockTwilioConfig{
43+
cfg := &MockTwilioConfig{ //nolint:gosec
4444
AccountSID: "TWILIO_SID_VALUE",
4545
AuthToken: "AUTH_TOKEN_VALUE",
4646
FromNumber: "+13334445555",

0 commit comments

Comments
 (0)