do not crash on invalid metric values (#17764)

```
minio[1032735]: panic: label value "\xc0.\xc0." is not valid UTF-8
minio[1032735]: goroutine 1781101 [running]:
minio[1032735]: github.com/prometheus/client_golang/prometheus.MustNewConstMetric(...)
```

log such errors for investigation
This commit is contained in:
Harshavardhana 2023-08-01 00:55:39 -07:00 committed by GitHub
parent 81be718674
commit 2fa561f22e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2953,7 +2953,7 @@ func (c *minioBucketCollector) Collect(out chan<- prometheus.Metric) {
continue continue
} }
for k, v := range metric.Histogram { for k, v := range metric.Histogram {
out <- prometheus.MustNewConstMetric( pmetric, err := prometheus.NewConstMetric(
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName(string(metric.Description.Namespace), prometheus.BuildFQName(string(metric.Description.Namespace),
string(metric.Description.Subsystem), string(metric.Description.Subsystem),
@ -2965,6 +2965,11 @@ func (c *minioBucketCollector) Collect(out chan<- prometheus.Metric) {
prometheus.GaugeValue, prometheus.GaugeValue,
float64(v), float64(v),
append(values, k)...) append(values, k)...)
if err != nil {
logger.LogOnceIf(GlobalContext, fmt.Errorf("unable to validate prometheus metric (%w) %v+%v", err, values, metric.Histogram), "bucket-metrics-histogram")
} else {
out <- pmetric
}
} }
continue continue
} }
@ -2972,7 +2977,7 @@ func (c *minioBucketCollector) Collect(out chan<- prometheus.Metric) {
if metric.Description.Type == counterMetric { if metric.Description.Type == counterMetric {
metricType = prometheus.CounterValue metricType = prometheus.CounterValue
} }
toPost := prometheus.MustNewConstMetric( pmetric, err := prometheus.NewConstMetric(
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName(string(metric.Description.Namespace), prometheus.BuildFQName(string(metric.Description.Namespace),
string(metric.Description.Subsystem), string(metric.Description.Subsystem),
@ -2984,7 +2989,11 @@ func (c *minioBucketCollector) Collect(out chan<- prometheus.Metric) {
metricType, metricType,
metric.Value, metric.Value,
values...) values...)
out <- toPost if err != nil {
logger.LogOnceIf(GlobalContext, fmt.Errorf("unable to validate prometheus metric (%w) %v", err, values), "bucket-metrics")
} else {
out <- pmetric
}
} }
} }
@ -3024,7 +3033,7 @@ func (c *minioClusterCollector) Collect(out chan<- prometheus.Metric) {
continue continue
} }
for k, v := range metric.Histogram { for k, v := range metric.Histogram {
out <- prometheus.MustNewConstMetric( pmetric, err := prometheus.NewConstMetric(
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName(string(metric.Description.Namespace), prometheus.BuildFQName(string(metric.Description.Namespace),
string(metric.Description.Subsystem), string(metric.Description.Subsystem),
@ -3036,6 +3045,11 @@ func (c *minioClusterCollector) Collect(out chan<- prometheus.Metric) {
prometheus.GaugeValue, prometheus.GaugeValue,
float64(v), float64(v),
append(values, k)...) append(values, k)...)
if err != nil {
logger.LogOnceIf(GlobalContext, fmt.Errorf("unable to validate prometheus metric (%w) %v:%v", err, values, metric.Histogram), "cluster-metrics-histogram")
} else {
out <- pmetric
}
} }
continue continue
} }
@ -3043,7 +3057,7 @@ func (c *minioClusterCollector) Collect(out chan<- prometheus.Metric) {
if metric.Description.Type == counterMetric { if metric.Description.Type == counterMetric {
metricType = prometheus.CounterValue metricType = prometheus.CounterValue
} }
toPost := prometheus.MustNewConstMetric( pmetric, err := prometheus.NewConstMetric(
prometheus.NewDesc( prometheus.NewDesc(
prometheus.BuildFQName(string(metric.Description.Namespace), prometheus.BuildFQName(string(metric.Description.Namespace),
string(metric.Description.Subsystem), string(metric.Description.Subsystem),
@ -3055,7 +3069,11 @@ func (c *minioClusterCollector) Collect(out chan<- prometheus.Metric) {
metricType, metricType,
metric.Value, metric.Value,
values...) values...)
out <- toPost if err != nil {
logger.LogOnceIf(GlobalContext, fmt.Errorf("unable to validate prometheus metric (%w) %v", err, values), "cluster-metrics")
} else {
out <- pmetric
}
} }
} }