-
Notifications
You must be signed in to change notification settings - Fork 244
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
Fix incorrect metrics when serving static files #1255
base: development
Are you sure you want to change the base?
Conversation
We need to be careful here. If we put file path for each file instead of a pattern while calculating the http related metrics, it will become very very large. |
|
||
if path == "/" || strings.HasPrefix(path, "/static") || staticFileRegex.MatchString(r.URL.Path) { | ||
path = r.URL.Path | ||
} | ||
|
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.
Please don't use regexp for this.
You can use filepath.Ext
Something like this could work
if path == "/" || strings.HasPrefix(path, "/static") || staticFileRegex.MatchString(r.URL.Path) { | |
path = r.URL.Path | |
} | |
switch strings.ToLower(filepath.Ext(r.URL.Path)) { | |
case "css", "js", "png", "jpg","jpeg","gif","ico","svg","txt","html","json","woff","woff2","ttf","eot","pdf": | |
path = r.URL.Path | |
} | |
if path == "/" || strings.HasPrefix(path, "/static") { | |
path = r.URL.Path | |
} |
@@ -21,11 +22,19 @@ type metrics interface { | |||
func Metrics(metrics metrics) func(inner http.Handler) http.Handler { | |||
return func(inner http.Handler) http.Handler { | |||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |||
// Compile regex for common file extensions | |||
staticFileRegex := regexp.MustCompile(`\.(css|js|png|jpg|jpeg|gif|ico|svg|txt|html|json|woff|woff2|ttf|eot|pdf)$`) |
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.
also, if you want to keep a regex, move it away from the method to a var in package level
Otherwise, you will compute the regex on each metrics call
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.
regexp are evil
Pull Request Template
Description:
Checklist:
goimport
andgolangci-lint
.Thank you for your contribution!