Skip to content

Commit

Permalink
Adds support for quoted UTF8 label names and special case label name …
Browse files Browse the repository at this point in the history
…without a value to specify metric name
  • Loading branch information
possibull committed Jan 14, 2025
1 parent b354639 commit 648dcb6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
39 changes: 34 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,16 +833,25 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) {
// Already expanded.
return t, nil
}
metricName := ""
{
var me MetricExpr
// Populate me.LabelFilterss
// Check to see if name is set in the first labelFilter

for _, lfes := range t.labelFilterss {
var lfsNew []LabelFilter
for _, lfe := range lfes {
if lfe.Value == nil {
// Expand lfe.Label into lfsNew.
wa := getWithArgExpr(was, lfe.Label)
if wa == nil {
if lfe.IsPossibleMetricName {
if metricName == "" {
metricName = lfe.Label
continue
}
}
return nil, fmt.Errorf("cannot find WITH template for %q inside %q", lfe.Label, t.AppendString(nil))
}
eNew, err := expandWithExprExt(was, wa, []Expr{})
Expand All @@ -867,7 +876,6 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) {
}
continue
}

// convert lfe to LabelFilter.
se, err := expandWithExpr(was, lfe.Value)
if err != nil {
Expand All @@ -879,6 +887,10 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) {
lfeNew.IsNegative = lfe.IsNegative
lfeNew.IsRegexp = lfe.IsRegexp
lf, err := lfeNew.toLabelFilter()
if lf.isMetricNameFilter() {
metricName = lf.Value
continue
}
if err != nil {
return nil, err
}
Expand All @@ -887,9 +899,25 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) {
lfsNew = removeDuplicateLabelFilters(lfsNew)
me.LabelFilterss = append(me.LabelFilterss, lfsNew)
}
// Prepend metric name to latest
if metricName != "" {
lfesCount := len(t.labelFilterss)
for i := 1; i <= lfesCount; i++ {
lfsLastIndex := len(me.LabelFilterss) - i
var lfsNew []LabelFilter
var lfNew LabelFilter
lfNew.Label = "__name__"
lfNew.Value = metricName
lfNew.IsNegative = false
lfNew.IsRegexp = false
lfsNew = append(lfsNew, lfNew)
lfsNew = append(lfsNew, me.LabelFilterss[lfsLastIndex]...)
me.LabelFilterss[lfsLastIndex] = lfsNew
}
}
t = &me
}
metricName := t.getMetricName()
//metricName := t.getMetricName()
if metricName == "" {
return t, nil
}
Expand Down Expand Up @@ -1353,7 +1381,6 @@ func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) {
end := len(p.lex.Token) - 1
if isStringPrefix(p.lex.Token[end:]) {
newToken := p.lex.Token[1:end]
fmt.Printf("string removed old token: %s new token %s\n", p.lex.Token, newToken)
p.lex.Token = newToken
}
} else {
Expand Down Expand Up @@ -1385,6 +1412,7 @@ func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) {
// - {lf or other="filter"}
//
// It must be substituted by complete label filter during WITH template expand.
lfe.IsPossibleMetricName = true
return &lfe, nil
default:
return nil, fmt.Errorf(`labelFilterExpr: unexpected token %q; want "=", "!=", "=~", "!~", ",", "or", "}"`, p.lex.Token)
Expand All @@ -1411,8 +1439,9 @@ type labelFilterExpr struct {
// Value can be nil if Label contains unexpanded WITH template reference.
Value *StringExpr

IsRegexp bool
IsNegative bool
IsRegexp bool
IsNegative bool
IsPossibleMetricName bool
}

func (lfe *labelFilterExpr) AppendString(dst []byte) []byte {
Expand Down
9 changes: 7 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestParseSuccess(t *testing.T) {
another(`{"foo"="bar"}`, `{foo="bar"}`)
another(`{"3foo"="bar"}`, `{\3foo="bar"}`)
another(`{"'3foo'"="bar"}`, `{\'3foo\'="bar"}`)
another(`{'温度{房间="水电费\xF3"}'="1"}[5m] offset 10m`, `{温度\{房间\=\"水电费ó\"\}="1"}[5m] offset 10m`)
same(`{foo="bar"}[5m]`)
another(`{"foo"="bar"}[5m]`, `{foo="bar"}[5m]`)
same(`{foo="bar"}[5m:]`)
Expand All @@ -54,8 +55,11 @@ func TestParseSuccess(t *testing.T) {
same(`{foo="bar"}[5m] offset 10y`)
same(`{foo="bar"}[5m:3s] offset 10y`)
another(`{foo="bar"}[5m] oFFSEt 10y`, `{foo="bar"}[5m] offset 10y`)
another(`{__name__="metric", a="1"}`, `metric{a="1"}`)
same("METRIC")
same("metric")
another(`{"metric"}`, `metric`)
another(`{a="1",__name__="metric"}`, `metric{a="1"}`)
another("metric{}", "metric")
same("m_e:tri44:_c123")
another("-metric", "0 - metric")
Expand Down Expand Up @@ -138,8 +142,9 @@ func TestParseSuccess(t *testing.T) {
another(`sum(x) by (b\x7Ca)`, `sum(x) by(b\|a)`)

// Duplicate filters
same(`foo{__name__="bar"}`)
same(`foo{a="b",a="c",__name__="aaa",b="d"}`)
//same(`foo{__name__="bar"}`)
//same(`foo{a="b",a="c",__name__="aaa",b="d"}`)
same(`{a="b",a="c",b="d"}`)

// Metric filters ending with comma
another(`m{foo="bar",}`, `m{foo="bar"}`)
Expand Down

0 comments on commit 648dcb6

Please sign in to comment.