From fe0fbee21125d4f6baf97514e8c2cf2796e6df00 Mon Sep 17 00:00:00 2001 From: foehammer127 Date: Wed, 28 Feb 2024 17:21:23 -0600 Subject: [PATCH] feat(prometheus/testutil/promlint/validations): refine lintMetricTypeInName Change the lintMetricTypeInName linter inside promlint to only trigger an error when the metric name matches the type of the metric. Signed-off-by: Lorenzo Good --- prometheus/testutil/promlint/promlint_test.go | 1 - .../validations/generic_name_validations.go | 20 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/prometheus/testutil/promlint/promlint_test.go b/prometheus/testutil/promlint/promlint_test.go index 3617ed84c..7a0ba46bb 100644 --- a/prometheus/testutil/promlint/promlint_test.go +++ b/prometheus/testutil/promlint/promlint_test.go @@ -668,7 +668,6 @@ func TestLintMetricTypeInName(t *testing.T) { twoProbTest, genTest("instance_memory_limit_bytes_gauge", "gauge", "gauge"), genTest("request_duration_seconds_summary", "summary", "summary"), - genTest("request_duration_seconds_summary", "histogram", "summary"), genTest("request_duration_seconds_histogram", "histogram", "histogram"), genTest("request_duration_seconds_HISTOGRAM", "histogram", "histogram"), diff --git a/prometheus/testutil/promlint/validations/generic_name_validations.go b/prometheus/testutil/promlint/validations/generic_name_validations.go index bc8dbd1e1..2677ab2d2 100644 --- a/prometheus/testutil/promlint/validations/generic_name_validations.go +++ b/prometheus/testutil/promlint/validations/generic_name_validations.go @@ -44,21 +44,21 @@ func LintMetricUnits(mf *dto.MetricFamily) []error { return problems } -// LintMetricTypeInName detects when metric types are included in the metric name. +// LintMetricTypeInName detects when the metric type is included in the metric name. func LintMetricTypeInName(mf *dto.MetricFamily) []error { + if *mf.Type == dto.MetricType_UNTYPED { + return nil + } + var problems []error - n := strings.ToLower(mf.GetName()) - for i, t := range dto.MetricType_name { - if i == int32(dto.MetricType_UNTYPED) { - continue - } + n := strings.ToLower(mf.GetName()) + typename := strings.ToLower(mf.Type.String()) - typename := strings.ToLower(t) - if strings.Contains(n, "_"+typename+"_") || strings.HasSuffix(n, "_"+typename) { - problems = append(problems, fmt.Errorf(`metric name should not include type '%s'`, typename)) - } + if strings.Contains(n, "_"+typename+"_") || strings.HasSuffix(n, "_"+typename) { + problems = append(problems, fmt.Errorf(`metric name should not include type '%s'`, typename)) } + return problems }