-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize resource name for prometheus SLIs (#804)
Co-authored-by: Akis Maziotis <[email protected]> Co-authored-by: Mir Shahriar Sabuj <[email protected]>
- Loading branch information
1 parent
b37df9a
commit 31403ad
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
// | ||
|
||
package utils | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var alphaNumericCharacters = regexp.MustCompile(`[^a-z0-9]+`) | ||
|
||
// SanitizeAlphaNumericString converts a string to a valid AlphaNumeric representation, converting all letters to | ||
// lowercase and then removing all invalid characters. | ||
func SanitizeAlphaNumericString(input string) string { | ||
return alphaNumericCharacters.ReplaceAllString(strings.ToLower(input), "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
// | ||
|
||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSanitizeRFC1123String(t *testing.T) { | ||
testCases := []struct { | ||
Input string | ||
Output string | ||
}{ | ||
{ | ||
Input: "aabbcc", | ||
Output: "aabbcc", | ||
}, | ||
{ | ||
Input: "SpinWick-thingy-123", | ||
Output: "spinwickthingy123", | ||
}, | ||
{ | ||
Input: "sup3rR@nD#mS7t))((###-oopp<>../;[", | ||
Output: "sup3rrndms7toopp", | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
assert.Equal(t, testCase.Output, SanitizeAlphaNumericString(testCase.Input)) | ||
} | ||
} |