Skip to content
Snippets Groups Projects
Commit c76a8dc9 authored by Georgios Tsoulos's avatar Georgios Tsoulos
Browse files

Add monitoring basic struct and WordPressObjectsProcessed,...

Add monitoring basic struct and WordPressObjectsProcessed, WordPressReconcileDuration and WordPressReconcileFailures metrics
parent 26cdd4c5
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"time"
"github.com/go-logr/logr"
k8stypes "k8s.io/apimachinery/pkg/types"
......@@ -13,6 +14,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
types "gitlab.cern.ch/wordpress/paas/wordpress-operator/api/v1"
"gitlab.cern.ch/wordpress/paas/wordpress-operator/internal"
"gitlab.cern.ch/wordpress/paas/wordpress-operator/internal/monitoring"
)
// ReconcileWordPress creates or patches resources as necessary to match the deployed crd manifest.
......@@ -22,12 +25,23 @@ func (r *WordPressReconciler) reconcileWordPress(
req reconcile.Request,
wp *types.WordPress,
) error {
startTime := time.Now()
log.V(logLevelDebug).Info("Attempting to reconcile WordPress release")
err := r.updateWordpress(ctx, log, req.NamespacedName, wp, &wp.Status)
if err != nil {
// inv reconcile error counter
monitoring.WordPressReconcileFailures.WithLabelValues(req.Namespace, internal.ExtractErrorReason(err)).Inc()
return err
}
// Update wp operator monitoring data
// Record the duration of the reconciliation
duration := time.Since(startTime).Seconds()
monitoring.WordPressReconcileDuration.Observe(duration)
// Increment the counter for processed WordPress objects
monitoring.WordPressObjectsProcessed.Inc()
return nil
}
......
package monitoring
import (
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)
const (
bucketStep = 0.1
bucketCount = 100
)
var (
// WordPressObjectsProcessed counts how many WordPress objects have been processed.
WordPressObjectsProcessed = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "wordpress_objects_processed_total",
Help: "Total number of WordPress objects processed (created, updated or deleted).",
},
)
// Exported Histogram for reconcile duration
WordPressReconcileDuration = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "wordpress_reconcile_duration_seconds",
Help: "Duration in seconds for reconciling WordPress objects.",
Buckets: prometheus.LinearBuckets(bucketStep, bucketStep, bucketCount),
},
)
// Exported CounterVec for reconcile failures.
WordPressReconcileFailures = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "wordpress_reconcile_failures_total",
Help: "Total number of reconciliation failures for WordPress objects.",
},
[]string{"namespace", "reason"},
)
)
// Register metrics with the global prometheus registry
func init() {
metrics.Registry.MustRegister(WordPressObjectsProcessed)
metrics.Registry.MustRegister(WordPressReconcileDuration)
metrics.Registry.MustRegister(WordPressReconcileFailures)
}
package internal
import (
"fmt"
"os"
"path/filepath"
"regexp"
......@@ -18,3 +19,12 @@ func GetFullPath(path string) string {
return filepath.Join(string(rootPath), path)
}
// ExtractErrorReason extracts a string representing the error reason.
func ExtractErrorReason(err error) string {
if err == nil {
return "none"
}
return fmt.Sprintf("%T", err)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment