1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
package metrics
import (
"fmt"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// TODO: These should not be global vars with this package
var epm *EndpointMetrics
var apm *ApplicationMetrics
var cpm *ClientMetrics
// EndpointMetrics stores metrics for registry endpoints
type EndpointMetrics struct {
requestsTotal *prometheus.CounterVec
requestsFailed *prometheus.CounterVec
}
// ApplicationMetrics stores metrics for applications
type ApplicationMetrics struct {
applicationsTotal prometheus.Gauge
imagesWatchedTotal *prometheus.GaugeVec
imagesUpdatedTotal *prometheus.CounterVec
imagesUpdatedErrorsTotal *prometheus.CounterVec
}
// ClientMetrics stores metrics for K8s and ArgoCD clients
type ClientMetrics struct {
argoCDRequestsTotal *prometheus.CounterVec
argoCDRequestsErrorsTotal *prometheus.CounterVec
kubeAPIRequestsTotal prometheus.Counter
kubeAPIRequestsErrorsTotal prometheus.Counter
}
// StartMetricsServer starts a new HTTP server for metrics on given port
func StartMetricsServer(port int) chan error {
errCh := make(chan error)
go func() {
sm := http.NewServeMux()
sm.Handle("/metrics", promhttp.Handler())
errCh <- http.ListenAndServe(fmt.Sprintf(":%d", port), sm)
}()
return errCh
}
// NewEndpointMetrics returns a new endpoint metrics object
func NewEndpointMetrics() *EndpointMetrics {
metrics := &EndpointMetrics{}
metrics.requestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "argocd_image_updater_registry_requests_total",
Help: "The total number of requests to this endpoint",
}, []string{"registry"})
metrics.requestsFailed = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "argocd_image_updater_registry_requests_failed_total",
Help: "The number of failed requests to this endpoint",
}, []string{"registry"})
return metrics
}
// NewApplicationsMetrics returns a new application metrics object
func NewApplicationsMetrics() *ApplicationMetrics {
metrics := &ApplicationMetrics{}
metrics.applicationsTotal = promauto.NewGauge(prometheus.GaugeOpts{
Name: "argocd_image_updater_applications_watched_total",
Help: "The total number of applications watched by Argo CD Image Updater",
})
metrics.imagesWatchedTotal = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "argocd_image_updater_images_watched_total",
Help: "Number of images watched by Argo CD Image Updater",
}, []string{"application"})
metrics.imagesUpdatedTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "argocd_image_updater_images_updated_total",
Help: "Number of images updates by Argo CD Image Updater",
}, []string{"application"})
metrics.imagesUpdatedErrorsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "argocd_image_updater_images_errors_total",
Help: "Number of errors reported by Argo CD Image Updater",
}, []string{"application"})
return metrics
}
// NewClientMetrics returns a new client metrics object
func NewClientMetrics() *ClientMetrics {
metrics := &ClientMetrics{}
metrics.argoCDRequestsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "argocd_image_updater_argocd_api_requests_total",
Help: "The total number of Argo CD API requests performed by the Argo CD Image Updater",
}, []string{"argocd_server"})
metrics.argoCDRequestsErrorsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "argocd_image_updater_argocd_api_errors_total",
Help: "The total number of Argo CD API requests resulting in error",
}, []string{"argocd_server"})
metrics.kubeAPIRequestsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "argocd_image_updater_k8s_api_requests_total",
Help: "The total number of Argo CD API requests resulting in error",
})
metrics.kubeAPIRequestsErrorsTotal = promauto.NewCounter(prometheus.CounterOpts{
Name: "argocd_image_updater_k8s_api_errors_total",
Help: "The total number of Argo CD API requests resulting in error",
})
return metrics
}
// Endpoint returns the global EndpointMetrics object
func Endpoint() *EndpointMetrics {
return epm
}
// Applications returns the global ApplicationMetrics object
func Applications() *ApplicationMetrics {
return apm
}
// Clients returns the global ClientMetrics object
func Clients() *ClientMetrics {
return cpm
}
// IncreaseRequest increases the request counter of EndpointMetrics object
func (epm *EndpointMetrics) IncreaseRequest(registryURL string, isFailed bool) {
epm.requestsTotal.WithLabelValues(registryURL).Inc()
if isFailed {
epm.requestsFailed.WithLabelValues(registryURL).Inc()
}
}
// SetNumberOfApplications sets the total number of currently watched applications
func (apm *ApplicationMetrics) SetNumberOfApplications(num int) {
apm.applicationsTotal.Set(float64(num))
}
// SetNumberOfImagesWatched sets the total number of currently watched images for given application
func (apm *ApplicationMetrics) SetNumberOfImagesWatched(application string, num int) {
apm.imagesWatchedTotal.WithLabelValues(application).Set(float64(num))
}
// IncreaseImageUpdate increases the number of image updates for given application
func (apm *ApplicationMetrics) IncreaseImageUpdate(application string, by int) {
apm.imagesUpdatedTotal.WithLabelValues(application).Add(float64(by))
}
// IncreaseUpdateErrors increases the number of errors for given application occurred during update process
func (apm *ApplicationMetrics) IncreaseUpdateErrors(application string, by int) {
apm.imagesUpdatedErrorsTotal.WithLabelValues(application).Add(float64(by))
}
// IncreaseArgoCDClientRequest increases the number of Argo CD API requests for given server
func (cpm *ClientMetrics) IncreaseArgoCDClientRequest(server string, by int) {
cpm.argoCDRequestsTotal.WithLabelValues(server).Add(float64(by))
}
// IncreaseArgoCDClientError increases the number of failed Argo CD API requests for given server
func (cpm *ClientMetrics) IncreaseArgoCDClientError(server string, by int) {
cpm.argoCDRequestsErrorsTotal.WithLabelValues(server).Add(float64(by))
}
// IncreaseK8sClientRequest increases the number of K8s API requests
func (cpm *ClientMetrics) IncreaseK8sClientRequest(by int) {
cpm.kubeAPIRequestsTotal.Add(float64(by))
}
// IncreaseK8sClientRequest increases the number of failed K8s API requests
func (cpm *ClientMetrics) IncreaseK8sClientError(by int) {
cpm.kubeAPIRequestsErrorsTotal.Add(float64(by))
}
// TODO: This is a lazy workaround, better initialize it somehwere else
func init() {
epm = NewEndpointMetrics()
apm = NewApplicationsMetrics()
cpm = NewClientMetrics()
}
|