Skip to content

Commit 4350e47

Browse files
authored
fix: Add stricter validation for metrics from profiles fields (#4522)
Since metrics from profiles is using Prometheus remote_write 1.0, the legacy format `^[a-zA-Z_][a-zA-Z0-9_]*$` for metric and label names must be enforced.
1 parent 609a861 commit 4350e47

File tree

5 files changed

+505
-12
lines changed

5 files changed

+505
-12
lines changed

pkg/model/recording_rule.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,26 @@ func newRecordingRuleWithBuilder(rule *settingsv1.RecordingRule, sb *labels.Scra
6464
}
6565
}
6666

67+
// validate group_by label names for Prometheus compatibility
68+
for _, labelName := range rule.GroupBy {
69+
name := prometheusmodel.LabelName(labelName)
70+
if !prometheusmodel.LegacyValidation.IsValidLabelName(string(name)) {
71+
return nil, fmt.Errorf("group_by label %q must match %s", labelName, prometheusmodel.LabelNameRE.String())
72+
}
73+
}
74+
6775
sb.Reset()
68-
// ensure no __name__ or profiles_rule_id labels already exist
6976
for _, lbl := range rule.ExternalLabels {
77+
// ensure no __name__ or profiles_rule_id labels already exist
7078
if uniqueLabels[lbl.Name] {
7179
// skip
7280
continue
7381
}
82+
// validate external label names for Prometheus compatibility
83+
name := prometheusmodel.LabelName(lbl.Name)
84+
if !prometheusmodel.LegacyValidation.IsValidLabelName(string(name)) {
85+
return nil, fmt.Errorf("external_labels name %q must match %s", lbl.Name, prometheusmodel.LabelNameRE.String())
86+
}
7487
sb.Add(lbl.Name, lbl.Value)
7588
}
7689

@@ -102,7 +115,7 @@ func parseMatchers(matchers []string) ([]*labels.Matcher, error) {
102115
}
103116

104117
func ValidateMetricName(name string) error {
105-
if !prometheusmodel.UTF8Validation.IsValidMetricName(name) {
118+
if !prometheusmodel.LegacyValidation.IsValidMetricName(name) {
106119
return fmt.Errorf("invalid metric name: %s", name)
107120
}
108121
if !strings.HasPrefix(name, metricNamePrefix) {

0 commit comments

Comments
 (0)