Skip to content

Commit

Permalink
models_metric: Add IsValidLegacyLabelName.
Browse files Browse the repository at this point in the history
This commit introduces two new functions, `isValidLegacyLabelRune`
and `IsValidLegacyLabelName`, to validate legacy label names and
their runes. The validation logic is consolidated by reusing the
`IsValidLabelCharacter` function.

Signed-off-by: sujal shah <[email protected]>
  • Loading branch information
sujalshah-bit committed Jan 30, 2025
1 parent 7684929 commit 6ecc2fa
Showing 1 changed file with 26 additions and 0 deletions.
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

0 comments on commit 6ecc2fa

Please sign in to comment.