From 767a0218dfc546080ef03703a42364218ce962a0 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Fri, 13 Jul 2018 14:14:39 +0200 Subject: [PATCH] Add more label checksn during gathering Including check for an invalid "quantile" label in summaries. Also, improve error messages. Signed-off-by: beorn7 --- prometheus/examples_test.go | 2 +- prometheus/registry.go | 20 +++++++++++++++++--- prometheus/registry_test.go | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/prometheus/examples_test.go b/prometheus/examples_test.go index 9808d5c..07b3929 100644 --- a/prometheus/examples_test.go +++ b/prometheus/examples_test.go @@ -740,7 +740,7 @@ temperature_kelvin 4.5 // temperature_kelvin{location="outside"} 273.14 // temperature_kelvin{location="somewhere else"} 4.5 // ---------- - // collected metric temperature_kelvin label: gauge: was collected before with the same name and label values + // collected metric "temperature_kelvin" { label: gauge: } was collected before with the same name and label values // # HELP humidity_percent Humidity in %. // # TYPE humidity_percent gauge // humidity_percent{location="inside"} 33.2 diff --git a/prometheus/registry.go b/prometheus/registry.go index 5c5cdfe..4fa2567 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -783,14 +783,28 @@ func checkMetricConsistency( metricFamily.GetType() == dto.MetricType_HISTOGRAM && dtoMetric.Histogram == nil || metricFamily.GetType() == dto.MetricType_UNTYPED && dtoMetric.Untyped == nil { return fmt.Errorf( - "collected metric %s %s is not a %s", + "collected metric %q { %s} is not a %s", metricFamily.GetName(), dtoMetric, metricFamily.GetType(), ) } for _, labelPair := range dtoMetric.GetLabel() { + if !checkLabelName(labelPair.GetName()) { + return fmt.Errorf( + "collected metric %q { %s} has a label with an invalid name: %s", + metricFamily.GetName(), dtoMetric, labelPair.GetName(), + ) + } + if dtoMetric.Summary != nil && labelPair.GetName() == quantileLabel { + return fmt.Errorf( + "collected metric %q { %s} must not have an explicit %q label", + metricFamily.GetName(), dtoMetric, quantileLabel, + ) + } if !utf8.ValidString(labelPair.GetValue()) { - return fmt.Errorf("collected metric's label %s is not utf8: %#v", labelPair.GetName(), labelPair.GetValue()) + return fmt.Errorf( + "collected metric %q { %s} has a label named %q whose value is not utf8: %#v", + metricFamily.GetName(), dtoMetric, labelPair.GetName(), labelPair.GetValue()) } } @@ -809,7 +823,7 @@ func checkMetricConsistency( } if _, exists := metricHashes[h]; exists { return fmt.Errorf( - "collected metric %s %s was collected before with the same name and label values", + "collected metric %q { %s} was collected before with the same name and label values", metricFamily.GetName(), dtoMetric, ) } diff --git a/prometheus/registry_test.go b/prometheus/registry_test.go index 10d4314..45b6dc9 100644 --- a/prometheus/registry_test.go +++ b/prometheus/registry_test.go @@ -249,7 +249,7 @@ metric: < expectedMetricFamilyInvalidLabelValueAsText := []byte(`An error has occurred during metrics gathering: -collected metric's label constname is not utf8: "\xff" +collected metric "name" { label: label: counter: } has a label named "constname" whose value is not utf8: "\xff" `) type output struct {