Skip to content

Commit

Permalink
Simplify the labeler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-at-luther committed Jan 31, 2024
1 parent 610fa45 commit 7a9be90
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
15 changes: 3 additions & 12 deletions lisp/x/profiler/funlabeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ const ELPSDocLabel = `@trace{([^}]+)}`

var elpsDocLabelRegExp = regexp.MustCompile(ELPSDocLabel)
var sanitizeRegExp = regexp.MustCompile(`[\W_]+`)
var validLabelRegExp = regexp.MustCompile(`[a-zA-Z_][\w_]*`)

//var validLabelRegExp = regexp.MustCompile(`[a-zA-Z_][\w_]*`)
var validLabelRegExp = regexp.MustCompile(`[a-zA-Z][\w_]*`)

func sanitizeLabel(userLabel string) string {
userLabel = strings.TrimSpace(userLabel)
Expand All @@ -52,24 +54,13 @@ func sanitizeLabel(userLabel string) string {
// Replace spaces with underscores
userLabel = sanitizeRegExp.ReplaceAllString(userLabel, "_")

// Eliminate duplicate underscores
parts := strings.Split(userLabel, "_")
var nonEmptyParts []string
for _, part := range parts {
if part != "" {
nonEmptyParts = append(nonEmptyParts, part)
}
}
userLabel = strings.Join(nonEmptyParts, "_")

// Find the first valid label match
matches := validLabelRegExp.FindStringSubmatch(userLabel)
if len(matches) > 0 {
return matches[0]
}

return ""

}

func elpsDocFunLabeler(runtime *lisp.Runtime, fun *lisp.LVal) string {
Expand Down
7 changes: 6 additions & 1 deletion lisp/x/profiler/funlabeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSanitizeLabel(t *testing.T) {
{
name: "contains special characters",
label: "Label@#$%^&",
expected: "Label",
expected: "Label_",
},
{
name: "starts with a digit",
Expand All @@ -42,6 +42,11 @@ func TestSanitizeLabel(t *testing.T) {
label: "_Label",
expected: "Label",
},
{
name: "label with underscores",
label: "label__with__underscores",
expected: "label_with_underscores",
},
{
name: "starts with a special character",
label: "@Label",
Expand Down

0 comments on commit 7a9be90

Please sign in to comment.