Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
13 changes: 7 additions & 6 deletions internal/controller/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
Expand Down Expand Up @@ -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)
}
Expand Down
35 changes: 11 additions & 24 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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"})
Expand All @@ -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"})
Expand All @@ -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",
})
Expand Down
9 changes: 9 additions & 0 deletions pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading