Skip to content

Commit

Permalink
parser.go: move the check for empty label filters into mustGetMetricN…
Browse files Browse the repository at this point in the history
…ame() function

This simplifies the getMetricNameFromLabelFilterss() a bit after 07bd0d9
  • Loading branch information
valyala committed Aug 15, 2024
1 parent 07bd0d9 commit 79f48bd
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) {
// - {lf,other="filter"}
// - {lf or other="filter"}
//
// It which must be substituted by complete label filter during WITH template expand.
// It must be substituted by complete label filter during WITH template expand.
return &lfe, nil
default:
return nil, fmt.Errorf(`labelFilterExpr: unexpected token %q; want "=", "!=", "=~", "!~", ",", "or", "}"`, p.lex.Token)
Expand Down Expand Up @@ -2257,28 +2257,31 @@ func isOnlyMetricNameInLabelFilterss(lfss [][]*labelFilterExpr) bool {
}

func getMetricNameFromLabelFilterss(lfss [][]*labelFilterExpr) string {
if len(lfss) == 0 || len(lfss[0]) == 0 {
if len(lfss) == 0 {
return ""
}
metricName := mustGetMetricName(lfss[0])
if metricName == "" {
return ""
}
metricName := lfss[0][0].mustGetMetricName()
for _, lfs := range lfss[1:] {
if len(lfs) == 0 {
return ""
}
metricNameLocal := lfs[0].mustGetMetricName()
metricNameLocal := mustGetMetricName(lfs)
if metricNameLocal != metricName {
return ""
}
}
return metricName
}

func (lfe *labelFilterExpr) mustGetMetricName() string {
if lfe.Label != "__name__" || lfe.Value == nil || len(lfe.Value.tokens) != 1 {
func mustGetMetricName(lfss []*labelFilterExpr) string {
if len(lfss) == 0 {
return ""
}
lfs := lfss[0]
if lfs.Label != "__name__" || lfs.Value == nil || len(lfs.Value.tokens) != 1 {
return ""
}
token := lfe.Value.tokens[0]
metricName, err := extractStringValue(token)
metricName, err := extractStringValue(lfs.Value.tokens[0])
if err != nil {
panic(fmt.Errorf("BUG: cannot obtain metric name: %w", err))
}
Expand Down

0 comments on commit 79f48bd

Please sign in to comment.