Skip to content

Commit

Permalink
Duplicate metric names are now allowed
Browse files Browse the repository at this point in the history
Refactored code checking for quoted strings into isQuoteString
Added some additional tests
  • Loading branch information
possibull committed Jan 22, 2025
1 parent ac3fe8f commit afd03f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
22 changes: 14 additions & 8 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,10 @@ func expandWithExpr(was []*withArgExpr, e Expr) (Expr, error) {
metricName = lfe.Label
continue
} else {
return nil, fmt.Errorf("parse error: metric name must not be set twice: %q or %q", metricName, lfe.Label)
if metricName != lfe.Label {
return nil, fmt.Errorf("parse error: metric name must not be set twice: %q or %q", metricName, lfe.Label)
}
continue
}
}
return nil, fmt.Errorf("cannot find WITH template for %q inside %q", lfe.Label, t.AppendString(nil))
Expand Down Expand Up @@ -1381,17 +1384,20 @@ func (p *parser) parseLabelFilters(mf *labelFilterExpr) ([]*labelFilterExpr, err
}
}

func isQuotedString(s string) bool {
if isStringPrefix(s) && isStringPrefix(s[len(s)-1:]) {
return true
}
return false
}

func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) {
var isPossibleMetricName bool

// Strip quotes if they exist
if isStringPrefix(p.lex.Token) {
end := len(p.lex.Token) - 1
if isStringPrefix(p.lex.Token[end:]) {
newToken := p.lex.Token[1:end]
p.lex.Token = newToken
isPossibleMetricName = true
}
if isQuotedString(p.lex.Token) {
p.lex.Token = p.lex.Token[1 : len(p.lex.Token)-1]
isPossibleMetricName = true
} else {
if !isIdentPrefix(p.lex.Token) {
return nil, fmt.Errorf(`labelFilterExpr: unexpected token %q; want "ident"`, p.lex.Token)
Expand Down
3 changes: 2 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func TestParseSuccess(t *testing.T) {
another(`{"metric", "a"="1"}`, `metric{a="1"}`)
same("METRIC")
same("metric")
another(`metric{"metric"}`, "metric")
another(`metric{__name__="metric"}`, "metric")
another(`{"metric"}`, `metric`)
another(`{a="1",__name__="metric"}`, `metric{a="1"}`)
another("metric{}", "metric")
Expand Down Expand Up @@ -666,7 +668,6 @@ func TestParseError(t *testing.T) {
// No longer invalid with Prometheus 3.0 quoted label names
//f(`foo{"foo"="bar"}`)
// Test if two metric names are set
f(`foo{"foo"}`)
f(`{"foo", "a"="1", "bar"}`)
f(`{"foo", __name__="bar"}`)
f(`foo{$`)
Expand Down

0 comments on commit afd03f8

Please sign in to comment.