Skip to content

Commit 8f74d60

Browse files
kubasikusraks-tt
authored andcommitted
PVO11Y-5002 Add node label to metrics
This label is needed for per-node grafana dashboards. The node name is provided as environment variable by kubernetes at pod runtime and is then added to metric labels. The exporter cannot run without this node env variable defined, as metrics would be hard to link to the origin node.
1 parent 2fe5a57 commit 8f74d60

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

config/exporters/monitoring/registry/base/registry-exporter-ds.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ spec:
8686
value: "/.docker/"
8787
- name: QUAY_URL
8888
value: quay.io/redhat-user-workloads/appstudio-registry-exporter/appstudio-registry-exporter
89+
- name: NODE_NAME
90+
valueFrom:
91+
fieldRef:
92+
fieldPath: spec.nodeName
8993

9094
volumeMounts:
9195
- name: quay-auth

exporters/registryexporter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,4 @@ The exporter can be run with the following commands:
138138
1. Build the exporter image locally by running:
139139
- `podman build . -t quay-exporter`
140140
1. Run the image locally
141-
- `podman run -it -p 9101:9101 -e=DOCKER_CONFIG="/.docker/" -e=QUAY_URL=${QUAY_URL} -v ${PATH_TO_CONFIG}:/.docker/config.json -v ${PATH_TO_TEST_DIR}:/mnt/storage localhost/quay-exporter registryexporter`
141+
- `podman run -it -p 9101:9101 -e=DOCKER_CONFIG="/.docker/" -e=QUAY_URL=${QUAY_URL} -e=NODE_NAME="test_node" -v ${PATH_TO_CONFIG}:/.docker/config.json -v ${PATH_TO_TEST_DIR}:/mnt/storage localhost/quay-exporter registryexporter`

exporters/registryexporter/registryexporter.go

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const scrapeInterval = 1 * time.Minute
6666
const pullArtifactPath = "/mnt/storage/pull-artifact.txt"
6767
const pullTag = ":pull"
6868
const metadataTag = ":metadata"
69+
var k8sNodeName = os.Getenv("NODE_NAME")
6970

7071
// Target file size in bytes (10MB)
7172
const targetFileSize = 10 * 1024 * 1024
@@ -79,59 +80,59 @@ func InitMetrics(reg prometheus.Registerer, registryMap map[string]RegistryConfi
7980
Name: "registry_exporter_pull_success",
8081
Help: "Gauge indicating if a pull from the registry was successful (1 if successful, 0 otherwise).",
8182
},
82-
[]string{"tested_registry"},
83+
[]string{"tested_registry","node"},
8384
),
8485
RegistryPullErrorCount: prometheus.NewCounterVec(
8586
prometheus.CounterOpts{
8687
Name: "registry_exporter_pull_error_count",
8788
Help: "Total number of errors encountered during pulls from the registry.",
8889
},
89-
[]string{"tested_registry", "error"},
90+
[]string{"tested_registry","node", "error"},
9091
),
9192
RegistryPullDuration: prometheus.NewHistogramVec(
9293
prometheus.HistogramOpts{
9394
Name: "registry_exporter_pull_duration_seconds",
9495
Help: "Histogram of durations for pulls from the registry in seconds.",
9596
Buckets: []float64{2, 3, 4, 5},
9697
},
97-
[]string{"tested_registry"},
98+
[]string{"tested_registry","node"},
9899
),
99100
RegistryPullImageSize: prometheus.NewGaugeVec(
100101
prometheus.GaugeOpts{
101102
Name: "registry_exporter_pull_image_size_mbytes",
102103
Help: "Gauge of image size for pulls from the registry in megabytes.",
103104
},
104-
[]string{"tested_registry"},
105+
[]string{"tested_registry","node"},
105106
),
106107
// Push metrics
107108
RegistryPushSuccess: prometheus.NewGaugeVec(
108109
prometheus.GaugeOpts{
109110
Name: "registry_exporter_push_success",
110111
Help: "Gauge indicating if a push to the registry was successful (1 if successful, 0 otherwise).",
111112
},
112-
[]string{"tested_registry"},
113+
[]string{"tested_registry","node"},
113114
),
114115
RegistryPushErrorCount: prometheus.NewCounterVec(
115116
prometheus.CounterOpts{
116117
Name: "registry_exporter_push_error_count",
117118
Help: "Total number of errors encountered during pushes to the registry.",
118119
},
119-
[]string{"tested_registry", "error"},
120+
[]string{"tested_registry","node", "error"},
120121
),
121122
RegistryPushDuration: prometheus.NewHistogramVec(
122123
prometheus.HistogramOpts{
123124
Name: "registry_exporter_push_duration_seconds",
124125
Help: "Histogram of durations for pushes to the registry.",
125126
Buckets: []float64{5, 6, 7, 8, 9, 10},
126127
},
127-
[]string{"tested_registry"},
128+
[]string{"tested_registry","node"},
128129
),
129130
RegistryPushImageSize: prometheus.NewGaugeVec(
130131
prometheus.GaugeOpts{
131132
Name: "registry_exporter_push_image_size_mbytes",
132133
Help: "Gauge of image size for pushes to the registry in megabytes.",
133134
},
134-
[]string{"tested_registry"},
135+
[]string{"tested_registry","node"},
135136
),
136137

