Skip to content

Commit 79f48bd

Browse files
committed
parser.go: move the check for empty label filters into mustGetMetricName() function
This simplifies the getMetricNameFromLabelFilterss() a bit after 07bd0d9
1 parent 07bd0d9 commit 79f48bd

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

parser.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ func (p *parser) parseLabelFilterExpr() (*labelFilterExpr, error) {
13741374
// - {lf,other="filter"}
13751375
// - {lf or other="filter"}
13761376
//
1377-
// It which must be substituted by complete label filter during WITH template expand.
1377+
// It must be substituted by complete label filter during WITH template expand.
13781378
return &lfe, nil
13791379
default:
13801380
return nil, fmt.Errorf(`labelFilterExpr: unexpected token %q; want "=", "!=", "=~", "!~", ",", "or", "}"`, p.lex.Token)
@@ -2257,28 +2257,31 @@ func isOnlyMetricNameInLabelFilterss(lfss [][]*labelFilterExpr) bool {
22572257
}
22582258

22592259
func getMetricNameFromLabelFilterss(lfss [][]*labelFilterExpr) string {
2260-
if len(lfss) == 0 || len(lfss[0]) == 0 {
2260+
if len(lfss) == 0 {
2261+
return ""
2262+
}
2263+
metricName := mustGetMetricName(lfss[0])
2264+
if metricName == "" {
22612265
return ""
22622266
}
2263-
metricName := lfss[0][0].mustGetMetricName()
22642267
for _, lfs := range lfss[1:] {
2265-
if len(lfs) == 0 {
2266-
return ""
2267-
}
2268-
metricNameLocal := lfs[0].mustGetMetricName()
2268+
metricNameLocal := mustGetMetricName(lfs)
22692269
if metricNameLocal != metricName {
22702270
return ""
22712271
}
22722272
}
22732273
return metricName
22742274
}
22752275

2276-
func (lfe *labelFilterExpr) mustGetMetricName() string {
2277-
if lfe.Label != "__name__" || lfe.Value == nil || len(lfe.Value.tokens) != 1 {
2276+
func mustGetMetricName(lfss []*labelFilterExpr) string {
2277+
if len(lfss) == 0 {
2278+
return ""
2279+
}
2280+
lfs := lfss[0]
2281+
if lfs.Label != "__name__" || lfs.Value == nil || len(lfs.Value.tokens) != 1 {
22782282
return ""
22792283
}
2280-
token := lfe.Value.tokens[0]
2281-
metricName, err := extractStringValue(token)
2284+
metricName, err := extractStringValue(lfs.Value.tokens[0])
22822285
if err != nil {
22832286
panic(fmt.Errorf("BUG: cannot obtain metric name: %w", err))
22842287
}

0 commit comments

Comments
 (0)