Skip to content

Commit

Permalink
Merge pull request #135 from qonto/fix-crash-on-unsupported-labels
Browse files Browse the repository at this point in the history
Avoid crash when the RDS instance contains tags with unsupported label characters
  • Loading branch information
vmercierfr authored Mar 14, 2024
2 parents 0556797 + 4742586 commit 4d75fb4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions internal/app/exporter/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func getUniqTypeAndIdentifiers(instances map[string]rds.RdsInstanceMetrics) ([]s

func ClearPrometheusLabel(str string) string {
// Prometheus metric names may contain ASCII letters, digits, underscores, and colons.
// It must match the regex [a-zA-Z_:][a-zA-Z0-9_:]*.
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
InvalidFirstLetterCharacters := regexp.MustCompile(`[^a-zA-Z_:]+`)
InvalidCharacters := regexp.MustCompile(`[^a-zA-Z0-9_:]+`)
InvalidFirstLetterCharacters := regexp.MustCompile(`[^a-zA-Z]+`)
InvalidCharacters := regexp.MustCompile(`[^a-zA-Z0-9_]+`)

return InvalidFirstLetterCharacters.ReplaceAllString(string(str[0]), "_") + InvalidCharacters.ReplaceAllString(str[1:], "_")
return InvalidFirstLetterCharacters.ReplaceAllString(string(str[0]), "") + InvalidCharacters.ReplaceAllString(str[1:], "_")
}
10 changes: 5 additions & 5 deletions internal/app/exporter/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func TestClearPrometheusLabel(t *testing.T) {
label string
want string
}{
{"tag_services.k8s.aws/controller-version", "tag_services_k8s_aws_controller_version"},
{"Unreplaced_LabeL:1", "Unreplaced_LabeL:1"},
{"1stInvalidCharacter", "_stInvalidCharacter"},
{"_IsValidAValidLabel", "_IsValidAValidLabel"},
{":IsValidAValidLabel", ":IsValidAValidLabel"},
{"aws:cloudformation:logical_id", "aws_cloudformation_logical_id"},
{"services.k8s.aws/controller-version", "services_k8s_aws_controller_version"},
{"1InvalidFirstCharacter", "InvalidFirstCharacter"},
{":InvalidFirstCharacter", "InvalidFirstCharacter"},
{"_InvalidFirstCharacter", "InvalidFirstCharacter"},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 4d75fb4

Please sign in to comment.