-
Notifications
You must be signed in to change notification settings - Fork 899
feat: tracing improvements #4988
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1affbe
feat: move tracing middleware higher up the middleware stack
deansheather 032f1cf
fixup! feat: move tracing middleware higher up the middleware stack
deansheather d892c21
fixup! feat: move tracing middleware higher up the middleware stack
deansheather 7383e8c
fixup! feat: move tracing middleware higher up the middleware stack
deansheather 343e27d
fixup! feat: move tracing middleware higher up the middleware stack
deansheather 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
Next
Next commit
feat: move tracing middleware higher up the middleware stack
- Loading branch information
commit f1affbebc81921838e1c2f564eb4f83acf85acc3
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
package patternmatcher | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// RoutePatterns provides a method to generate a regex which will match a URL | ||
// path against a collection of patterns. If any of the patterns match the path, | ||
// the regex will return a successful match. | ||
// | ||
// Multiple patterns can be provided and they are matched in order. Example: | ||
// - /api/* matches /api/1 but not /api or /api/1/2 | ||
// - /api/*/2 matches /api/1/2 but not /api/2 /api/1 | ||
// - /api/** matches /api/1, /api/1/2, /api/1/2/3 but not /api | ||
// - /api/**/3 matches /api/1/2, /api/1/2/3 but not /api, /api/1 or /api/1/2 | ||
// | ||
// All patterns support an optional trailing slash. | ||
type RoutePatterns []string | ||
|
||
func (rp RoutePatterns) MustCompile() *regexp.Regexp { | ||
re, err := rp.Compile() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return re | ||
} | ||
|
||
func (rp RoutePatterns) Compile() (*regexp.Regexp, error) { | ||
patterns := make([]string, len(rp)) | ||
for i, p := range rp { | ||
p = strings.ReplaceAll(p, "**", ".+") | ||
p = strings.ReplaceAll(p, "*", "[^/]+") | ||
if !strings.HasSuffix(p, "/") { | ||
p += "/?" | ||
} | ||
patterns[i] = p | ||
} | ||
|
||
pattern := fmt.Sprintf("^(%s)$", strings.Join(patterns, "|")) | ||
re, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return nil, xerrors.Errorf("compile regex %q: %w", pattern, err) | ||
} | ||
|
||
return re, nil | ||
} |
This file contains hidden or 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,99 @@ | ||
package patternmatcher_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/coderd/httpmw/patternmatcher" | ||
) | ||
|
||
func Test_RoutePatterns(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
name string | ||
patterns []string | ||
errContains string | ||
output string | ||
}{ | ||
deansheather marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
name: "Empty", | ||
patterns: []string{}, | ||
output: "^()$", | ||
}, | ||
{ | ||
name: "Single", | ||
patterns: []string{ | ||
"/api", | ||
}, | ||
output: "^(/api/?)$", | ||
}, | ||
{ | ||
name: "Multiple", | ||
patterns: []string{ | ||
"/api", | ||
"/api2", | ||
}, | ||
output: "^(/api/?|/api2/?)$", | ||
}, | ||
{ | ||
name: "Star", | ||
patterns: []string{ | ||
"/api/*", | ||
}, | ||
output: "^(/api/[^/]+/?)$", | ||
}, | ||
{ | ||
name: "StarStar", | ||
patterns: []string{ | ||
"/api/**", | ||
}, | ||
output: "^(/api/.+/?)$", | ||
}, | ||
{ | ||
name: "TelemetryPatterns", | ||
patterns: []string{ | ||
"/api", | ||
"/api/**", | ||
"/@*/*/apps/**", | ||
"/%40*/*/apps/**", | ||
"/gitauth/*/callback", | ||
}, | ||
output: "^(/api/?|/api/.+/?|/@[^/]+/[^/]+/apps/.+/?|/%40[^/]+/[^/]+/apps/.+/?|/gitauth/[^/]+/callback/?)$", | ||
}, | ||
{ | ||
name: "Invalid", | ||
patterns: []string{ | ||
"/api(", | ||
}, | ||
errContains: "compile regex", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
c := c | ||
t.Run(c.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
rp := patternmatcher.RoutePatterns(c.patterns) | ||
re, err := rp.Compile() | ||
if c.errContains != "" { | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, c.errContains) | ||
|
||
require.Panics(t, func() { | ||
_ = rp.MustCompile() | ||
}) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, c.output, re.String()) | ||
|
||
require.NotPanics(t, func() { | ||
re := rp.MustCompile() | ||
require.Equal(t, c.output, re.String()) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
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 will trace every static HTTP file... is that ok?
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.
There's a filter within the middleware itself (and a test) to ensure that this is skipped on non-API routes.