Skip to content

Commit c583fa5

Browse files
authored
fix: use safe prometheus.Register instead of MustRegister in main (#522)
1 parent eada4e6 commit c583fa5

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
branches: [ "master" ]
1010

1111
env:
12-
GO_VERSION: stable
12+
GO_VERSION: "1.24"
1313
GOLANGCI_LINT_VERSION: v2.1.1
1414

1515
jobs:

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ linters:
77
- common-false-positives
88
- legacy
99
- std-error-handling
10+
rules:
11+
# Suppress SA1019 deprecation warnings for aws-sdk-go v1 until migration to v2
12+
# is complete. Tracked in https://github.com/keikoproj/instance-manager/issues/523
13+
- linters:
14+
- staticcheck
15+
text: "SA1019.*\"github.com/aws/aws-sdk-go/"
1016
paths:
1117
- third_party$
1218
- builtin$

main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,18 @@ func main() {
137137
Ec2Metadata: metadata,
138138
}
139139

140-
prometheus.MustRegister(cacheCollector, controllerCollector)
140+
if err := prometheus.Register(cacheCollector); err != nil {
141+
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
142+
setupLog.Error(err, "failed to register cache metrics collector")
143+
os.Exit(1)
144+
}
145+
}
146+
if err := prometheus.Register(controllerCollector); err != nil {
147+
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
148+
setupLog.Error(err, "failed to register controller metrics collector")
149+
os.Exit(1)
150+
}
151+
}
141152
kube := kubeprovider.KubernetesClientSet{
142153
Kubernetes: client,
143154
KubeDynamic: dynClient,

0 commit comments

Comments
 (0)