@@ -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
1821type 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
121132func 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
126140func 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
131148func 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}
0 commit comments