Skip to content

Commit e52eb8f

Browse files
committed
Made NewCustomMetrics() idempotent wrt Prometheus registration. Fixed unit tests by plugging CustomMetrics objects into the *clients* they allocate and use.
1 parent 5efc0bc commit e52eb8f

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

pkg/cloud/affinity_groups_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ var _ = Describe("AffinityGroup Unit Tests", func() {
8888
// Make the created VM go away quickly by force stopping it.
8989
p := realCSClient.VirtualMachine.NewStopVirtualMachineParams(*dummies.CSMachine1.Spec.InstanceID)
9090
p.SetForced(true)
91-
Ω(realCSClient.VirtualMachine.StopVirtualMachine(p)).Should(Succeed())
91+
_, err := realCSClient.VirtualMachine.StopVirtualMachine(p)
92+
Ω(err).ShouldNot(HaveOccurred())
9293
})
9394

9495
It("Creates and deletes an affinity group.", func() {

pkg/cloud/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (origC *client) NewClientFromSpec(cfg Config) (Client, error) {
9494
// comments for more details
9595
newC.cs = cloudstack.NewAsyncClient(newC.config.APIURL, newC.config.APIKey, newC.config.SecretKey, newC.config.VerifySSL)
9696
newC.csAsync = cloudstack.NewClient(newC.config.APIURL, newC.config.APIKey, newC.config.SecretKey, newC.config.VerifySSL)
97+
newC.customMetrics = metrics.NewCustomMetrics()
9798

9899
_, err := newC.cs.APIDiscovery.ListApis(newC.cs.APIDiscovery.NewListApisParams())
99100
if err != nil && strings.Contains(strings.ToLower(err.Error()), "i/o timeout") {
@@ -103,6 +104,6 @@ func (origC *client) NewClientFromSpec(cfg Config) (Client, error) {
103104
}
104105

105106
func NewClientFromCSAPIClient(cs *cloudstack.CloudStackClient) Client {
106-
c := &client{cs: cs, csAsync: cs}
107+
c := &client{cs: cs, csAsync: cs, customMetrics: metrics.NewCustomMetrics()}
107108
return c
108109
}

pkg/metrics/metrics.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ func NewCustomMetrics() AcsCustomMetrics {
3939
},
4040
[]string{"acs_error_code"},
4141
)
42-
crtlmetrics.Registry.MustRegister(customMetrics.acsReconciliationErrorCount)
42+
if err := crtlmetrics.Registry.Register(customMetrics.acsReconciliationErrorCount); err != nil {
43+
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
44+
customMetrics.acsReconciliationErrorCount = are.ExistingCollector.(*prometheus.CounterVec)
45+
} else {
46+
// Something else went wrong!
47+
panic(err)
48+
}
49+
}
4350

4451
// ACS standard error messages of the form "CloudStack API error 431 (CSExceptionErrorCode: 9999):..."
4552
// This regexp is used to extract CSExceptionCodes from the message.

0 commit comments

Comments
 (0)