Skip to content

Commit c89b8f7

Browse files
chansukechengfang
andauthored
refactor: replace global metrics initialization (#1140)
Signed-off-by: chansuke <[email protected]> Co-authored-by: Cheng Fang <[email protected]>
1 parent 1dfab3e commit c89b8f7

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

cmd/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ func newRunCommand() *cobra.Command {
138138
cfg.ClientOpts.Plaintext,
139139
)
140140

141+
// Initialize metrics before starting the metrics server or using any counters
142+
metrics.InitMetrics()
143+
141144
// Health server will start in a go routine and run asynchronously
142145
var hsErrCh chan error
143146
var msErrCh chan error

pkg/metrics/metrics.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ import (
99
"github.com/prometheus/client_golang/prometheus/promhttp"
1010
)
1111

12-
// TODO: These should not be global vars with this package
13-
var epm *EndpointMetrics
14-
var apm *ApplicationMetrics
15-
var cpm *ClientMetrics
12+
type Metrics struct {
13+
Endpoint *EndpointMetrics
14+
Applications *ApplicationMetrics
15+
Clients *ClientMetrics
16+
}
17+
18+
var defaultMetrics *Metrics
1619

1720
// EndpointMetrics stores metrics for registry endpoints
1821
type EndpointMetrics struct {
@@ -117,19 +120,36 @@ func NewClientMetrics() *ClientMetrics {
117120
return metrics
118121
}
119122

123+
func NewMetrics() *Metrics {
124+
return &Metrics{
125+
Endpoint: NewEndpointMetrics(),
126+
Applications: NewApplicationsMetrics(),
127+
Clients: NewClientMetrics(),
128+
}
129+
}
130+
120131
// Endpoint returns the global EndpointMetrics object
121132
func Endpoint() *EndpointMetrics {
122-
return epm
133+
if defaultMetrics == nil {
134+
return nil
135+
}
136+
return defaultMetrics.Endpoint
123137
}
124138

125139
// Applications returns the global ApplicationMetrics object
126140
func Applications() *ApplicationMetrics {
127-
return apm
141+
if defaultMetrics == nil {
142+
return nil
143+
}
144+
return defaultMetrics.Applications
128145
}
129146

130147
// Clients returns the global ClientMetrics object
131148
func Clients() *ClientMetrics {
132-
return cpm
149+
if defaultMetrics == nil {
150+
return nil
151+
}
152+
return defaultMetrics.Clients
133153
}
134154

135155
// IncreaseRequest increases the request counter of EndpointMetrics object
@@ -180,9 +200,7 @@ func (cpm *ClientMetrics) IncreaseK8sClientError(by int) {
180200
cpm.kubeAPIRequestsErrorsTotal.Add(float64(by))
181201
}
182202

183-
// TODO: This is a lazy workaround, better initialize it somehwere else
184-
func init() {
185-
epm = NewEndpointMetrics()
186-
apm = NewApplicationsMetrics()
187-
cpm = NewClientMetrics()
203+
// InitMetrics initializes the global metrics objects
204+
func InitMetrics() {
205+
defaultMetrics = NewMetrics()
188206
}

pkg/metrics/metrics_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestMetricsInitialization(t *testing.T) {
5151
}
5252

5353
func TestMetricsOperations(t *testing.T) {
54+
InitMetrics()
5455
epm := Endpoint()
5556
epm.IncreaseRequest("/registry1", false)
5657
epm.IncreaseRequest("/registry1", true)

0 commit comments

Comments
 (0)