Skip to content

Commit 6948192

Browse files
Merge pull request #1603 from anik120/subs_sync_metric_re-fix
Bug 1822396: Update metric when Subscription is updated
2 parents afd2348 + 2b658ed commit 6948192

File tree

3 files changed

+81
-17
lines changed

3 files changed

+81
-17
lines changed

pkg/metrics/metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,5 +267,7 @@ func UpdateSubsSyncCounterStorage(sub *olmv1alpha1.Subscription) {
267267
counterValues.installedCSV = sub.Status.InstalledCSV
268268
counterValues.pkg = sub.Spec.Package
269269
counterValues.channel = sub.Spec.Channel
270+
271+
subscriptionSyncCounters[sub.GetName()] = counterValues
270272
}
271273
}

test/e2e/like_metric_matcher_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ func WithLabel(n, v string) MetricPredicate {
4747
}
4848
}
4949

50+
func WithName(name string) MetricPredicate {
51+
return WithLabel("name", name)
52+
}
53+
54+
func WithChannel(channel string) MetricPredicate {
55+
return WithLabel("channel", channel)
56+
}
57+
58+
func WithPackage(pkg string) MetricPredicate {
59+
return WithLabel("package", pkg)
60+
}
61+
62+
func WithPhase(phase string) MetricPredicate {
63+
return WithLabel("phase", phase)
64+
}
65+
66+
func WithReason(reason string) MetricPredicate {
67+
return WithLabel("reason", reason)
68+
}
69+
70+
func WithVersion(version string) MetricPredicate {
71+
return WithLabel("version", version)
72+
}
73+
5074
func WithValue(v float64) MetricPredicate {
5175
return MetricPredicate{
5276
name: fmt.Sprintf("WithValue(%g)", v),

test/e2e/metrics_e2e_test.go

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
7575
Expect(getMetricsFromPod(c, getPodWithLabel(c, "app=olm-operator"), "8081")).To(And(
7676
ContainElement(LikeMetric(
7777
WithFamily("csv_abnormal"),
78-
WithLabel("name", failingCSV.Name),
79-
WithLabel("phase", "Failed"),
80-
WithLabel("reason", "UnsupportedOperatorGroup"),
81-
WithLabel("version", "0.0.0"),
78+
WithName(failingCSV.Name),
79+
WithPhase("Failed"),
80+
WithReason("UnsupportedOperatorGroup"),
81+
WithVersion("0.0.0"),
8282
)),
8383
ContainElement(LikeMetric(
8484
WithFamily("csv_succeeded"),
8585
WithValue(0),
86-
WithLabel("name", failingCSV.Name),
86+
WithName(failingCSV.Name),
8787
)),
8888
))
8989

@@ -101,8 +101,8 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
101101
It("deletes its associated CSV metrics", func() {
102102
// Verify that when the csv has been deleted, it deletes the corresponding CSV metrics
103103
Expect(getMetricsFromPod(c, getPodWithLabel(c, "app=olm-operator"), "8081")).ToNot(And(
104-
ContainElement(LikeMetric(WithFamily("csv_abnormal"), WithLabel("name", failingCSV.Name))),
105-
ContainElement(LikeMetric(WithFamily("csv_succeeded"), WithLabel("name", failingCSV.Name))),
104+
ContainElement(LikeMetric(WithFamily("csv_abnormal"), WithName(failingCSV.Name))),
105+
ContainElement(LikeMetric(WithFamily("csv_succeeded"), WithName(failingCSV.Name))),
106106
))
107107
})
108108
})
@@ -133,9 +133,9 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
133133
return getMetricsFromPod(c, getPodWithLabel(c, "app=catalog-operator"), "8081")
134134
}).Should(ContainElement(LikeMetric(
135135
WithFamily("subscription_sync_total"),
136-
WithLabel("name", "metric-subscription-for-create"),
137-
WithLabel("channel", stableChannel),
138-
WithLabel("package", testPackageName),
136+
WithName("metric-subscription-for-create"),
137+
WithChannel(stableChannel),
138+
WithPackage(testPackageName),
139139
)))
140140
})
141141
})
@@ -152,7 +152,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
152152
if err != nil {
153153
return err
154154
}
155-
s.Spec.Channel = "beta"
155+
s.Spec.Channel = betaChannel
156156
_, err = crc.OperatorsV1alpha1().Subscriptions(s.GetNamespace()).Update(context.TODO(), s, metav1.UpdateOptions{})
157157
return err
158158
}).Should(Succeed())
@@ -170,17 +170,55 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
170170
}).Should(And(
171171
Not(ContainElement(LikeMetric(
172172
WithFamily("subscription_sync_total"),
173-
WithLabel("name", "metric-subscription-for-update"),
174-
WithLabel("channel", stableChannel),
173+
WithName("metric-subscription-for-update"),
174+
WithChannel(stableChannel),
175+
WithPackage(testPackageName),
175176
))),
176177
ContainElement(LikeMetric(
177178
WithFamily("subscription_sync_total"),
178-
WithLabel("name", "metric-subscription-for-update"),
179-
WithLabel("channel", "beta"),
180-
WithLabel("package", testPackageName),
179+
WithName("metric-subscription-for-update"),
180+
WithChannel(betaChannel),
181+
WithPackage(testPackageName),
181182
)),
182183
))
183184
})
185+
When("The subscription object is updated again", func() {
186+
187+
BeforeEach(func() {
188+
Eventually(func() error {
189+
s, err := crc.OperatorsV1alpha1().Subscriptions(subscription.GetNamespace()).Get(context.TODO(), subscription.GetName(), metav1.GetOptions{})
190+
if err != nil {
191+
return err
192+
}
193+
s.Spec.Channel = alphaChannel
194+
_, err = crc.OperatorsV1alpha1().Subscriptions(s.GetNamespace()).Update(context.TODO(), s, metav1.UpdateOptions{})
195+
return err
196+
}).Should(Succeed())
197+
})
198+
199+
It("deletes the old subscription metric and emits the new metric(there is only one metric for the subscription)", func() {
200+
Eventually(func() []Metric {
201+
return getMetricsFromPod(c, getPodWithLabel(c, "app=catalog-operator"), "8081")
202+
}).Should(And(
203+
Not(ContainElement(LikeMetric(
204+
WithFamily("subscription_sync_total"),
205+
WithName("metric-subscription-for-update"),
206+
WithChannel(stableChannel),
207+
))),
208+
Not(ContainElement(LikeMetric(
209+
WithFamily("subscription_sync_total"),
210+
WithName("metric-subscription-for-update"),
211+
WithChannel(betaChannel),
212+
WithPackage(testPackageName),
213+
))),
214+
ContainElement(LikeMetric(
215+
WithFamily("subscription_sync_total"),
216+
WithName("metric-subscription-for-update"),
217+
WithChannel(alphaChannel),
218+
WithPackage(testPackageName),
219+
))))
220+
})
221+
})
184222
})
185223

186224
When("A subscription object is deleted after emitting metrics", func() {
@@ -205,7 +243,7 @@ var _ = Describe("Metrics are generated for OLM managed resources", func() {
205243
It("deletes the Subscription metric", func() {
206244
Eventually(func() []Metric {
207245
return getMetricsFromPod(c, getPodWithLabel(c, "app=catalog-operator"), "8081")
208-
}).ShouldNot(ContainElement(LikeMetric(WithFamily("subscription_sync_total"), WithLabel("name", "metric-subscription-for-delete"))))
246+
}).ShouldNot(ContainElement(LikeMetric(WithFamily("subscription_sync_total"), WithName("metric-subscription-for-delete"))))
209247
})
210248
})
211249
})

0 commit comments

Comments
 (0)