diff --git a/cmd/common.go b/cmd/common.go index acd3ce45..8455bbc9 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -14,6 +14,7 @@ import ( "github.com/argoproj-labs/argocd-image-updater/internal/controller" "github.com/argoproj-labs/argocd-image-updater/pkg/argocd" "github.com/argoproj-labs/argocd-image-updater/pkg/common" + "github.com/argoproj-labs/argocd-image-updater/pkg/metrics" "github.com/argoproj-labs/argocd-image-updater/pkg/webhook" "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/registry" ) @@ -30,6 +31,9 @@ type WebhookConfig struct { // SetupCommon initializes common components (logging, context, etc.) func SetupCommon(ctx context.Context, cfg *controller.ImageUpdaterConfig, setupLogger logr.Logger, commitMessagePath, kubeConfig string) error { + // Initialize metrics before starting the metrics server or using any counters + metrics.InitMetrics() + var commitMessageTpl string // User can specify a path to a template used for Git commit messages diff --git a/internal/controller/reconcile.go b/internal/controller/reconcile.go index 50bd3bab..0384f235 100644 --- a/internal/controller/reconcile.go +++ b/internal/controller/reconcile.go @@ -10,6 +10,7 @@ import ( iuapi "github.com/argoproj-labs/argocd-image-updater/api/v1alpha1" "github.com/argoproj-labs/argocd-image-updater/pkg/argocd" + "github.com/argoproj-labs/argocd-image-updater/pkg/metrics" "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/image" "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/log" "github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/registry" @@ -34,7 +35,7 @@ func (r *ImageUpdaterReconciler) RunImageUpdater(ctx context.Context, cr *iuapi. } // TODO: metrics will be implemented in GITOPS-7113 - //metrics.Applications().SetNumberOfApplications(len(appList)) + metrics.Applications().SetNumberOfApplications(len(appList)) if !warmUp { baseLogger.Infof("Starting image update cycle, considering %d application(s) for update", len(appList)) @@ -96,11 +97,11 @@ func (r *ImageUpdaterReconciler) RunImageUpdater(ctx context.Context, cr *iuapi. result.NumImagesUpdated += res.NumImagesUpdated result.NumSkipped += res.NumSkipped // TODO: metrics will be implemnted in GITOPS-7113 - //if !warmUp && !r.Config.DryRun { - // metrics.Applications().IncreaseImageUpdate(app, res.NumImagesUpdated) - //} - //metrics.Applications().IncreaseUpdateErrors(app, res.NumErrors) - //metrics.Applications().SetNumberOfImagesWatched(app, res.NumImagesConsidered) + if !warmUp && !r.Config.DryRun { + metrics.Applications().IncreaseImageUpdate(app, res.NumImagesUpdated) + } + metrics.Applications().IncreaseUpdateErrors(app, res.NumErrors) + metrics.Applications().SetNumberOfImagesWatched(app, res.NumImagesConsidered) wg.Done() }(app, curApplication) } diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index be47bfb2..36a84599 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -1,12 +1,10 @@ package metrics import ( - "fmt" - "net/http" + crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "github.com/prometheus/client_golang/prometheus/promhttp" ) type Metrics struct { @@ -39,26 +37,15 @@ type ClientMetrics struct { kubeAPIRequestsErrorsTotal prometheus.Counter } -// StartMetricsServer starts a new HTTP server for metrics on given port -func StartMetricsServer(port int) chan error { - errCh := make(chan error) - go func() { - sm := http.NewServeMux() - sm.Handle("/metrics", promhttp.Handler()) - errCh <- http.ListenAndServe(fmt.Sprintf(":%d", port), sm) - }() - return errCh -} - // NewEndpointMetrics returns a new endpoint metrics object func NewEndpointMetrics() *EndpointMetrics { metrics := &EndpointMetrics{} - metrics.requestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + metrics.requestsTotal = promauto.With(crmetrics.Registry).NewCounterVec(prometheus.CounterOpts{ Name: "argocd_image_updater_registry_requests_total", Help: "The total number of requests to this endpoint", }, []string{"registry"}) - metrics.requestsFailed = promauto.NewCounterVec(prometheus.CounterOpts{ + metrics.requestsFailed = promauto.With(crmetrics.Registry).NewCounterVec(prometheus.CounterOpts{ Name: "argocd_image_updater_registry_requests_failed_total", Help: "The number of failed requests to this endpoint", }, []string{"registry"}) @@ -70,22 +57,22 @@ func NewEndpointMetrics() *EndpointMetrics { func NewApplicationsMetrics() *ApplicationMetrics { metrics := &ApplicationMetrics{} - metrics.applicationsTotal = promauto.NewGauge(prometheus.GaugeOpts{ + metrics.applicationsTotal = promauto.With(crmetrics.Registry).NewGauge(prometheus.GaugeOpts{ Name: "argocd_image_updater_applications_watched_total", Help: "The total number of applications watched by Argo CD Image Updater", }) - metrics.imagesWatchedTotal = promauto.NewGaugeVec(prometheus.GaugeOpts{ + metrics.imagesWatchedTotal = promauto.With(crmetrics.Registry).NewGaugeVec(prometheus.GaugeOpts{ Name: "argocd_image_updater_images_watched_total", Help: "Number of images watched by Argo CD Image Updater", }, []string{"application"}) - metrics.imagesUpdatedTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + metrics.imagesUpdatedTotal = promauto.With(crmetrics.Registry).NewCounterVec(prometheus.CounterOpts{ Name: "argocd_image_updater_images_updated_total", Help: "Number of images updates by Argo CD Image Updater", }, []string{"application"}) - metrics.imagesUpdatedErrorsTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + metrics.imagesUpdatedErrorsTotal = promauto.With(crmetrics.Registry).NewCounterVec(prometheus.CounterOpts{ Name: "argocd_image_updater_images_errors_total", Help: "Number of errors reported by Argo CD Image Updater", }, []string{"application"}) @@ -97,22 +84,22 @@ func NewApplicationsMetrics() *ApplicationMetrics { func NewClientMetrics() *ClientMetrics { metrics := &ClientMetrics{} - metrics.argoCDRequestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + metrics.argoCDRequestsTotal = promauto.With(crmetrics.Registry).NewCounterVec(prometheus.CounterOpts{ Name: "argocd_image_updater_argocd_api_requests_total", Help: "The total number of Argo CD API requests performed by the Argo CD Image Updater", }, []string{"argocd_server"}) - metrics.argoCDRequestsErrorsTotal = promauto.NewCounterVec(prometheus.CounterOpts{ + metrics.argoCDRequestsErrorsTotal = promauto.With(crmetrics.Registry).NewCounterVec(prometheus.CounterOpts{ Name: "argocd_image_updater_argocd_api_errors_total", Help: "The total number of Argo CD API requests resulting in error", }, []string{"argocd_server"}) - metrics.kubeAPIRequestsTotal = promauto.NewCounter(prometheus.CounterOpts{ + metrics.kubeAPIRequestsTotal = promauto.With(crmetrics.Registry).NewCounter(prometheus.CounterOpts{ Name: "argocd_image_updater_k8s_api_requests_total", Help: "The total number of Argo CD API requests resulting in error", }) - metrics.kubeAPIRequestsErrorsTotal = promauto.NewCounter(prometheus.CounterOpts{ + metrics.kubeAPIRequestsErrorsTotal = promauto.With(crmetrics.Registry).NewCounter(prometheus.CounterOpts{ Name: "argocd_image_updater_k8s_api_errors_total", Help: "The total number of Argo CD API requests resulting in error", }) diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index 05af83f1..ef353b35 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -3,18 +3,22 @@ package metrics import ( "testing" + crmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" + "github.com/prometheus/client_golang/prometheus" "github.com/stretchr/testify/assert" ) func TestMetricsInitialization(t *testing.T) { t.Run("NewEndpointMetrics", func(t *testing.T) { + crmetrics.Registry = prometheus.NewRegistry() prometheus.DefaultRegisterer = prometheus.NewRegistry() epm := NewEndpointMetrics() assert.NotNil(t, epm) assert.NotNil(t, epm.requestsTotal) assert.NotNil(t, epm.requestsFailed) + crmetrics.Registry = prometheus.NewRegistry() prometheus.DefaultRegisterer = nil epm = NewEndpointMetrics() assert.NotNil(t, epm) @@ -23,6 +27,7 @@ func TestMetricsInitialization(t *testing.T) { }) t.Run("NewClientMetrics", func(t *testing.T) { + crmetrics.Registry = prometheus.NewRegistry() prometheus.DefaultRegisterer = prometheus.NewRegistry() cpm := NewClientMetrics() assert.NotNil(t, cpm) @@ -31,6 +36,7 @@ func TestMetricsInitialization(t *testing.T) { assert.NotNil(t, cpm.kubeAPIRequestsTotal) assert.NotNil(t, cpm.kubeAPIRequestsErrorsTotal) + crmetrics.Registry = prometheus.NewRegistry() prometheus.DefaultRegisterer = nil cpm = NewClientMetrics() assert.NotNil(t, cpm) @@ -41,6 +47,7 @@ func TestMetricsInitialization(t *testing.T) { }) t.Run("NewApplicationsMetrics", func(t *testing.T) { + crmetrics.Registry = prometheus.NewRegistry() apm := NewApplicationsMetrics() assert.NotNil(t, apm) assert.NotNil(t, apm.applicationsTotal) @@ -51,6 +58,8 @@ func TestMetricsInitialization(t *testing.T) { } func TestMetricsOperations(t *testing.T) { + crmetrics.Registry = prometheus.NewRegistry() + InitMetrics() epm := Endpoint() epm.IncreaseRequest("/registry1", false)