Sanitize ethtool metric name keys

Apply the same metric name sanitization to the keys as to the metric
names. This avoids conflicting help strings in the metric registry.

Fixes: https://github.com/prometheus/node_exporter/issues/2893

Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
Ben Kochie 2024-02-29 22:00:45 +01:00 committed by Johannes 'fish' Ziemke
parent 274cd51ce7
commit b3bbd1f52c
1 changed files with 6 additions and 5 deletions

View File

@ -445,18 +445,19 @@ func (c *ethtoolCollector) Update(ch chan<- prometheus.Metric) error {
// Sanitizing the metric names can lead to duplicate metric names. Therefore check for clashes beforehand. // Sanitizing the metric names can lead to duplicate metric names. Therefore check for clashes beforehand.
metricFQNames := make(map[string]string) metricFQNames := make(map[string]string)
for metric := range stats { for metric := range stats {
if !c.metricsPattern.MatchString(metric) { metricName := SanitizeMetricName(metric)
if !c.metricsPattern.MatchString(metricName) {
continue continue
} }
metricFQName := buildEthtoolFQName(metric) metricFQName := buildEthtoolFQName(metricName)
existingMetric, exists := metricFQNames[metricFQName] existingMetric, exists := metricFQNames[metricFQName]
if exists { if exists {
level.Debug(c.logger).Log("msg", "dropping duplicate metric name", "device", device, level.Debug(c.logger).Log("msg", "dropping duplicate metric name", "device", device,
"metricFQName", metricFQName, "metric1", existingMetric, "metric2", metric) "metricFQName", metricFQName, "metric1", existingMetric, "metric2", metricName)
// Keep the metric as "deleted" in the dict in case there are 3 duplicates. // Keep the metricName as "deleted" in the dict in case there are 3 duplicates.
metricFQNames[metricFQName] = "" metricFQNames[metricFQName] = ""
} else { } else {
metricFQNames[metricFQName] = metric metricFQNames[metricFQName] = metricName
} }
} }