From 7c8bbd6bcb53742ec33fd1533dc0d5469731b3d1 Mon Sep 17 00:00:00 2001 From: Max Neverov <1296281+mneverov@users.noreply.github.com> Date: Wed, 12 Aug 2020 23:37:31 +0200 Subject: [PATCH] Add fmt rule check: labels with the name __name__ are invalid (#7787) Signed-off-by: Max Neverov --- pkg/rulefmt/rulefmt.go | 2 +- pkg/rulefmt/rulefmt_test.go | 4 ++++ pkg/rulefmt/testdata/invalid_label_name.bad.yaml | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 pkg/rulefmt/testdata/invalid_label_name.bad.yaml diff --git a/pkg/rulefmt/rulefmt.go b/pkg/rulefmt/rulefmt.go index 7a2179e7d..8267bc90e 100644 --- a/pkg/rulefmt/rulefmt.go +++ b/pkg/rulefmt/rulefmt.go @@ -186,7 +186,7 @@ func (r *RuleNode) Validate() (nodes []WrappedError) { } for k, v := range r.Labels { - if !model.LabelName(k).IsValid() { + if !model.LabelName(k).IsValid() || k == model.MetricNameLabel { nodes = append(nodes, WrappedError{ err: errors.Errorf("invalid label name: %s", k), }) diff --git a/pkg/rulefmt/rulefmt_test.go b/pkg/rulefmt/rulefmt_test.go index 4101b61b9..92967cf7d 100644 --- a/pkg/rulefmt/rulefmt_test.go +++ b/pkg/rulefmt/rulefmt_test.go @@ -71,6 +71,10 @@ func TestParseFileFailure(t *testing.T) { filename: "bad_field.bad.yaml", errMsg: "field annotation not found", }, + { + filename: "invalid_label_name.bad.yaml", + errMsg: "invalid label name", + }, } for _, c := range table { diff --git a/pkg/rulefmt/testdata/invalid_label_name.bad.yaml b/pkg/rulefmt/testdata/invalid_label_name.bad.yaml new file mode 100644 index 000000000..72f69faa9 --- /dev/null +++ b/pkg/rulefmt/testdata/invalid_label_name.bad.yaml @@ -0,0 +1,7 @@ +groups: +- name: yolo + rules: + - record: hola + expr: 1 + labels: + __name__: anything