Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models_metric: Add IsValidLegacyLabelName. #753

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions model/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ func IsValidLegacyMetricName(n string) bool {
return true
}

// IsValidLabelCharacter checks if a rune is valid based on its position in the string.
func IsValidLabelCharacter(c rune, i int) bool {
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '_' ||
(i > 0 && c >= '0' && c <= '9')
}

// IsValidLegacyLabelName checks if a label name follows the legacy validation rules.
func IsValidLegacyLabelName(n string) bool {
if len(n) == 0 {
return false
}
for i, b := range n {
if !isValidLegacyLabelRune(b, i) {
return false
}
}
return true
}

// IsValidLegacyLabelRune checks if a rune is valid for a label name.
func isValidLegacyLabelRune(b rune, i int) bool {
return IsValidLabelCharacter(b, i)
}

// EscapeMetricFamily escapes the given metric names and labels with the given
// escaping scheme. Returns a new object that uses the same pointers to fields
// when possible and creates new escaped versions so as not to mutate the
Expand Down
Loading