Skip to content

fix: use upstream path in modify response #269

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

seanbanko
Copy link

Some open-source projects expose a Prometheus-compatible API at at a subpath, e.g. /prometheus. We should use the upstream.Path configured when matching the resp.Request.URL.Path in ModifyResponse so that we can handle the case of a non-root upstream path when proxying /api/v1/rules and /api/v1/alerts.

@seanbanko seanbanko force-pushed the use-upstream-path-in-modify-response branch from c12ba39 to 3b35b04 Compare February 6, 2025 21:28
@squat
Copy link
Member

squat commented Feb 6, 2025

Hi @seanbanko can you add a concrete test case? Otherwise I'm sure this will regress

Signed-off-by: Sean Banko <[email protected]>
@seanbanko seanbanko force-pushed the use-upstream-path-in-modify-response branch from c8ad898 to 734ea73 Compare February 6, 2025 22:58
@seanbanko
Copy link
Author

seanbanko commented Feb 6, 2025

Hi @seanbanko can you add a concrete test case? Otherwise I'm sure this will regress

Great call. I've added a simple test case to TestRules that fails if the upstream path concatenation is removed from r.modifiers.

I copied rules_match_namespace_ns1.golden to rules_match_namespace_ns1_non_root_upstream.golden to make the purpose of the test clear, but we could also just use rules_match_namespace_ns1.golden in the test instead to make it clear that the expected result is the same.

@seanbanko
Copy link
Author

@squat just bumping this. Is that test case is sufficient?

@seanbanko
Copy link
Author

Just taking a look at recent commits, @simonpasquier are you able to review?

@@ -389,8 +389,8 @@ func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, o

r.mux = mux
r.modifiers = map[string]func(*http.Response) error{
"/api/v1/rules": modifyAPIResponse(r.filterRules),
"/api/v1/alerts": modifyAPIResponse(r.filterAlerts),
upstream.Path + "/api/v1/rules": modifyAPIResponse(r.filterRules),
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use https://pkg.go.dev/net/url#JoinPath instead?

It might be the right time to use a proper struct for modifies instead of an opaque map[string]func.

Copy link
Author

Choose a reason for hiding this comment

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

Updated to use url.JoinPath.

I agree the map for modifies isn't ideal. Do you think it's better to have a separate PR for the refactor to keep scope limited? Glad to open a follow-up.

@seanbanko seanbanko force-pushed the use-upstream-path-in-modify-response branch from 7935d27 to c918521 Compare March 21, 2025 23:28
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