-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add profiler span labeler #62
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,85 @@ | ||
package profiler | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"unicode" | ||
|
||
"github.com/luthersystems/elps/lisp" | ||
"github.com/luthersystems/elps/lisp/lisplib/libhelp" | ||
) | ||
|
||
// FunLabeler provides an alternative name for a function label in the trace. | ||
type FunLabeler func(runtime *lisp.Runtime, fun *lisp.LVal) string | ||
|
||
// WithELPSDocLabeler labels spans using elps doc magic strings. | ||
func WithELPSDocLabeler() Option { | ||
return WithFunLabeler(elpsDocFunLabeler) | ||
} | ||
|
||
// WithFunLabeler sets the labeler for tracing spans. | ||
func WithFunLabeler(funLabeler FunLabeler) Option { | ||
return func(p *profiler) { | ||
p.funLabeler = funLabeler | ||
} | ||
} | ||
|
||
// defaultFunName constructs a pretty canonical name using the function name. | ||
func defaultFunName(runtime *lisp.Runtime, fun *lisp.LVal) string { | ||
if fun.Type != lisp.LFun { | ||
return "" | ||
} | ||
funData := fun.FunData() | ||
if funData == nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("%s:%s", funData.Package, getFunNameFromFID(runtime, funData.FID)) | ||
} | ||
|
||
// ELPSDocLabel is a magic string used to extract function labels. | ||
const ELPSDocLabel = `@trace{([^}]+)}` | ||
|
||
var elpsDocLabelRegExp = regexp.MustCompile(ELPSDocLabel) | ||
|
||
var sanitizeRegExp = regexp.MustCompile(`[\W_]+`) | ||
|
||
func sanitizeLabel(userLabel string) string { | ||
userLabel = strings.TrimSpace(userLabel) | ||
if userLabel == "" { | ||
return "" | ||
} | ||
|
||
userLabel = sanitizeRegExp.ReplaceAllString(userLabel, "_") | ||
// Ensure the label doesn't start with a digit or special character | ||
if len(userLabel) > 0 && !unicode.IsLetter(rune(userLabel[0])) { | ||
userLabel = "label_" + userLabel | ||
} | ||
|
||
// Eliminate duplicate underscores | ||
parts := strings.Split(userLabel, "_") | ||
var nonEmptyParts []string | ||
for _, part := range parts { | ||
if part != "" { | ||
nonEmptyParts = append(nonEmptyParts, part) | ||
} | ||
} | ||
return strings.Join(nonEmptyParts, "_") | ||
} | ||
|
||
func elpsDocFunLabeler(runtime *lisp.Runtime, fun *lisp.LVal) string { | ||
docStr := libhelp.FunDocstring(fun) | ||
if docStr == "" { | ||
return "" | ||
} | ||
matches := elpsDocLabelRegExp.FindAllStringSubmatch(docStr, -1) | ||
label := "" | ||
for _, match := range matches { | ||
if len(match) > 1 { | ||
label = match[1] | ||
break | ||
} | ||
} | ||
|
||
return sanitizeLabel(label) | ||
} |
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,68 @@ | ||
package profiler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSanitizeLabel(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
label string | ||
expected string | ||
}{ | ||
{ | ||
name: "normal case", | ||
label: "NormalLabel", | ||
expected: "NormalLabel", | ||
}, | ||
{ | ||
name: "contains spaces", | ||
label: "Label With Spaces", | ||
expected: "Label_With_Spaces", | ||
}, | ||
{ | ||
name: "contains special characters", | ||
label: "Label@#$%^&", | ||
expected: "Label", | ||
}, | ||
{ | ||
name: "starts with a digit", | ||
label: "123Label", | ||
expected: "label_123Label", | ||
}, | ||
{ | ||
name: "empty string", | ||
label: "", | ||
expected: "", | ||
}, | ||
{ | ||
name: "starts with a digit", | ||
label: "123Label", | ||
expected: "label_123Label", | ||
}, | ||
{ | ||
name: "starts with an underscore", | ||
label: "_Label", | ||
expected: "label_Label", | ||
}, | ||
{ | ||
name: "starts with a special character", | ||
label: "@Label", | ||
expected: "label_Label", | ||
}, | ||
{ | ||
name: "starts with a space", | ||
label: " Label", | ||
expected: "Label", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual := sanitizeLabel(tc.label) | ||
assert.Equal(t, tc.expected, actual, "sanitizeLabel(%s)", tc.label) | ||
}) | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
(debug-print x)) | ||
|
||
(defun add-it (x y) | ||
"@trace" | ||
"@trace{ Add-It }" | ||
(+ x y)) | ||
|
||
(defun recurse-it (x) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This prefixing behavior seems a bit non-intuitive. Would it make sense to just drop leading junk we don't want to allow or reject the label entirely and fall back to the function name?