diff --git a/controllers/spacerequest/spacerequest_controller_test.go b/controllers/spacerequest/spacerequest_controller_test.go index 40d10dcc8..adcdf77be 100644 --- a/controllers/spacerequest/spacerequest_controller_test.go +++ b/controllers/spacerequest/spacerequest_controller_test.go @@ -223,7 +223,7 @@ func TestCreateSpaceRequest(t *testing.T) { HasSpecTargetClusterRoles(srClusterRoles). HasConditions(spacetest.Ready()). // condition is reflected from space status HasStatusTargetClusterURL(member1.APIEndpoint). // has new target cluster url - HasNamespaceAccess([]toolchainv1alpha1.NamespaceAccess{{Name: "jane-env1", SecretRef: "existingDevSecret"}, {Name: "jane-env2", SecretRef: "existingDevSecret2"}}). // expected secrets are there. The secret names may not match exactly since it is generated the first time it's created + HasNamespaceAccess([]toolchainv1alpha1.NamespaceAccess{{Name: "jane-env1", SecretRef: "existingDevSecret"}, {Name: "jane-env2", SecretRef: "existingDevSecret2"}}). // nolint:gosec // expected secrets are there. The secret names may not match exactly since it is generated the first time it's created HasFinalizer() // a subspace is created with the tierName and cluster roles from the spacerequest spacetest.AssertThatSpace(t, commontest.HostOperatorNs, spaceutil.SubSpaceName(parentSpace, sr), hostClient). @@ -662,7 +662,7 @@ func TestCreateSpaceRequest(t *testing.T) { HasSpecTargetClusterRoles(srClusterRoles). HasConditions(spacetest.Ready()). // condition is reflected from space status HasStatusTargetClusterURL(member1.APIEndpoint). // has new target cluster url - HasNamespaceAccess([]toolchainv1alpha1.NamespaceAccess{{Name: "jane-env1", SecretRef: "existingDevSecret"}, {Name: "jane-env2", SecretRef: "existingDevSecret2"}}). // expected secrets are there. The secret names may not match exactly since it is generated the first time it's created + HasNamespaceAccess([]toolchainv1alpha1.NamespaceAccess{{Name: "jane-env1", SecretRef: "existingDevSecret"}, {Name: "jane-env2", SecretRef: "existingDevSecret2"}}). // nolint:gosec // expected secrets are there. The secret names may not match exactly since it is generated the first time it's created HasFinalizer() // check that the secrets are there secrets := &corev1.SecretList{} @@ -1241,7 +1241,7 @@ func TestUpdateSpaceRequest(t *testing.T) { // spacerequest exists with expected cluster roles and finalizer spacerequesttest.AssertThatSpaceRequest(t, srNamespace.Name, sr.GetName(), member1.Client). HasSpecTierName("appstudio-env"). - HasNamespaceAccess([]toolchainv1alpha1.NamespaceAccess{{Name: "jane-env", SecretRef: "existingDevSecret"}}). // expected secrets are there. The secret name may not match exactly since it is generated the first time it's created + HasNamespaceAccess([]toolchainv1alpha1.NamespaceAccess{{Name: "jane-env", SecretRef: "existingDevSecret"}}). // nolint:gosec // expected secrets are there. The secret name may not match exactly since it is generated the first time it's created HasFinalizer() }) }) diff --git a/controllers/usersignup/usersignup_controller.go b/controllers/usersignup/usersignup_controller.go index e59f96e21..983c79f8e 100644 --- a/controllers/usersignup/usersignup_controller.go +++ b/controllers/usersignup/usersignup_controller.go @@ -830,9 +830,8 @@ func (r *Reconciler) annotateCaptchaAssessment(ctx context.Context, userSignup * // set the annotated assessment value: FRAUDULENT or LEGITIMATE userSignup.Annotations[toolchainv1alpha1.UserSignupCaptchaAnnotatedAssessmentAnnotationKey] = newAnnotationName - go func() { - gctx := context.Background() - rclient, err := recaptcha.NewClient(gctx) + go func(ctx context.Context) { + rclient, err := recaptcha.NewClient(ctx) if err != nil { logger.Error(err, "error creating reCAPTCHA client, cannot annotate assessment") return @@ -844,13 +843,13 @@ func (r *Reconciler) annotateCaptchaAssessment(ctx context.Context, userSignup * Annotation: newAssessmentAnnotation, } - response, err := rclient.AnnotateAssessment(gctx, annotateRequest) + response, err := rclient.AnnotateAssessment(ctx, annotateRequest) if err != nil { logger.Error(err, "error annotating assessment") return } logger.Info("Assessment annotated successfully", "assessment_annotation", newAnnotationName, "response", response.String()) - }() + }(ctx) } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 52757d484..07faf9e3d 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -108,6 +108,16 @@ func initMetrics() { HostOperatorCommitGaugeVec = newGaugeVec("host_operator_commit", "Full commit used to identify the version of the host operator", "commit") HostOperatorShortCommitGaugeVec = newGaugeVec("host_operator_short_commit", "Short commit used to identify the version of the host operator", "commit") ToolchainStatusGaugeVec = newGaugeVec("toolchain_status", "Current status of the toolchain components", "component") + + // set the version metrics + shortCommit := version.Commit + if len(version.Commit) >= 7 { + shortCommit = version.Commit[0:7] + } + HostOperatorVersionGaugeVec.WithLabelValues(shortCommit).Set(1) // the HostOperatorVersionGaugeVec is set to `1`, Grafana will display the 'commit' label as the version for the instant record + HostOperatorCommitGaugeVec.WithLabelValues(version.Commit).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit + HostOperatorShortCommitGaugeVec.WithLabelValues(shortCommit).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit + // Histograms UserSignupProvisionTimeHistogram = newHistogram("user_signup_provision_time", "UserSignup provision time in seconds") logger.Info("custom metrics initialized") @@ -205,10 +215,6 @@ func RegisterCustomMetrics() []prometheus.Collector { collectors = append(collectors, v) } - 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 - HostOperatorCommitGaugeVec.WithLabelValues(version.Commit).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit - HostOperatorShortCommitGaugeVec.WithLabelValues(version.Commit[0:7]).SetToCurrentTime() // automatically set the value to the current time, so that the highest value is the current commit - logger.Info("custom metrics registered") return collectors } diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index bf35f7a42..f319ec410 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -11,6 +11,7 @@ import ( toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" "github.com/codeready-toolchain/host-operator/pkg/metrics" metricstest "github.com/codeready-toolchain/host-operator/test/metrics" + "github.com/codeready-toolchain/host-operator/version" "github.com/codeready-toolchain/toolchain-common/pkg/test" "github.com/codeready-toolchain/toolchain-common/pkg/test/masteruserrecord" metricscommontest "github.com/codeready-toolchain/toolchain-common/pkg/test/metrics" @@ -44,29 +45,48 @@ func TestResetMetrics(t *testing.T) { } func TestGitCommitGauge(t *testing.T) { - t.Run("HostOperatorCommitGaugeVec", func(t *testing.T) { + t.Run("when commit is longer than 7 characters", func(t *testing.T) { // given + version.Commit = "short12-34567890" metrics.Reset() defer metrics.Reset() now := time.Now() // when - metrics.HostOperatorCommitGaugeVec.WithLabelValues("commit-1234567890").SetToCurrentTime() + metrics.Reset() // then - assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorCommitGaugeVec.WithLabelValues("commit-1234567890")), float64(time.Minute.Seconds())) + assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorShortCommitGaugeVec.WithLabelValues("short12")), float64(time.Minute.Seconds())) + assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorCommitGaugeVec.WithLabelValues("short12-34567890")), float64(time.Minute.Seconds())) }) - t.Run("HostOperatorShortCommitGaugeVec", func(t *testing.T) { + t.Run("when commit is shorter than 7 characters", func(t *testing.T) { // given + version.Commit = "short" metrics.Reset() defer metrics.Reset() now := time.Now() // when - metrics.HostOperatorShortCommitGaugeVec.WithLabelValues("hash-1234567890").SetToCurrentTime() + metrics.Reset() + + // then + assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorShortCommitGaugeVec.WithLabelValues("short")), float64(time.Minute.Seconds())) + assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorCommitGaugeVec.WithLabelValues("short")), float64(time.Minute.Seconds())) + }) + + t.Run("when commit is empty", func(t *testing.T) { + // given + version.Commit = "" + metrics.Reset() + defer metrics.Reset() + now := time.Now() + + // when + metrics.Reset() // then - assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorShortCommitGaugeVec.WithLabelValues("hash-1234567890")), float64(time.Minute.Seconds())) + assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorShortCommitGaugeVec.WithLabelValues("")), float64(time.Minute.Seconds())) + assert.InDelta(t, float64(now.Unix()), promtestutil.ToFloat64(metrics.HostOperatorCommitGaugeVec.WithLabelValues("")), float64(time.Minute.Seconds())) }) } func TestIncrementMasterUserRecordCount(t *testing.T) {