Skip to content

Commit f27e717

Browse files
Add Errors Reporting Prometheus Metric (#345)
1 parent a6fd2a1 commit f27e717

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

pkg/controller/sync.go

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ func (c *Controller) checkContainer(ctx context.Context, log *logrus.Entry,
9393

9494
result, err := c.checker.Container(ctx, log, pod, container, opts)
9595
if err != nil {
96+
// Report the error using ErrorsReporting
97+
c.metrics.ErrorsReporting(pod.Namespace, pod.Name, container.Name, container.Image)
9698
return err
9799
}
98100

pkg/metrics/metrics.go

+17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Metrics struct {
2525
registry *prometheus.Registry
2626
containerImageVersion *prometheus.GaugeVec
2727
containerImageDuration *prometheus.GaugeVec
28+
containerImageErrors *prometheus.CounterVec
2829

2930
// Contains all metrics for the roundtripper
3031
roundTripper *RoundTripper
@@ -62,12 +63,23 @@ func NewServer(log *logrus.Entry) *Metrics {
6263
},
6364
[]string{"namespace", "pod", "container", "image"},
6465
)
66+
containerImageErrors := promauto.With(reg).NewCounterVec(
67+
prometheus.CounterOpts{
68+
Namespace: "version_checker",
69+
Name: "image_failures_total",
70+
Help: "Total number of errors where the version-checker was unable to get the latest upstream registry version",
71+
},
72+
[]string{
73+
"namespace", "pod", "container", "image",
74+
},
75+
)
6576

6677
return &Metrics{
6778
log: log.WithField("module", "metrics"),
6879
registry: reg,
6980
containerImageVersion: containerImageVersion,
7081
containerImageDuration: containerImageDuration,
82+
containerImageErrors: containerImageErrors,
7183
containerCache: make(map[string]cacheItem),
7284
roundTripper: NewRoundTripper(reg),
7385
}
@@ -160,6 +172,11 @@ func (m *Metrics) latestImageIndex(namespace, pod, container, containerType stri
160172
return strings.Join([]string{namespace, pod, container, containerType}, "")
161173
}
162174

175+
func (m *Metrics) ErrorsReporting(namespace, pod, container, imageURL string) {
176+
177+
m.containerImageErrors.WithLabelValues(namespace, pod, container, imageURL).Inc()
178+
}
179+
163180
func (m *Metrics) buildLabels(namespace, pod, container, containerType, imageURL, currentVersion, latestVersion string) prometheus.Labels {
164181
return prometheus.Labels{
165182
"namespace": namespace,

pkg/metrics/metrics_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/prometheus/client_golang/prometheus"
78
"github.com/prometheus/client_golang/prometheus/testutil"
89
"github.com/sirupsen/logrus"
10+
"github.com/stretchr/testify/assert"
911
)
1012

1113
func TestCache(t *testing.T) {
@@ -37,3 +39,43 @@ func TestCache(t *testing.T) {
3739
}
3840
}
3941
}
42+
43+
// TestErrorsReporting verifies that the error metric increments correctly
44+
func TestErrorsReporting(t *testing.T) {
45+
m := NewServer(logrus.NewEntry(logrus.New()))
46+
47+
// Reset the metrics before testing
48+
m.containerImageErrors.Reset()
49+
50+
testCases := []struct {
51+
namespace string
52+
pod string
53+
container string
54+
image string
55+
expected int
56+
}{
57+
{"namespace", "pod", "container", "url", 1},
58+
{"namespace", "pod", "container", "url", 2},
59+
{"namespace2", "pod2", "container2", "url2", 1},
60+
}
61+
62+
for i, tc := range testCases {
63+
t.Run(fmt.Sprintf("Case %d", i+1), func(t *testing.T) {
64+
// Report an error
65+
m.ErrorsReporting(tc.namespace, tc.pod, tc.container, tc.image)
66+
67+
// Retrieve metric
68+
metric, err := m.containerImageErrors.GetMetricWith(prometheus.Labels{
69+
"namespace": tc.namespace,
70+
"pod": tc.pod,
71+
"container": tc.container,
72+
"image": tc.image,
73+
})
74+
assert.NoError(t, err, "Failed to get metric with labels")
75+
76+
// Validate metric count
77+
fetchErrorCount := testutil.ToFloat64(metric)
78+
assert.Equal(t, float64(tc.expected), fetchErrorCount, "Expected error count to increment correctly")
79+
})
80+
}
81+
}

0 commit comments

Comments
 (0)