137138
// Metadata metrics
@@ -140,22 +141,22 @@ func InitMetrics(reg prometheus.Registerer, registryMap map[string]RegistryConfi
140141
Name: "registry_exporter_metadata_success",
141142
Help: "Gauge indicating if a metadata test for the registry was successful (1 if successful, 0 otherwise).",
142143
},
143-
[]string{"tested_registry"},
144+
[]string{"tested_registry","node"},
144145
),
145146
RegistryMetadataErrorCount: prometheus.NewCounterVec(
146147
prometheus.CounterOpts{
147148
Name: "registry_exporter_metadata_error_count",
148149
Help: "Total number of errors encountered during metadata tests for the registry.",
149150
},
150-
[]string{"tested_registry", "error"},
151+
[]string{"tested_registry","node", "error"},
151152
),
152153
RegistryMetadataDuration: prometheus.NewHistogramVec(
153154
prometheus.HistogramOpts{
154155
Name: "registry_exporter_metadata_duration_seconds",
155156
Help: "Histogram of durations for metadata tests for the registry in seconds.",
156157
Buckets: []float64{2, 3, 4, 5},
157158
},
158-
[]string{"tested_registry"},
159+
[]string{"tested_registry","node"},
159160
),
160161

161162
// Authentication metrics
@@ -164,22 +165,22 @@ func InitMetrics(reg prometheus.Registerer, registryMap map[string]RegistryConfi
164165
Name: "registry_exporter_authentication_success",
165166
Help: "Gauge indicating if an authentication test for the registry was successful (1 if successful, 0 otherwise).",
166167
},
167-
[]string{"tested_registry"},
168+
[]string{"tested_registry","node"},
168169
),
169170
RegistryAuthenticationErrorCount: prometheus.NewCounterVec(
170171
prometheus.CounterOpts{
171172
Name: "registry_exporter_authentication_error_count",
172173
Help: "Total number of errors encountered during authentication tests for the registry.",
173174
},
174-
[]string{"tested_registry", "error"},
175+
[]string{"tested_registry","node", "error"},
175176
),
176177
RegistryAuthenticationDuration: prometheus.NewHistogramVec(
177178
prometheus.HistogramOpts{
178179
Name: "registry_exporter_authentication_duration_seconds",
179180
Help: "Histogram of durations for authentication tests for the registry in seconds.",
180181
Buckets: []float64{1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5},
181182
},
182-
[]string{"tested_registry"},
183+
[]string{"tested_registry","node"},
183184
),
184185
}
185186
reg.MustRegister(m.RegistryPullSuccess)
@@ -232,13 +233,13 @@ func ExtractErrorReason(output []byte) string {
232233
func setSuccessState(metrics *Metrics, registryType string, testType string) {
233234
switch testType {
234235
case "pull":
235-
metrics.RegistryPullSuccess.WithLabelValues(registryType).Set(1)
236+
metrics.RegistryPullSuccess.WithLabelValues(registryType,k8sNodeName).Set(1)
236237
case "push":
237-
metrics.RegistryPushSuccess.WithLabelValues(registryType).Set(1)
238+
metrics.RegistryPushSuccess.WithLabelValues(registryType,k8sNodeName).Set(1)
238239
case "metadata":
239-
metrics.RegistryMetadataSuccess.WithLabelValues(registryType).Set(1)
240+
metrics.RegistryMetadataSuccess.WithLabelValues(registryType,k8sNodeName).Set(1)
240241
case "authentication":
241-
metrics.RegistryAuthenticationSuccess.WithLabelValues(registryType).Set(1)
242+
metrics.RegistryAuthenticationSuccess.WithLabelValues(registryType,k8sNodeName).Set(1)
242243
}
243244
}
244245

@@ -248,16 +249,16 @@ func setErrorState(metrics *Metrics, registryType string, testType string, outpu
248249

249250
switch testType {
250251
case "pull":
251-
metrics.RegistryPullSuccess.WithLabelValues(registryType).Set(0)
252+
metrics.RegistryPullSuccess.WithLabelValues(registryType,k8sNodeName).Set(0)
252253
metrics.RegistryPullErrorCount.WithLabelValues(registryType, errorType).Inc()
253254
case "push":
254-
metrics.RegistryPushSuccess.WithLabelValues(registryType).Set(0)
255+
metrics.RegistryPushSuccess.WithLabelValues(registryType,k8sNodeName).Set(0)
255256
metrics.RegistryPushErrorCount.WithLabelValues(registryType, errorType).Inc()
256257
case "metadata":
257-
metrics.RegistryMetadataSuccess.WithLabelValues(registryType).Set(0)
258+
metrics.RegistryMetadataSuccess.WithLabelValues(registryType,k8sNodeName).Set(0)
258259
metrics.RegistryMetadataErrorCount.WithLabelValues(registryType, errorType).Inc()
259260
case "authentication":
260-
metrics.RegistryAuthenticationSuccess.WithLabelValues(registryType).Set(0)
261+
metrics.RegistryAuthenticationSuccess.WithLabelValues(registryType,k8sNodeName).Set(0)
261262
metrics.RegistryAuthenticationErrorCount.WithLabelValues(registryType, errorType).Inc()
262263
}
263264
}
@@ -275,9 +276,9 @@ func getFileSizeMB(filePath string) (float64, error) {
275276
func setImageSize(metrics *Metrics, registryType string, testType string, sizeMB float64) {
276277
switch testType {
277278
case "pull":
278-
metrics.RegistryPullImageSize.WithLabelValues(registryType).Set(sizeMB)
279+
metrics.RegistryPullImageSize.WithLabelValues(registryType,k8sNodeName).Set(sizeMB)
279280
case "push":
280-
metrics.RegistryPushImageSize.WithLabelValues(registryType).Set(sizeMB)
281+
metrics.RegistryPushImageSize.WithLabelValues(registryType,k8sNodeName).Set(sizeMB)
281282
}
282283
}
283284

@@ -286,13 +287,13 @@ func recordDuration(metrics *Metrics, registryType string, testType string, dura
286287
durationSeconds := duration.Seconds()
287288
switch testType {
288289
case "pull":
289-
metrics.RegistryPullDuration.WithLabelValues(registryType).Observe(durationSeconds)
290+
metrics.RegistryPullDuration.WithLabelValues(registryType,k8sNodeName).Observe(durationSeconds)
290291
case "push":
291-
metrics.RegistryPushDuration.WithLabelValues(registryType).Observe(durationSeconds)
292+
metrics.RegistryPushDuration.WithLabelValues(registryType,k8sNodeName).Observe(durationSeconds)
292293
case "metadata":
293-
metrics.RegistryMetadataDuration.WithLabelValues(registryType).Observe(durationSeconds)
294+
metrics.RegistryMetadataDuration.WithLabelValues(registryType,k8sNodeName).Observe(durationSeconds)
294295
case "authentication":
295-
metrics.RegistryAuthenticationDuration.WithLabelValues(registryType).Observe(durationSeconds)
296+
metrics.RegistryAuthenticationDuration.WithLabelValues(registryType,k8sNodeName).Observe(durationSeconds)
296297
}
297298
}
298299

@@ -587,7 +588,9 @@ func AuthenticationTest(metrics *Metrics, registryMap map[string]RegistryConfig,
587588

588589
func main() {
589590
log.SetOutput(os.Stderr)
590-
591+
if k8sNodeName == "" {
592+
log.Fatalf("FATAL: Missing NODE_NAME environment variable. Metrics must always have node label.")
593+
}
591594
registryMap := PrepareRegistryMap()
592595

593596
reg := prometheus.NewRegistry()

0 commit comments

Comments
 (0)