Skip to content

Commit fbf9768

Browse files
sd2kclaude
andcommitted
fix: handle braces in matcher strings to prevent double-wrapping
parseMatcherStrings now checks if the input already has braces before wrapping, so both 'severity="critical"' and '{severity="critical"}' work correctly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9a29345 commit fbf9768

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

tools/alerting_manage_rules_types.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tools
33
import (
44
"encoding/json"
55
"fmt"
6+
"strings"
67

78
"github.com/grafana/grafana-openapi-client-go/models"
89
"github.com/prometheus/prometheus/model/labels"
@@ -339,16 +340,21 @@ func (p DeleteAlertRuleParams) validate() error {
339340
return nil
340341
}
341342

342-
// parseMatcherStrings parses Prometheus-style matcher strings (e.g. "severity=critical")
343-
// into LabelMatcher structs. Each string should be a single matcher like "name=value",
344-
// "name!=value", "name=~regex", or "name!~regex".
343+
// parseMatcherStrings parses Prometheus-style matcher strings (e.g. 'severity="critical"')
344+
// into label matchers. Each string should be a single matcher like 'name="value"',
345+
// 'name!="value"', 'name=~"regex"', or 'name!~"regex"'. Strings may optionally be
346+
// wrapped in braces (e.g. '{severity="critical"}') — braces are stripped if present.
345347
func parseMatcherStrings(strs []string) ([]*labels.Matcher, error) {
346348
if len(strs) == 0 {
347349
return nil, nil
348350
}
349351
var result []*labels.Matcher
350352
for _, s := range strs {
351-
parsed, err := parser.ParseMetricSelector("{" + s + "}")
353+
s = strings.TrimSpace(s)
354+
if !strings.HasPrefix(s, "{") {
355+
s = "{" + s + "}"
356+
}
357+
parsed, err := parser.ParseMetricSelector(s)
352358
if err != nil {
353359
return nil, fmt.Errorf("invalid matcher %q: %w", s, err)
354360
}

0 commit comments

Comments
 (0)