From 6ecc2fafb52b9a8250b0b11f833e6d70e6385693 Mon Sep 17 00:00:00 2001 From: sujal shah Date: Thu, 30 Jan 2025 18:59:48 +0530 Subject: [PATCH] models_metric: Add IsValidLegacyLabelName. 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 --- model/metric.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/model/metric.go b/model/metric.go index 5766107c..4df3d4ae 100644 --- a/model/metric.go +++ b/model/metric.go @@ -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