@@ -9,10 +9,13 @@ import (
9
9
"github.com/prometheus/client_golang/prometheus/promhttp"
10
10
)
11
11
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
16
19
17
20
// EndpointMetrics stores metrics for registry endpoints
18
21
type EndpointMetrics struct {
@@ -117,19 +120,36 @@ func NewClientMetrics() *ClientMetrics {
117
120
return metrics
118
121
}
119
122
123
+ func NewMetrics () * Metrics {
124
+ return & Metrics {
125
+ Endpoint : NewEndpointMetrics (),
126
+ Applications : NewApplicationsMetrics (),
127
+ Clients : NewClientMetrics (),
128
+ }
129
+ }
130
+
120
131
// Endpoint returns the global EndpointMetrics object
121
132
func Endpoint () * EndpointMetrics {
122
- return epm
133
+ if defaultMetrics == nil {
134
+ return nil
135
+ }
136
+ return defaultMetrics .Endpoint
123
137
}
124
138
125
139
// Applications returns the global ApplicationMetrics object
126
140
func Applications () * ApplicationMetrics {
127
- return apm
141
+ if defaultMetrics == nil {
142
+ return nil
143
+ }
144
+ return defaultMetrics .Applications
128
145
}
129
146
130
147
// Clients returns the global ClientMetrics object
131
148
func Clients () * ClientMetrics {
132
- return cpm
149
+ if defaultMetrics == nil {
150
+ return nil
151
+ }
152
+ return defaultMetrics .Clients
133
153
}
134
154
135
155
// IncreaseRequest increases the request counter of EndpointMetrics object
@@ -180,9 +200,7 @@ func (cpm *ClientMetrics) IncreaseK8sClientError(by int) {
180
200
cpm .kubeAPIRequestsErrorsTotal .Add (float64 (by ))
181
201
}
182
202
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 ()
188
206
}
0 commit comments