Skip to content

Commit

Permalink
feat(metrics): add image_caching_request metric
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Oct 31, 2024
1 parent 0129733 commit c57184a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions api/kuik/v1alpha1/cachedimage_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ func (r *CachedImage) Repository() (reference.Named, error) {
return named, nil
}

func (r *CachedImage) Upstream() (string, error) {
named, err := r.Repository()
if err != nil {
return "", err
}

return reference.Domain(named), nil
}

func (r *CachedImage) GetPullSecrets(apiReader client.Reader) ([]corev1.Secret, error) {
named, err := r.Repository()
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions internal/controller/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ import (
const subsystem = "controller"

var (
ImageCachingRequest = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: kuikMetrics.Namespace,
Subsystem: subsystem,
Name: "image_caching_request",
Help: "Number of request to cache an image",
},
[]string{"successful", "upstream_registry"},
)
ImagePutInCache = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: kuikMetrics.Namespace,
Expand Down Expand Up @@ -69,6 +78,7 @@ var (
func RegisterMetrics(client client.Client) {
// Register custom metrics with the global prometheus registry
metrics.Registry.MustRegister(
ImageCachingRequest,
ImagePutInCache,
ImageRemovedFromCache,
kuikMetrics.NewInfo(subsystem),
Expand Down
10 changes: 7 additions & 3 deletions internal/controller/kuik/cachedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/x509"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -239,8 +240,14 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
putImageInCache = false
}
if putImageInCache {
upstream, err := cachedImage.Upstream()
if err != nil {
return ctrl.Result{}, err
}

r.Recorder.Eventf(&cachedImage, "Normal", "Caching", "Start caching image %s", cachedImage.Spec.SourceImage)
err = r.cacheImage(&cachedImage)
kuikController.ImageCachingRequest.WithLabelValues(strconv.FormatBool(err == nil), upstream).Inc()
if err != nil {
log.Error(err, "failed to cache image")
r.Recorder.Eventf(&cachedImage, "Warning", "CacheFailed", "Failed to cache image %s, reason: %s", cachedImage.Spec.SourceImage, err)
Expand Down Expand Up @@ -401,9 +408,6 @@ func (r *CachedImageReconciler) SetupWithManager(mgr ctrl.Manager, maxConcurrent
&corev1.Pod{},
handler.EnqueueRequestsFromMapFunc(r.cachedImagesRequestFromPod),
builder.WithPredicates(predicate.Funcs{
// GenericFunc: func(e event.GenericEvent) bool {
// return true
// },
DeleteFunc: func(e event.DeleteEvent) bool {
pod := e.Object.(*corev1.Pod)
var currentPod corev1.Pod
Expand Down

0 comments on commit c57184a

Please sign in to comment.