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 <sujalshah28092004@gmail.com>
This commit is contained in:
sujal shah 2025-01-16 04:26:54 +05:30
parent 906f6a33b6
commit 7b699d279b

View file

@ -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