Skip to content
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

Open
wants to merge 2 commits into
base: development
Choose a base branch
from

Conversation

Umang01-hash
Copy link
Contributor

@Umang01-hash Umang01-hash commented Nov 29, 2024

Pull Request Template

Description:

Screenshot 2024-11-29 at 2 59 57 PM

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

Thank you for your contribution!

@vikash
Copy link
Contributor

vikash commented Nov 29, 2024

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.

Comment on lines +33 to +37

if path == "/" || strings.HasPrefix(path, "/static") || staticFileRegex.MatchString(r.URL.Path) {
path = r.URL.Path
}

Copy link
Contributor

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

Suggested change
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)$`)
Copy link
Contributor

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

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regexp are evil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants