Skip to content

Commit

Permalink
Adds support for quoted UTF8 label and metric names
Browse files Browse the repository at this point in the history
  • Loading branch information
possibull committed Feb 10, 2025
1 parent 72b650d commit 5783096
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
24 changes: 24 additions & 0 deletions lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,30 @@ func unescapeIdent(s string) string {
}
}

func hasEscapedChars(s string) bool {
i := 0
for i < len(s) {
r, size := utf8.DecodeRuneInString(s[i:])
if i == 0 && !isFirstIdentChar(r) || i > 0 && !isIdentChar(r) {
return true
}
i += size
}
return false
}
func fEscapedCharsAppendQuotedIdent(dst []byte, s string) []byte {
if hasEscapedChars(s) {
dst = utf8.AppendRune(dst, '"')
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
dst = utf8.AppendRune(dst, r)
i += size
}
dst = utf8.AppendRune(dst, '"')
return dst
}
return appendEscapedIdent(dst, s)
}
func appendEscapedIdent(dst []byte, s string) []byte {
i := 0
for i < len(s) {
Expand Down
27 changes: 22 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ type labelFilterExpr struct {
}

func (lfe *labelFilterExpr) AppendString(dst []byte) []byte {
dst = appendEscapedIdent(dst, lfe.Label)
dst = ifEscapedCharsAppendQuotedIdent(dst, lfe.Label)
if lfe.Value == nil {
return dst
}
Expand Down Expand Up @@ -2267,15 +2267,28 @@ type MetricExpr struct {
func appendLabelFilterss(dst []byte, lfss [][]*labelFilterExpr) []byte {
offset := 0
metricName := getMetricNameFromLabelFilterss(lfss)
metricNameHasEscapedChars := hasEscapedChars(metricName)

if metricName != "" {
offset = 1
dst = appendEscapedIdent(dst, metricName)
if !metricNameHasEscapedChars {
dst = appendEscapedIdent(dst, metricName)
} else {
dst = append(dst, '{')
dst = ifEscapedCharsAppendQuotedIdent(dst, metricName)
}
}
if isOnlyMetricNameInLabelFilterss(lfss) {
if metricNameHasEscapedChars {
dst = append(dst, '}')
}
return dst
}

dst = append(dst, '{')
if !metricNameHasEscapedChars {
dst = append(dst, '{')
} else {
dst = append(dst, ',', ' ')
}
for i, lfs := range lfss {
lfs = lfs[offset:]
if len(lfs) == 0 {
Expand Down Expand Up @@ -2335,7 +2348,11 @@ func mustGetMetricName(lfss []*labelFilterExpr) string {
}
lfs := lfss[0]
if lfs.Label != "__name__" || lfs.Value == nil || len(lfs.Value.tokens) != 1 {
return ""
if lfs.IsPossibleMetricName {
return lfs.Label
} else {
return ""
}
}
metricName, err := extractStringValue(lfs.Value.tokens[0])
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions prettifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,16 @@ func TestPrettifySuccess(t *testing.T) {

// Verify that short queries remain single-line
same(`foo`)
another(`{"foo"}`, "foo")
another(`{"3foo"}`, `{"3foo"}`)
another(`{"foo", bar="baz"}`, `foo{bar="baz"}`)
another(`{"foo", "bar"="baz"}`, `foo{bar="baz"}`)
same(`foo{bar="baz"}`)
another(`foo{"3bar"="baz"}`, `foo{"3bar"="baz"}`)
another(`foo{"bar3"="baz"}`, `foo{bar3="baz"}`)
same(`"metr\"ic"`)
same(`'metr"ic'`)
same(`{"3foo", bar="baz"}`)
same(`foo{bar="baz",x="y" or q="w",r="t"}`)
same(`foo{bar="baz"} + rate(x{y="x"}[5m] offset 1h)`)

Expand Down

0 comments on commit 5783096

Please sign in to comment.