From 7b699d279b6b266a0d977c914b22907a42a636b0 Mon Sep 17 00:00:00 2001 From: sujal shah Date: Thu, 16 Jan 2025 04:26:54 +0530 Subject: [PATCH] promql_parser: Fix invalid format return by `/format_query`. Added `IsValidLegacy` to ensure UTF-8 labels in MatchingLabels and Include are properly quoted. Updated getMatchingStr to use quoted labels for correct formatting in returned expressions. Fixes #15243. Signed-off-by: sujal shah --- promql/parser/printer.go | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/promql/parser/printer.go b/promql/parser/printer.go index afe755e7dd..02dde452ee 100644 --- a/promql/parser/printer.go +++ b/promql/parser/printer.go @@ -100,7 +100,7 @@ func joinLabels(ss []string) string { if i > 0 { b.WriteString(", ") } - if !model.IsValidLegacyMetricName(string(model.LabelValue(s))) { + if !model.LabelName(s).IsValidLegacy() { b.Write(strconv.AppendQuote(b.AvailableBuffer(), s)) } else { b.WriteString(s) @@ -128,19 +128,24 @@ func (node *BinaryExpr) ShortString() string { func (node *BinaryExpr) getMatchingStr() string { matching := "" vm := node.VectorMatching - if vm != nil && (len(vm.MatchingLabels) > 0 || vm.On) { - vmTag := "ignoring" - if vm.On { - vmTag = "on" - } - matching = fmt.Sprintf(" %s (%s)", vmTag, strings.Join(vm.MatchingLabels, ", ")) - - if vm.Card == CardManyToOne || vm.Card == CardOneToMany { - vmCard := "right" - if vm.Card == CardManyToOne { - vmCard = "left" + if vm != nil { + if len(vm.MatchingLabels) > 0 || vm.On { + vmTag := "ignoring" + if vm.On { + vmTag = "on" + } + // Quote only necessary labels. + matching = fmt.Sprintf(" %s (%s)", vmTag, joinLabels(vm.MatchingLabels)) + + if vm.Card == CardManyToOne || vm.Card == CardOneToMany { + vmCard := "right" + if vm.Card == CardManyToOne { + vmCard = "left" + } + // Quote only necessary Include labels. + matching += fmt.Sprintf(" group_%s (%s)", vmCard, joinLabels(vm.Include)) + fmt.Printf("\n") } - matching += fmt.Sprintf(" group_%s (%s)", vmCard, strings.Join(vm.Include, ", ")) } } return matching